Class: Bybit::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/bybit/session.rb

Overview

Session owns the Faraday connection + auth-header assembly. Every service class receives a Session instance and dispatches through public_request / sign_request. Modeled after binance-connector-ruby's Session pattern.

Signing invariant: the payload signed and the query string sent on the wire MUST be byte-identical. We enforce this by building ONE query_str (via encode_query after key-sort) and using it verbatim for both the signature payload AND the request URL — never letting Faraday's NestedParamsEncoder re-serialize the params. This avoids the classic array-value / nested-hash reorder bug.

Constant Summary collapse

PAYLOAD_QUERY_METHODS =
%i[get delete].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Session

Returns a new instance of Session.



22
23
24
25
# File 'lib/bybit/session.rb', line 22

def initialize(config)
  @config = config
  @conn = config.faraday_connection || build_connection
end

Instance Method Details

#public_request(path:, method: :get, params: nil, body: nil) ⇒ Object

Public unsigned endpoint — no X-BAPI-* headers attached. session.public_request(path: '/v5/market/kline', params: ...)



29
30
31
# File 'lib/bybit/session.rb', line 29

def public_request(path:, method: :get, params: nil, body: nil)
  dispatch(method: method, path: path, signed: false, params: params, body: body)
end

#sign_request(method:, path:, params: nil, body: nil) ⇒ Object

Signed endpoint — X-BAPI-* headers computed via Authentication.



34
35
36
# File 'lib/bybit/session.rb', line 34

def sign_request(method:, path:, params: nil, body: nil)
  dispatch(method: method, path: path, signed: true, params: params, body: body)
end