Class: Wazirx::Client::REST

Inherits:
Object
  • Object
show all
Defined in:
lib/wazirx_official/client/rest.rb,
lib/wazirx_official/client/rest/clients.rb,
lib/wazirx_official/client/rest/methods.rb,
lib/wazirx_official/client/rest/endpoints.rb,
lib/wazirx_official/client/rest/sign_request_middleware.rb,
lib/wazirx_official/client/rest/timestamp_request_middleware.rb

Defined Under Namespace

Classes: SignRequestMiddleware, TimestampRequestMiddleware

Constant Summary collapse

BASE_URL =
'https://api.wazirx.com'.freeze
METHODS =
[
  # Public API Methods
  # #ping
  { name: :ping, client: :public,
    action: :get, endpoint: :ping },
  # #time
  { name: :time, client: :public,
    action: :get, endpoint: :time },
  # #system_status
  { name: :system_status, client: :public,
    action: :get, endpoint: :system_status },
  # #exchange_info
  { name: :exchange_info, client: :public,
    action: :get, endpoint: :exchange_info },
  # #tickers
  { name: :tickers, client: :public,
    action: :get, endpoint: :tickers },
  # #ticker
  { name: :ticker, client: :public,
    action: :get, endpoint: :ticker },
  # #depth
  { name: :depth, client: :public,
    action: :get, endpoint: :depth },
  # #trades
  { name: :trades, client: :public,
    action: :get, endpoint: :trades },
  # #historical_trades
  { name: :historical_trades, client: :signed,
    action: :get, endpoint: :historical_trades },

  # Account API Methods
  # #create_order!
  { name: :create_order!, client: :signed,
    action: :post, endpoint: :order },
  # #create_test_order
  { name: :create_test_order, client: :signed,
    action: :post, endpoint: :test_order },
  # #query_order
  { name: :query_order, client: :signed,
    action: :get, endpoint: :order },
  # #cancel_order!
  { name: :cancel_order!, client: :signed,
    action: :delete, endpoint: :order },
  # #open_orders
  { name: :open_orders, client: :signed,
    action: :get, endpoint: :open_orders },
  # #cancel_open_orders!
  { name: :cancel_open_orders, client: :signed,
    action: :delete, endpoint: :open_orders },
  # #all_orders
  { name: :all_orders, client: :signed,
    action: :get, endpoint: :all_orders },
  # #account_info
  { name: :account_info, client: :signed,
    action: :get, endpoint: :account },
  # #funds
  { name: :funds_info, client: :signed,
    action: :get, endpoint: :funds },

  # Websocket Auth Token
  # #create_auth_token!
  { name: :create_auth_token!, client: :signed,
    action: :post, endpoint: :ws_auth_token }
].freeze
ENDPOINTS =
{
  # Public API Endpoints
  ping:              'v1/ping',
  time:              'v1/time',
  system_status:     'v1/systemStatus',
  exchange_info:     'v1/exchangeInfo',
  tickers:           'v1/tickers/24hr',
  ticker:            'v1/ticker/24hr',
  depth:             'v1/depth',
  trades:            'v1/trades',
  historical_trades: 'v1/historicalTrades',

  # Account API Endpoints
  order:            'v1/order',
  test_order:       'v1/order/test',
  open_orders:      'v1/openOrders',
  all_orders:       'v1/allOrders',
  account:          'v1/account',
  funds:            'v1/funds',

  # Websocket Auth Token
  ws_auth_token:    'v1/create_auth_token'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: '', secret_key: '', adapter: Faraday.default_adapter) ⇒ REST

Returns a new instance of REST.



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

def initialize(api_key: '', secret_key: '',
               adapter: Faraday.default_adapter)
  @clients = {}
  @clients[:public]   = public_client adapter
  @clients[:signed]   = signed_client api_key, secret_key, adapter
end

Class Method Details

.add_query_param(query, key, value) ⇒ Object



42
43
44
45
46
# File 'lib/wazirx_official/client/rest.rb', line 42

def self.add_query_param(query, key, value)
  query = query ? query.split('&') : []
  query << "#{Faraday::Utils.escape key}=#{Faraday::Utils.escape value}"
  query.sort.join('&')
end

.sort_body(body, key, value) ⇒ Object



48
49
50
51
52
# File 'lib/wazirx_official/client/rest.rb', line 48

def self.sort_body(body, key, value)
  body = JSON.parse(body)
  body[key] = value
  body.sort.to_h
end

Instance Method Details

#call(method, params = {}) ⇒ Object



21
22
23
24
25
# File 'lib/wazirx_official/client/rest.rb', line 21

def call(method, params={})
  return {error: {message: "No method found named #{method}, please check if misspelled!"} }if
  METHODS.select { |mthd| mthd[:name] == method.to_sym}.empty?
  send(method, params)
end

#camelize(str) ⇒ Object



54
55
56
57
# File 'lib/wazirx_official/client/rest.rb', line 54

def camelize(str)
  str.split('_')
     .map.with_index { |word, i| i.zero? ? word : word.capitalize }.join
end

#public_client(adapter) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/wazirx_official/client/rest/clients.rb', line 6

def public_client(adapter)
  Faraday.new(url: "#{BASE_URL}/sapi") do |conn|
    conn.request :multipart
    conn.request :url_encoded
    conn.response :json, content_type: /\bjson$/
    conn.adapter adapter
  end
end

#signed_client(api_key, secret_key, adapter) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wazirx_official/client/rest/clients.rb', line 15

def signed_client(api_key, secret_key, adapter)
  Faraday.new(url: "#{BASE_URL}/sapi") do |conn|
    conn.request :multipart
    conn.request :url_encoded
    conn.response :json, content_type: /\bjson$/
    conn.headers['X-API-KEY'] = api_key
    conn.use TimestampRequestMiddleware
    conn.use SignRequestMiddleware, secret_key
    conn.adapter :net_http
  end
end