class CoinTracking::Api

Constants

API_URL
DEBUG

Public Class Methods

new(api_key, secret_key) click to toggle source
# File lib/coin_tracking/api.rb, line 10
def initialize(api_key, secret_key)
  @api_key, @secret_key = api_key, secret_key
end

Public Instance Methods

balance(params = {}) click to toggle source
# File lib/coin_tracking/api.rb, line 18
def balance(params = {})
  api_query('getBalance', sanitize_params(params))
end
gains(params = {}) click to toggle source
# File lib/coin_tracking/api.rb, line 34
def gains(params = {})
  api_query('getGains', sanitize_params(params))
end
grouped_balance(params = {}) click to toggle source
# File lib/coin_tracking/api.rb, line 30
def grouped_balance(params = {})
  api_query('getGroupedBalance', sanitize_params(params))
end
historical_currency(params = {}) click to toggle source
# File lib/coin_tracking/api.rb, line 26
def historical_currency(params = {})
  api_query('getHistoricalCurrency', sanitize_params(params))
end
historical_summary(params = {}) click to toggle source
# File lib/coin_tracking/api.rb, line 22
def historical_summary(params = {})
  api_query('getHistoricalSummary', sanitize_params(params))
end
trades(params = {}) click to toggle source
# File lib/coin_tracking/api.rb, line 14
def trades(params = {})
  api_query('getTrades', sanitize_params(params))
end

Private Instance Methods

api_query(action, params = {}) click to toggle source
# File lib/coin_tracking/api.rb, line 62
def api_query(action, params = {})
  params   = { method: action, nonce: nonce }.merge(params)
  response = HTTParty.post API_URL, body: params, headers: http_headers(params), debug_output: DEBUG ? $stdout : nil
  CoinTracking::Response.new(response)
end
encoded_params(params) click to toggle source
# File lib/coin_tracking/api.rb, line 68
def encoded_params(params)
  HTTParty::HashConversions.to_params(params)
end
http_headers(params) click to toggle source
# File lib/coin_tracking/api.rb, line 80
def http_headers(params)
  { 'Key' => @api_key,
    'Sign' => signed_params(params),
    'Connection' => 'close',
    'User-Agent' => user_agent }
end
nonce() click to toggle source
# File lib/coin_tracking/api.rb, line 76
def nonce
  Time.now.to_i
end
sanitize_params(params = {}) click to toggle source
# File lib/coin_tracking/api.rb, line 40
def sanitize_params(params = {})
  %w(start end).each do |key|
    sym = key.to_sym
    params[key] = time_to_unix(params[key]) if params.include?(key)
    params[sym] = time_to_unix(params[sym]) if params.include?(sym)
  end
  params
end
signed_params(params) click to toggle source
# File lib/coin_tracking/api.rb, line 72
def signed_params(params)
  OpenSSL::HMAC.hexdigest('sha512', @secret_key, encoded_params(params))
end
time_to_unix(v) click to toggle source
# File lib/coin_tracking/api.rb, line 49
def time_to_unix(v)
  case v
  when DateTime, Date
    v.to_time.to_i
  when Time, String
    v.to_i
  when Integer
    v
  else
    raise ArgumentError.new("Invalid timestamp type: #{v.class}.")
  end
end
user_agent() click to toggle source
# File lib/coin_tracking/api.rb, line 87
def user_agent
  "CoinTracking.rb/#{CoinTracking::VERSION} (#{CoinTracking::SOURCE_URL})"
end