class Binance::Session

Session has the http request connection

Public Class Methods

new(options = {}) click to toggle source
# File lib/binance/session.rb, line 12
def initialize(options = {})
  @base_url = options[:base_url] || 'https://api.binance.com'
  @auth = Authentication.new(options[:key], options[:secret])
  @logger = options[:logger]
  @show_weight_usage = options[:show_weight_usage] || false
  @show_header = options[:show_header] || false
  @timeout = options[:timeout]
end

Public Instance Methods

limit_request(method: :get, path: '/', params: {}) click to toggle source
# File lib/binance/session.rb, line 25
def limit_request(method: :get, path: '/', params: {})
  process_request(limit_conn, method, path, params)
end
public_request(path: '/', params: {}) click to toggle source
# File lib/binance/session.rb, line 21
def public_request(path: '/', params: {})
  process_request(public_conn, :get, path, params)
end
sign_request(method, path, params: {}) click to toggle source
# File lib/binance/session.rb, line 29
def sign_request(method, path, params: {})
  process_request(signed_conn, method, path, params)
end

Private Instance Methods

build_connection() { |client| ... } click to toggle source
# File lib/binance/session.rb, line 85
def build_connection
  Faraday.new(url: @base_url) do |client|
    prepare_headers(client)
    client.options.timeout = @timeout
    client.options.params_encoder = Binance::Utils::Faraday::CustomParamsEncoder
    yield client if block_given?
    client.use Faraday::Response::RaiseError
    logger_response(client)
    client.adapter Faraday.default_adapter
  end
end
extract_response(response) click to toggle source
# File lib/binance/session.rb, line 44
def extract_response(response)
  begin
    data = JSON.parse(response.body, symbolize_names: true)
  rescue JSON::ParserError
    data = response.body
  end

  return data if !@show_header && !@show_weight_usage

  res = { data: data }
  res[:header] = response.headers if @show_header
  res[:weight_usage] = response.headers.select { |k, _| weight_usage?(k) } if @show_weight_usage
  res
end
limit_conn() click to toggle source
# File lib/binance/session.rb, line 71
def limit_conn
  build_connection do |conn|
    conn.headers['X-MBX-APIKEY'] = @auth.key
  end
end
logger_response(client) click to toggle source
# File lib/binance/session.rb, line 97
def logger_response(client)
  client.response :logger, @logger if @logger
end
path_with_query(path, params) click to toggle source
# File lib/binance/session.rb, line 63
def path_with_query(path, params)
  "#{path}?#{Binance::Utils::Url.build_query(params)}"
end
prepare_headers(client) click to toggle source
# File lib/binance/session.rb, line 101
def prepare_headers(client)
  client.headers['Content-Type'] = 'application/json'
  client.headers['User-Agent'] = "binance-connector-ruby/#{Binance::VERSION}"
end
process_request(conn, method, path, params) click to toggle source
# File lib/binance/session.rb, line 35
def process_request(conn, method, path, params)
  response = conn.send(method, path_with_query(path, params.compact), nil)
  extract_response(response)
rescue Faraday::ClientError => e
  raise Binance::ClientError, e.response
rescue Faraday::ServerError => e
  raise Binance::ServerError, e
end
public_conn() click to toggle source
# File lib/binance/session.rb, line 67
def public_conn
  build_connection
end
signed_conn() click to toggle source
# File lib/binance/session.rb, line 77
def signed_conn
  build_connection do |conn|
    conn.headers['X-MBX-APIKEY'] = @auth.key
    conn.use Timestamp
    conn.use Signature, @auth.secret
  end
end
weight_usage?(key) click to toggle source
# File lib/binance/session.rb, line 59
def weight_usage?(key)
  key.start_with?('x-mbx-used-weight') || key.start_with?('x-sapi-used')
end