Class: Rybit::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Requests::Account, Requests::Trade
Defined in:
lib/rybit/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Requests::Trade

#create_order, #get_open_orders

Methods included from Requests::Account

#get_wallet_balance

Constructor Details

#initialize(api_key:, secret_key:, testnet: false, recv_window: '5000') ⇒ Client

debug_output $stdout



13
14
15
16
17
18
19
# File 'lib/rybit/client.rb', line 13

def initialize(api_key:, secret_key:, testnet: false, recv_window: '5000')
  @api_key = api_key
  @secret_key = secret_key
  @testnet = testnet
  @recv_window = recv_window
  @base_url = "https://#{testnet ? 'api-testnet' : 'api'}.bybit.com"
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/rybit/client.rb', line 9

def api_key
  @api_key
end

#base_urlObject

Returns the value of attribute base_url.



9
10
11
# File 'lib/rybit/client.rb', line 9

def base_url
  @base_url
end

#recv_windowObject

Returns the value of attribute recv_window.



9
10
11
# File 'lib/rybit/client.rb', line 9

def recv_window
  @recv_window
end

#secret_keyObject

Returns the value of attribute secret_key.



9
10
11
# File 'lib/rybit/client.rb', line 9

def secret_key
  @secret_key
end

#testnetObject

Returns the value of attribute testnet.



9
10
11
# File 'lib/rybit/client.rb', line 9

def testnet
  @testnet
end

Instance Method Details

#request(method, endpoint, payload = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rybit/client.rb', line 21

def request(method, endpoint, payload = {})
  full_url = base_url + endpoint
  timestamp = DateTime.now.strftime('%Q')
  payload = method == :post ? payload.to_json : HTTParty::HashConversions.to_params(payload)
  headers = {
    'X-BAPI-API-KEY' => api_key,
    'X-BAPI-TIMESTAMP' => timestamp,
    'X-BAPI-RECV-WINDOW' => recv_window,
    'X-BAPI-SIGN' => signature(payload, timestamp),
    'Content-Type' => ('application/json' if method == :post)
  }.compact

  wrapper = method == :post ? :body : :query
  response = self.class.send(method, full_url, wrapper => payload, headers: headers)
  response.body
end