Class: Coinbasepro::Api::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbasepro/api/api_client.rb

Overview

Net-http client for Coinbase Pro API

Direct Known Subclasses

EMHTTPClient, NetHTTPClient

Instance Method Summary collapse

Constructor Details

#initialize(api_key = '', api_secret = '', api_pass = '', options = {}) ⇒ APIClient

Returns a new instance of APIClient.



5
6
7
8
9
10
11
# 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

Instance Method Details

#account(id, params = {}) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/coinbasepro/api/api_client.rb', line 118

def (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 = {}) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/coinbasepro/api/api_client.rb', line 127

def (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 = {}) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/coinbasepro/api/api_client.rb', line 136

def (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 = {}) ⇒ Object

Accounts



109
110
111
112
113
114
115
116
# 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 = {}) ⇒ Object Also known as: sell



174
175
176
177
178
179
180
181
182
183
184
185
186
# 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

#bid(amt, price, params = {}) ⇒ Object Also known as: buy



159
160
161
162
163
164
165
166
167
168
169
170
171
# 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

#cancel(id) ⇒ Object



189
190
191
192
193
194
195
196
# 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 = {}) ⇒ Object



301
302
303
304
305
306
307
308
# 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 = {}) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/coinbasepro/api/api_client.rb', line 268

def coinbase_withdrawal(amount, currency, , params = {})
  params[:amount] = amount
  params[:currency] = currency
  params[: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 = {}) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
# 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 = {}) ⇒ Object

Market Data



22
23
24
25
26
27
28
29
# 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 = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# 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 = {}) ⇒ Object

Transfers



230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/coinbasepro/api/api_client.rb', line 230

def deposit(, amt, params = {})
  params[:type] = "deposit"
  params[:coinbase_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 = {}) ⇒ Object

Orders



149
150
151
152
153
154
155
156
157
# 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 = {}) ⇒ Object



310
311
312
313
314
315
316
# 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 = {}) ⇒ Object



218
219
220
221
222
223
224
225
# 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 = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# 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 = {}) ⇒ Object



209
210
211
212
213
214
215
216
# 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 = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# 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 = {}) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# 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 = {}) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
# 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 = {}) ⇒ Object



292
293
294
295
296
297
298
299
# 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 = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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 = {}) ⇒ Object



31
32
33
34
35
36
37
38
# 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

#server_epoch(params = {}) ⇒ Object



13
14
15
16
17
# 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 = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# 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 = {}) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/coinbasepro/api/api_client.rb', line 243

def withdraw(, amt, params = {})
  params[:type] = "withdraw"
  params[:coinbase_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