class FTX::API::Base

Attributes

config[R]
key[R]
secret[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/ftx/api/base.rb, line 12
def initialize(args = {})
  @config = FTX::API::Config.new(args.dig(:config))
  @key = args.fetch(:key, ENV['FTX_KEY'])
  @secret = args.fetch(:secret, ENV['FTX_SECRET'])
end

Protected Instance Methods

send_request(method, path, headers, query) click to toggle source
# File lib/ftx/api/base.rb, line 20
def send_request(method, path, headers, query)
  uuid = SecureRandom.uuid
  print_log(:info, "[API] #{uuid} #{method.upcase} '#{path}' query = #{query}")

  if method == :get
    body_or_query = :query
  else
    body_or_query = :body
    query = query.to_json
  end

  begin
    response = self.class.send(
      method, 
      path, 
      headers: headers,
      timeout: @config.timeout,
      body_or_query.to_sym => query
    )

    print_log(:info, "[API] #{uuid} response #{response}")
    return parse_response(response)
  rescue => error
    print_log(:error, "[API] #{uuid} raise exception #{error.message}")
    raise error
  end
end

Private Instance Methods

parse_response(response) click to toggle source
# File lib/ftx/api/base.rb, line 50
def parse_response(response)
  response = response.parsed_response
  if response.dig("success")
    response.dig("result").symbolize_keys
  else
    response.symbolize_keys
  end
end
print_log(method, message) click to toggle source