Class: Coinbase::Exchange::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/exchange/api_client.rb

Overview

Net-http client for Coinbase Exchange 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/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

Instance Method Details

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



118
119
120
121
122
123
124
125
# File 'lib/coinbase/exchange/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/coinbase/exchange/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/coinbase/exchange/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/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 = {}) ⇒ Object Also known as: sell



163
164
165
166
167
168
169
170
171
172
173
174
175
# 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

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

Orders



148
149
150
151
152
153
154
155
156
157
158
159
160
# 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

#cancel(id) ⇒ Object



178
179
180
181
182
183
184
185
# 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 = {}) ⇒ Object

Market Data



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



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

Transfers



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/coinbase/exchange/api_client.rb', line 219

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

#fills(params = {}) ⇒ Object



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



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



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



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



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



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



31
32
33
34
35
36
37
38
# 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 = {}) ⇒ Object



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



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



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/coinbase/exchange/api_client.rb', line 232

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