class CobinhoodApi

Constants

VERB_MAP

Public Class Methods

new(api_key:nil,console_log:false,logfile:"cobinhood.log") click to toggle source
# File lib/cobinhood_api.rb, line 75
def initialize(api_key:nil,console_log:false,logfile:"cobinhood.log")
    unless api_key.nil?
        @auth = {:authorization => api_key}
        @live_api = URI(LIVE_API)
        @http =  Net::HTTP.new(@live_api.host,@live_api.port)
        @http.use_ssl = true
        #@http.set_debug_output($stdout)
    else
        @auth = nil
        @live_api = nil
        @http = nil
    end

    @loggers = []
    if console_log
        @loggers.push(Logger.new(STDOUT))
    end
    unless logfile.nil?
        @loggers.push(Logger.new(logfile))
    end
end

Public Instance Methods

add_filter(filter,value,url,params) click to toggle source
# File lib/cobinhood_api.rb, line 175
def add_filter(filter,value,url,params)
    unless value.nil?
        if url["?"]
            url += "&"
        else
            url += "?"
        end
        url += filter + "=%{" + filter + "}"
        params[filter.to_sym] = value
    end
    url
end
cancel_order(order_id) click to toggle source
# File lib/cobinhood_api.rb, line 309
def cancel_order(order_id)
    params = {:order_id => order_id}
    outcome delete(TRADING_ORDER, params:params)
end
delete(request_url,params:nil) click to toggle source
# File lib/cobinhood_api.rb, line 146
def delete(request_url,params:nil)
    request(:delete,request_url,params:params,auth:true)
end
get(request_url,params:nil,auth:nil) click to toggle source
# File lib/cobinhood_api.rb, line 130
def get(request_url,params:nil,auth:nil)
    request(:get,request_url,params:params,auth:auth)
end
get_all_last_prices() click to toggle source
# File lib/cobinhood_api.rb, line 220
def get_all_last_prices
    stats = result get(MARKET_STATS)
    unless stats.nil?
        prices = {}
        stats.each do |pair,stat|
            prices[pair] = stat["last_price"].to_f
        end
        prices
    end
end
get_auth(request_url,params:nil) click to toggle source
# File lib/cobinhood_api.rb, line 134
def get_auth(request_url,params:nil)
    request(:get,request_url,params:params,auth:true)
end
get_chart_candles(trading_pair,timeframe=CHART_CANDLE_TIMEFRAMES::TIMEFRAME_1_HOUR,start_time=nil,end_time=nil) click to toggle source
# File lib/cobinhood_api.rb, line 242
def get_chart_candles(trading_pair,timeframe=CHART_CANDLE_TIMEFRAMES::TIMEFRAME_1_HOUR,start_time=nil,end_time=nil)
    params = {:trading_pair => trading_pair,:timeframe => timeframe}
    base_url = CHART_CANDLES
    base_url = add_filter("start_time",start_time,base_url,params)
    base_url = add_filter("end_time", end_time, base_url, params)
    result get(base_url,params:params) ,"candles"
end
get_deposit(deposit_id) click to toggle source
# File lib/cobinhood_api.rb, line 352
def get_deposit(deposit_id)
    params = {:deposit_id => deposit_id}
    result get_auth(WALLET_DEPOSIT, params:params),"deposit"
end
get_deposit_addresses(currency=nil) click to toggle source
# File lib/cobinhood_api.rb, line 323
def get_deposit_addresses(currency=nil)
    params = {}
    base_url = add_filter("currency",currency, WALLET_DEPOSIT_ADDRESSES, params)
    result get_auth(base_url, params:params),"deposit_addresses"
end
get_deposit_history(currency=nil) click to toggle source
# File lib/cobinhood_api.rb, line 335
def get_deposit_history(currency=nil)
    params = {}
    base_url = add_filter("currency", currency, WALLET_DEPOSITS, params)
    result get_auth(base_url, params:params),"deposits"
end
get_ledger(currency=nil) click to toggle source
# File lib/cobinhood_api.rb, line 317
def get_ledger(currency=nil)
    params = {}
    base_url = add_filter("currency", currency, WALLET_LEDGER, params)
    result get_auth(base_url, params:params) ,"ledger"
end
get_market_currencies() click to toggle source
# File lib/cobinhood_api.rb, line 197
def get_market_currencies
    result get(MARKET_CURRENCIES),"currencies"
end
get_market_order_book(trading_pair,limit=50) click to toggle source
# File lib/cobinhood_api.rb, line 205
def get_market_order_book(trading_pair,limit=50)
    raw_order_book = result get(MARKET_ORDER_BOOK,params:{:trading_pair=>trading_pair,:limit=>limit}),"orderbook"
    order_book = {asks:[],bids:[]}
    %w(asks bids).each do |side|
        raw_order_book[side].each do |offer|
            order_book[side.to_sym].push({"price" => offer[0].to_f, "size" => offer[2].to_f,"count" => offer[1].to_i})
        end
    end
    order_book
end
get_market_stats() click to toggle source
# File lib/cobinhood_api.rb, line 216
def get_market_stats
    result get(MARKET_STATS)
end
get_market_trading_pairs() click to toggle source
# File lib/cobinhood_api.rb, line 201
def get_market_trading_pairs
    result get(MARKET_TRADING_PAIRS),"trading_pairs"
end
get_order(order_id) click to toggle source
# File lib/cobinhood_api.rb, line 265
def get_order(order_id)
    params = {:order_id => order_id}
    result get_auth(TRADING_ORDER, params:params),"order"
end
get_order_history(trading_pair=nil) click to toggle source
# File lib/cobinhood_api.rb, line 259
def get_order_history(trading_pair=nil)
    params ={}
    base_url = add_filter("trading_pair_id",trading_pair,TRADING_ORDERS_HISTORY,params)
    result get_auth(base_url,params:params) , "orders"
end
get_order_trades(order_id) click to toggle source
# File lib/cobinhood_api.rb, line 270
def get_order_trades(order_id)
    params = {:order_id => order_id}
    result get_auth(TRADING_ORDER_TRADES, params:params),"trades"
end
get_orders(trading_pair=nil) click to toggle source
# File lib/cobinhood_api.rb, line 253
def get_orders(trading_pair=nil)
    params ={}
    base_url = add_filter("trading_pair_id",trading_pair,TRADING_ORDERS,params)
    result get_auth(base_url,params:params) , "orders"
end
get_recent_trades(trading_pair) click to toggle source
# File lib/cobinhood_api.rb, line 235
def get_recent_trades(trading_pair)
    result get(MARKET_RECENT_TRADES, params:{:trading_pair=>trading_pair}),"trades"
end
get_system_info() click to toggle source
# File lib/cobinhood_api.rb, line 189
def get_system_info
    result get(SYSTEM_INFO) ,"info"
end
get_system_time() click to toggle source
# File lib/cobinhood_api.rb, line 193
def get_system_time
    result get(SYSTEM_TIME)
end
get_ticker(trading_pair) click to toggle source
# File lib/cobinhood_api.rb, line 231
def get_ticker(trading_pair)
    result get(MARKET_TICKER, params:{:trading_pair=>trading_pair}) , "ticker"
end
get_trade(trade_id) click to toggle source
# File lib/cobinhood_api.rb, line 275
def get_trade(trade_id)
    params = {:trade_id => trade_id}
    result get_auth(TRADING_TRADE, params:params) , "trade"
end
get_trade_history(trading_pair=nil) click to toggle source
# File lib/cobinhood_api.rb, line 280
def get_trade_history(trading_pair=nil)
    params ={}
    base_url = add_filter("trading_pair_id",trading_pair,TRADING_TRADE_HISTORY,params)
    result get_auth(base_url, params:params),"trades"
end
get_withdrawal(withdrawal_id) click to toggle source
# File lib/cobinhood_api.rb, line 347
def get_withdrawal(withdrawal_id)
    params = {:withdrawal_id =>withdrawal_id}
    result get_auth(WALLET_WITHDRAWAL, params:params),"withdrawal"
end
get_withdrawal_addresses(currency=nil) click to toggle source
# File lib/cobinhood_api.rb, line 329
def get_withdrawal_addresses(currency=nil)
    params = {}
    base_url = add_filter("currency", currency, WALLET_WITHDRAWAL_ADDRESSES, params)
    result get_auth(base_url, params:params),"withdrawal_addresses"
end
get_withdrawal_history(currency=nil) click to toggle source
# File lib/cobinhood_api.rb, line 341
def get_withdrawal_history(currency=nil)
    params = {}
    base_url = add_filter("currency", currency, WALLET_WITHDRAWALS, params)
    result get_auth(base_url, params:params),"withdrawals"
end
log(message) click to toggle source
# File lib/cobinhood_api.rb, line 97
def log(message)
    @loggers.each do |logger|
        logger.info(message)
    end
end
modify_order(order_id,size,price) click to toggle source
# File lib/cobinhood_api.rb, line 300
def modify_order(order_id,size,price)
    params = {:order_id => order_id}
    data = {
        :size => size.to_s,
        :price => price.to_s
    }
    outcome put(TRADING_ORDER,params:params,data:data)
end
outcome(request_result) click to toggle source
# File lib/cobinhood_api.rb, line 165
def outcome(request_result)
    return nil if request_result.nil?
    return nil unless request_result.key?("success") and request_result.key?("result")
    request_result['success']
end
place_order(trading_pair,side,order_type,size,price=nil) click to toggle source
# File lib/cobinhood_api.rb, line 286
def place_order(trading_pair,side,order_type,size,price=nil)
    unless order_type == TRADING_ORDER_TYPE::TYPE_MARKET
        return nil if price.nil?
    end
    data = {
        :trading_pair_id => trading_pair,
        :side => side,
        :type => order_type,
        :size => size.to_s,
        :price => price.to_s
    }
    result post(TRADING_ORDERS,data:data) , "order"
end
post(request_url,params:nil,data:nil) click to toggle source
# File lib/cobinhood_api.rb, line 142
def post(request_url,params:nil,data:nil)
    request(:post,request_url,params:params,data:data,auth:true)
end
put(request_url,params:nil,data:nil) click to toggle source
# File lib/cobinhood_api.rb, line 138
def put(request_url,params:nil,data:nil)
    request(:put,request_url,params:params,data:data,auth:true)
end
request(method,request_url,params:nil,data:nil,auth:false) click to toggle source
# File lib/cobinhood_api.rb, line 106
def request(method,request_url,params:nil,data:nil,auth:false)
    unless params.nil?
        request_url = request_url % params
    end
    uri = URI(LIVE_API + request_url)
    log(method.to_s + " " + uri.to_s + " data=" + data.to_s)

    if auth
        request = VERB_MAP[method].new(uri.request_uri)
        request[:authorization] = @auth[:authorization]
        unless method.equal? :get
            request[:nonce] = Time.now.to_i.to_s
        end
        unless data.nil?
            request.body = JSON.generate(data)
        end
        response = @http.request(request).body
    else
        response = Net::HTTP.get(uri)
    end
    JSON.parse(response)

end
result(request_result,entry=nil) click to toggle source
# File lib/cobinhood_api.rb, line 153
def result(request_result,entry=nil)
    return nil if request_result.nil?
    return nil unless request_result.key?("success") and request_result.key?("result")
    return request_result unless request_result["success"]

    if entry.nil?
        request_result["result"]
    else
        request_result["result"][entry]
    end
end