class Coinbase::Exchange::APIClient
Public Class Methods
new(api_key = '', api_secret = '', api_pass = '', options = {})
click to toggle source
# File lib/coinbase/exchange/api_client.rb, line 5 def initialize(api_key = '', api_secret = '', api_pass = '', options = {}) @api_uri = URI.parse(options[:api_url] || "https://api.gdax.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/coinbase/exchange/api_client.rb, line 337 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/coinbase/exchange/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/coinbase/exchange/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/coinbase/exchange/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/coinbase/exchange/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/coinbase/exchange/api_client.rb, line 163 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
Orders
# File lib/coinbase/exchange/api_client.rb, line 148 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
cancel(id) { |out, resp| ... }
click to toggle source
# File lib/coinbase/exchange/api_client.rb, line 178 def cancel(id) out = nil delete("/orders/#{id}") 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/coinbase/exchange/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/coinbase/exchange/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/coinbase/exchange/api_client.rb, line 219 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
fills(params = {}) { |out, resp| ... }
click to toggle source
# File lib/coinbase/exchange/api_client.rb, line 207 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/coinbase/exchange/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/coinbase/exchange/api_client.rb, line 198 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/coinbase/exchange/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/coinbase/exchange/api_client.rb, line 187 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
price_history(params = {}) { |out, resp| ... }
click to toggle source
# File lib/coinbase/exchange/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/coinbase/exchange/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
server_epoch(params = {}) { |resp| ... }
click to toggle source
# File lib/coinbase/exchange/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/coinbase/exchange/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/coinbase/exchange/api_client.rb, line 232 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/coinbase/exchange/api_client.rb, line 261 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/coinbase/exchange/api_client.rb, line 320 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/coinbase/exchange/api_client.rb, line 282 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/coinbase/exchange/api_client.rb, line 333 def http_verb(_method, _path, _body) fail NotImplementedError end
post(path, params = {}) { |out| ... }
click to toggle source
# File lib/coinbase/exchange/api_client.rb, line 307 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/coinbase/exchange/api_client.rb, line 267 def raw @response.raw end
response()
click to toggle source
# File lib/coinbase/exchange/api_client.rb, line 263 def response @response end
response_collection(resp)
click to toggle source
# File lib/coinbase/exchange/api_client.rb, line 247 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/coinbase/exchange/api_client.rb, line 271 def response_headers @response.headers end
response_object(resp)
click to toggle source
# File lib/coinbase/exchange/api_client.rb, line 254 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/coinbase/exchange/api_client.rb, line 275 def response_status @response.status end