class CoinSync::Importers::BittrexAPI

Constants

BASE_URL

Public Class Methods

new(config, params = {}) click to toggle source
Calls superclass method CoinSync::Importers::Base::new
# File lib/coinsync/importers/bittrex_api.rb, line 19
def initialize(config, params = {})
  super

  # only "Read Info" permission is required for the key
  @api_key = params['api_key']
  @api_secret = params['api_secret']
end

Public Instance Methods

can_build?() click to toggle source
# File lib/coinsync/importers/bittrex_api.rb, line 31
def can_build?
  false
end
can_import?(type) click to toggle source
# File lib/coinsync/importers/bittrex_api.rb, line 27
def can_import?(type)
  @api_key && @api_secret && [:balances].include?(type)
end
import_balances() click to toggle source
# File lib/coinsync/importers/bittrex_api.rb, line 35
def import_balances
  response = make_request('/account/getbalances')

  case response
  when Net::HTTPSuccess
    json = JSON.parse(response.body)

    if json['success'] != true || !json['result']
      raise "Bittrex importer: Invalid response: #{response.body}"
    end

    return json['result'].select { |b|
      b['Balance'] > 0
    }.map { |b|
      Balance.new(
        CryptoCurrency.new(b['Currency']),
        available: BigDecimal.new(b['Available'], 0),
        locked: BigDecimal.new(b['Balance'], 0) - BigDecimal.new(b['Available'], 0)
      )
    }
  when Net::HTTPBadRequest
    raise "Bittrex importer: Bad request: #{response}"
  else
    raise "Bittrex importer: Bad response: #{response}"
  end
end

Private Instance Methods

make_request(path, params = {}) click to toggle source
# File lib/coinsync/importers/bittrex_api.rb, line 64
def make_request(path, params = {})
  (@api_key && @api_secret) or raise "Public and secret API keys must be provided"

  params['apikey'] = @api_key
  params['nonce'] = (Time.now.to_f * 1000).to_i

  url = URI(BASE_URL + path)
  url.query = URI.encode_www_form(params)

  hmac = OpenSSL::HMAC.hexdigest('sha512', @api_secret, url.to_s)

  Request.get(url) do |request|
    request['apisign'] = hmac
  end
end