class Coinbasepro::Api::APIClient

Net-http client for Coinbase Pro API

Public Class Methods

new(api_key = '', api_secret = '', api_pass = '', options = {}) click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 5
def initialize(api_key = '', api_secret = '', api_pass = '', options = {})
  @api_uri = URI.parse(options[:api_url] || "https://api.pro.coinbase.com")
  @api_pass = api_pass
  @api_key = api_key
  @api_secret = api_secret
  @default_product = options[:product_id] || "BTC-USD"
end

Private Class Methods

whitelisted_certificates() click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 410
def self.whitelisted_certificates
  path = File.expand_path(File.join(File.dirname(__FILE__), 'ca-coinbase.crt'))

  certs = [ [] ]
  File.readlines(path).each do |line|
    next if ["\n", "#"].include?(line[0])
    certs.last << line
    certs << [] if line == "-----END CERTIFICATE-----\n"
  end

  result = OpenSSL::X509::Store.new

  certs.each do |lines|
    next if lines.empty?
    cert = OpenSSL::X509::Certificate.new(lines.join)
    result.add_cert(cert)
  end

  result
end

Public Instance Methods

account(id, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 118
def account(id, params = {})
  out = nil
  get("/accounts/#{id}", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
account_history(id, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 127
def account_history(id, params = {})
  out = nil
  get("/accounts/#{id}/ledger", params, paginate: true) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
account_holds(id, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 136
def account_holds(id, params = {})
  out = nil
  get("/accounts/#{id}/holds", params, paginate: true) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
accounts(params = {}) { |out, resp| ... } click to toggle source

Accounts

# File lib/coinbasepro/api/api_client.rb, line 109
def accounts(params = {})
  out = nil
  get("/accounts", params) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
ask(amt, price, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 174
def ask(amt, price, params = {})
  params[:product_id] ||= @default_product
  params[:size] = amt
  params[:price] = price
  params[:side] = "sell"

  out = nil
  post("/orders", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
Also aliased as: sell
bid(amt, price, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 159
def bid(amt, price, params = {})
  params[:product_id] ||= @default_product
  params[:size] = amt
  params[:price] = price
  params[:side] = "buy"

  out = nil
  post("/orders", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
Also aliased as: buy
buy(amt, price, params = {})
Alias for: bid
cancel(id) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 189
def cancel(id)
  out = nil
  delete("/orders/#{id}") do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
coinbase_accounts(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 301
def coinbase_accounts(params = {})
  out = nil
  get("/coinbase-accounts", params, paginate: true) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
coinbase_withdrawal(amount, currency, coinbase_account_id, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 268
def coinbase_withdrawal(amount, currency, coinbase_account_id, params = {})
  params[:amount] = amount
  params[:currency] = currency
  params[:coinbase_account_id] = coinbase_account_id
   out = nil
  post("/withdrawals/coinbase-account", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
crypto_withdrawal(amount, currency, crypto_address, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 280
def crypto_withdrawal(amount, currency, crypto_address, params = {})
  params[:amount] = amount
  params[:currency] = currency
  params[:crypto_address] = crypto_address
   out = nil
  post("/withdrawals/crypto", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
currencies(params = {}) { |out, resp| ... } click to toggle source

Market Data

# File lib/coinbasepro/api/api_client.rb, line 22
def currencies(params = {})
  out = nil
  get("/currencies", params) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
daily_stats(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 94
def daily_stats(params = {})
  product = params[:product_id] || @default_product

  out = nil
  get("/products/#{product}/stats", params) do |resp|
    resp["start"] = (Time.now - 24 * 60 * 60).to_s
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
deposit(account_id, amt, params = {}) { |out, resp| ... } click to toggle source

Transfers

# File lib/coinbasepro/api/api_client.rb, line 230
def deposit(account_id, amt, params = {})
  params[:type] = "deposit"
  params[:coinbase_account_id] = account_id
  params[:amount] = amt

  out = nil
  post("/transfers", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
do_order(params = {}) { |out, resp| ... } click to toggle source

Orders

# File lib/coinbasepro/api/api_client.rb, line 149
def do_order(params = {})
  params[:product_id] ||= @default_product
  out = nil
  post("/orders", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
fees(params = {}) click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 310
def fees(params = {})
  out = nil
  get("/fees") do |resp|
    out = resp
  end
  out
end
fills(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 218
def fills(params = {})
  out = nil
  get("/fills", params, paginate: true) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
last_trade(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 51
def last_trade(params = {})
  product = params[:product_id] || @default_product

  out = nil
  get("/products/#{product}/ticker", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
order(id, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 209
def order(id, params = {})
  out = nil
  get("/orders/#{id}", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
orderbook(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 40
def orderbook(params = {})
  product = params[:product_id] || @default_product

  out = nil
  get("/products/#{product}/book", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
orders(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 198
def orders(params = {})
  params[:status] ||= "all"

  out = nil
  get("/orders", params, paginate: true) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
payment_method_withdrawal(amount, currency, payment_method_id, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 256
def payment_method_withdrawal(amount, currency, payment_method_id, params = {})
  params[:amount] = amount
  params[:currency] = currency
  params[:payment_method_id] = payment_method_id
   out = nil
  post("/withdrawals/payment-method", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end
payment_methods(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 292
def payment_methods(params = {})
  out = nil
  get("/payment-methods", params, paginate: true) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
price_history(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 73
def price_history(params = {})
  product = params[:product_id] || @default_product

  out = nil
  get("/products/#{product}/candles", params) do |resp|
    out = response_collection(
      resp.map do |item|
        { 'start' => Time.at(item[0]),
          'low' => item[1],
          'high' => item[2],
          'open' => item[3],
          'close' => item[4],
          'volume' => item[5]
        }
      end
    )
    yield(out, resp) if block_given?
  end
  out
end
products(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 31
def products(params = {})
  out = nil
  get("/products", params) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
sell(amt, price, params = {})
Alias for: ask
server_epoch(params = {}) { |resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 13
def server_epoch(params = {})
  get("/time", params) do |resp|
    yield(resp) if block_given?
  end
end
trade_history(params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 62
def trade_history(params = {})
  product = params[:product_id] || @default_product

  out = nil
  get("/products/#{product}/trades", params, paginate: true) do |resp|
    out = response_collection(resp)
    yield(out, resp) if block_given?
  end
  out
end
withdraw(account_id, amt, params = {}) { |out, resp| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 243
def withdraw(account_id, amt, params = {})
  params[:type] = "withdraw"
  params[:coinbase_account_id] = account_id
  params[:amount] = amt

  out = nil
  post("/transfers", params) do |resp|
    out = response_object(resp)
    yield(out, resp) if block_given?
  end
  out
end

Private Instance Methods

add_metadata(resp) click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 334
def add_metadata(resp)
  resp.instance_eval do
    def response
      @response
    end

    def raw
      @response.raw
    end

    def response_headers
      @response.headers
    end

    def response_status
      @response.status
    end
  end
  resp
end
delete(path) { |out| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 393
def delete(path)
  http_verb('DELETE', path) do |resp|
    begin
      out = JSON.parse(resp.body)
    rescue
      out = resp.body
    end
    out.instance_eval { @response = resp }
    add_metadata(out)
    yield(out)
  end
end
get(path, params = {}, options = {}) { |out| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 355
def get(path, params = {}, options = {})
  params[:limit] ||= 100 if options[:paginate] == true

  http_verb('GET', "#{path}?#{URI.encode_www_form(params)}") do |resp|
    begin
      out = JSON.parse(resp.body)
    rescue JSON::ParserError
      out = resp.body
    end
    out.instance_eval { @response = resp }
    add_metadata(out)

    if options[:paginate] && out.count == params[:limit]
      params[:after] = resp.headers['CB-AFTER']
      get(path, params, options) do |pages|
        out += pages
        add_metadata(out)
        yield(out)
      end
    else
      yield(out)
    end
  end
end
http_verb(_method, _path, _body) click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 406
def http_verb(_method, _path, _body)
  fail NotImplementedError
end
post(path, params = {}) { |out| ... } click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 380
def post(path, params = {})
  http_verb('POST', path, params.to_json) do |resp|
    begin
      out = JSON.parse(resp.body)
    rescue JSON::ParserError
      out = resp.body
    end
    out.instance_eval { @response = resp }
    add_metadata(out)
    yield(out)
  end
end
raw() click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 340
def raw
  @response.raw
end
response() click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 336
def response
  @response
end
response_collection(resp) click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 320
def response_collection(resp)
  out = resp.map { |item| APIObject.new(item) }
  out.instance_eval { @response = resp }
  add_metadata(out)
  out
end
response_headers() click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 344
def response_headers
  @response.headers
end
response_object(resp) click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 327
def response_object(resp)
  out = APIObject.new(resp)
  out.instance_eval { @response = resp.response }
  add_metadata(out)
  out
end
response_status() click to toggle source
# File lib/coinbasepro/api/api_client.rb, line 348
def response_status
  @response.status
end