class RuBittrex::Client

Constants

API_URI

Attributes

api_key[R]
secret[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/ru_bittrex/client.rb, line 15
def initialize(opts = {})
  raise AuthError.new('You must provide API key and Secret') unless opts[:secret] && opts[:api_key]

  @secret = opts[:secret]
  @api_key = opts[:api_key]
end

Public Instance Methods

get(path, params = {}) click to toggle source
# File lib/ru_bittrex/client.rb, line 22
def get(path, params = {})
  uri = encode_uri(path, params)
  timestamp = nonce
  content_hash = digest
  presign = timestamp.to_s + uri + 'GET' + content_hash

  resp = Faraday.get(uri) do |req|
    req.params = params
    # req.params['limit'] = 100
    req.headers['Api-Key'] = @api_key
    req.headers['Api-Timestamp'] = timestamp.to_s
    req.headers['Api-Content-Hash'] = content_hash
    req.headers['Api-Signature'] = sign(presign)
    req.headers['Content-Type'] = 'application/json'
    # req.body = body.to_json
  end

  JSON.parse(resp.body)
end

Private Instance Methods

digest(body = nil) click to toggle source
# File lib/ru_bittrex/client.rb, line 54
def digest(body = nil)
  string =
    if body.is_a?(Hash)
      body.to_json
    elsif body.nil?
      ''
    end

  Digest::SHA512.hexdigest(string)
end
encode_uri(path, params) click to toggle source
# File lib/ru_bittrex/client.rb, line 44
def encode_uri(path, params)
  uri = API_URI + '/' + path
  uri = uri + '?' + URI.encode_www_form(params) if params.any?
  uri
end
nonce() click to toggle source
# File lib/ru_bittrex/client.rb, line 50
def nonce
  (Time.now.to_f * 1000).to_i
end
sign(presign) click to toggle source
# File lib/ru_bittrex/client.rb, line 65
def sign(presign)
  OpenSSL::HMAC.hexdigest('sha512', @secret, presign)
end