coinbase 0.0.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of coinbase might be problematic. Click here for more details.

Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +67 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +578 -0
  7. data/Rakefile +7 -0
  8. data/bin/console +14 -0
  9. data/bin/setup +7 -0
  10. data/coinbase.gemspec +30 -0
  11. data/lib/coinbase/wallet/adapters/em_http.rb +64 -0
  12. data/lib/coinbase/wallet/adapters/net_http.rb +62 -0
  13. data/lib/coinbase/wallet/api_client.rb +659 -0
  14. data/lib/coinbase/wallet/api_errors.rb +112 -0
  15. data/lib/coinbase/wallet/api_response.rb +37 -0
  16. data/lib/coinbase/wallet/client.rb +98 -0
  17. data/lib/coinbase/wallet/models/account.rb +187 -0
  18. data/lib/coinbase/wallet/models/api_object.rb +46 -0
  19. data/lib/coinbase/wallet/models/checkout.rb +19 -0
  20. data/lib/coinbase/wallet/models/order.rb +12 -0
  21. data/lib/coinbase/wallet/models/transaction.rb +21 -0
  22. data/lib/coinbase/wallet/models/transfer.rb +13 -0
  23. data/lib/coinbase/wallet/models/user.rb +15 -0
  24. data/lib/coinbase/wallet/version.rb +5 -0
  25. data/lib/coinbase/wallet.rb +23 -156
  26. data/spec/account_spec.rb +193 -0
  27. data/spec/clients/client_spec.rb +34 -0
  28. data/spec/clients/oauth_client_spec.rb +56 -0
  29. data/spec/endpoints_spec.rb +346 -0
  30. data/spec/error_spec.rb +130 -0
  31. data/spec/models/api_object_spec.rb +63 -0
  32. data/spec/models/checkout_spec.rb +52 -0
  33. data/spec/models/current_user_spec.rb +29 -0
  34. data/spec/models/order_spec.rb +52 -0
  35. data/spec/models/request_spec.rb +47 -0
  36. data/spec/models/transfer_spec.rb +64 -0
  37. data/spec/models/user_spec.rb +24 -0
  38. data/spec/spec_helper.rb +13 -0
  39. metadata +72 -82
  40. data/lib/coinbase/address.rb +0 -127
  41. data/lib/coinbase/asset.rb +0 -20
  42. data/lib/coinbase/balance_map.rb +0 -48
  43. data/lib/coinbase/constants.rb +0 -38
  44. data/lib/coinbase/network.rb +0 -55
  45. data/lib/coinbase/transfer.rb +0 -153
  46. data/lib/coinbase.rb +0 -18
@@ -0,0 +1,659 @@
1
+ module Coinbase
2
+ module Wallet
3
+ class APIClient
4
+ def auth_headers(method, path, body)
5
+ raise NotImplementedError, "APIClient is not intended to be used directly"
6
+ end
7
+
8
+ #
9
+ # Market Data
10
+ #
11
+ def currencies(params = {})
12
+ out = nil
13
+ get("/v2/currencies", params) do |data, resp|
14
+ out = resp.data.map { |item| APIObject.new(self, item) }
15
+ yield(data, resp) if block_given?
16
+ end
17
+ out
18
+ end
19
+
20
+ def exchange_rates(params = {})
21
+ out = nil
22
+ get("/v2/exchange-rates", params) do |data, resp|
23
+ out = resp.data.map { |item| APIObject.new(self, item) }
24
+ yield(data, resp) if block_given?
25
+ end
26
+ out
27
+ end
28
+
29
+ def buy_price(params = {})
30
+ out = nil
31
+ get("/v2/prices/buy", params) do |data, resp|
32
+ out = APIObject.new(self, resp.data)
33
+ yield(data, resp) if block_given?
34
+ end
35
+ out
36
+ end
37
+
38
+ def sell_price(params = {})
39
+ out = nil
40
+ get("/v2/prices/sell", params) do |data, resp|
41
+ out = APIObject.new(self, resp.data)
42
+ yield(data, resp) if block_given?
43
+ end
44
+ out
45
+ end
46
+ def spot_price(params = {})
47
+ out = nil
48
+ get("/v2/prices/spot", params) do |data, resp|
49
+ out = APIObject.new(self, resp.data)
50
+ yield(data, resp) if block_given?
51
+ end
52
+ out
53
+ end
54
+
55
+ def time(params = {})
56
+ out = nil
57
+ get("/v2/time", params) do |data, resp|
58
+ out = APIObject.new(self, resp.data)
59
+ yield(data, resp) if block_given?
60
+ end
61
+ out
62
+ end
63
+
64
+ #
65
+ # Users
66
+ #
67
+ def user(user_id, params = {})
68
+ out = nil
69
+ get("/v2/users/#{user_id}", params) do |data, resp|
70
+ out = User.new(self, resp.data)
71
+ yield(out, resp) if block_given?
72
+ end
73
+ out
74
+ end
75
+
76
+ def current_user(params = {})
77
+ out = nil
78
+ get("/v2/user", params) do |data, resp|
79
+ out = CurrentUser.new(self, resp.data)
80
+ yield(out, resp) if block_given?
81
+ end
82
+ out
83
+ end
84
+
85
+ def auth_info(params = {})
86
+ out = nil
87
+ get("/v2/user/auth") do |data, resp|
88
+ out = APIObject.new(self, resp.data)
89
+ yield(out, resp) if block_given?
90
+ end
91
+ out
92
+ end
93
+
94
+ def update_current_user(params = {})
95
+ out = nil
96
+ put("/v2/user", params) do |data, resp|
97
+ out = CurrentUser.new(self, resp.data)
98
+ yield(out, resp) if block_given?
99
+ end
100
+ out
101
+ end
102
+
103
+ #
104
+ # Accounts
105
+ #
106
+ def accounts(params = {})
107
+ out = nil
108
+ get("/v2/accounts", params) do |data, resp|
109
+ out = data.map { |item| Account.new(self, item) }
110
+ yield(out, resp) if block_given?
111
+ end
112
+ out
113
+ end
114
+
115
+ def account(account_id, params = {})
116
+ out = nil
117
+ get("/v2/accounts/#{account_id}", params) do |data, resp|
118
+ out = Account.new(self, resp.data)
119
+ yield(out, resp) if block_given?
120
+ end
121
+ out
122
+ end
123
+
124
+ def primary_account(params = {})
125
+ out = nil
126
+ get("/v2/accounts/primary", params) do |data, resp|
127
+ out = Account.new(self, resp.data)
128
+ yield(out, resp) if block_given?
129
+ end
130
+ out
131
+ end
132
+
133
+ def set_primary_account(account_id, params = {})
134
+ out = nil
135
+ post("/v2/accounts/#{account_id}/primary", params) do |data, resp|
136
+ out = Account.new(self, resp.data)
137
+ yield(out, resp) if block_given?
138
+ end
139
+ out
140
+ end
141
+
142
+ def create_account(params = {})
143
+ out = nil
144
+ post("/v2/accounts", params) do |data, resp|
145
+ out = Account.new(self, resp.data)
146
+ yield(out, resp) if block_given?
147
+ end
148
+ out
149
+ end
150
+
151
+ def update_account(account_id, params = {})
152
+ out = nil
153
+ put("/v2/accounts/#{account_id}", params) do |data, resp|
154
+ out = Account.new(self, resp.data)
155
+ yield(out, resp) if block_given?
156
+ end
157
+ out
158
+ end
159
+
160
+ def delete_account(account_id, params = {})
161
+ out = nil
162
+ delete("/v2/accounts/#{account_id}", params) do |data, resp|
163
+ out = APIObject.new(self, resp.data)
164
+ yield(out, resp) if block_given?
165
+ end
166
+ out
167
+ end
168
+
169
+ #
170
+ # Addresses
171
+ #
172
+ def addresses(account_id, params = {})
173
+ out = nil
174
+ get("/v2/accounts/#{account_id}/addresses", params) do |data, resp|
175
+ out = resp.data.map { |item| APIObject.new(self, item) }
176
+ yield(data, resp) if block_given?
177
+ end
178
+ out
179
+ end
180
+
181
+ def address(account_id, address_id, params = {})
182
+ out = nil
183
+ get("/v2/accounts/#{account_id}/addresses/#{address_id}", params) do |data, resp|
184
+ out = APIObject.new(self, resp.data)
185
+ yield(out, resp) if block_given?
186
+ end
187
+ out
188
+ end
189
+
190
+ def create_address(account_id, params = {})
191
+ out = nil
192
+ post("/v2/accounts/#{account_id}/addresses", params) do |data, resp|
193
+ out = APIObject.new(self, resp.data)
194
+ yield(out, resp) if block_given?
195
+ end
196
+ out
197
+ end
198
+
199
+ #
200
+ # Transactions
201
+ #
202
+ def transactions(account_id, params = {})
203
+ out = nil
204
+ get("/v2/accounts/#{account_id}/transactions", params) do |data, resp|
205
+ out = resp.data.map { |item| Transaction.new(self, item) }
206
+ yield(data, resp) if block_given?
207
+ end
208
+ out
209
+ end
210
+
211
+ def transaction(account_id, transaction_id, params = {})
212
+ out = nil
213
+ get("/v2/accounts/#{account_id}/transactions/#{transaction_id}", params) do |data, resp|
214
+ out = Transaction.new(self, resp.data)
215
+ yield(out, resp) if block_given?
216
+ end
217
+ out
218
+ end
219
+
220
+ def send(account_id, params = {})
221
+ [ :to, :amount ].each do |param|
222
+ raise APIError, "Missing parameter: #{param}" unless params.include? param
223
+ end
224
+ params['type'] = 'send'
225
+
226
+ out = nil
227
+ post("/v2/accounts/#{account_id}/transactions", params) do |data, resp|
228
+ out = Transaction.new(self, resp.data)
229
+ yield(out, resp) if block_given?
230
+ end
231
+ out
232
+ end
233
+
234
+ def transfer(account_id, params = {})
235
+ [ :to, :amount ].each do |param|
236
+ raise APIError, "Missing parameter: #{param}" unless params.include? param
237
+ end
238
+ params['type'] = 'transfer'
239
+
240
+ out = nil
241
+ post("/v2/accounts/#{account_id}/transactions", params) do |data, resp|
242
+ out = Transaction.new(self, resp.data)
243
+ yield(out, resp) if block_given?
244
+ end
245
+ out
246
+ end
247
+
248
+ def request(account_id, params = {})
249
+ [ :to, :amount, :currency ].each do |param|
250
+ raise APIError, "Missing parameter: #{param}" unless params.include? param
251
+ end
252
+ params['type'] = 'request'
253
+
254
+ out = nil
255
+ post("/v2/accounts/#{account_id}/transactions", params) do |data, resp|
256
+ out = Request.new(self, resp.data)
257
+ yield(out, resp) if block_given?
258
+ end
259
+ out
260
+ end
261
+
262
+ def resend_request(account_id, transaction_id, params = {})
263
+ out = nil
264
+ post("/v2/accounts/#{account_id}/transactions/#{transaction_id}/resend", params) do |data, resp|
265
+ out = APIObject.new(self, resp.data)
266
+ yield(out, resp) if block_given?
267
+ end
268
+ out
269
+ end
270
+
271
+ def cancel_request(account_id, transaction_id, params = {})
272
+ out = nil
273
+ delete("/v2/accounts/#{account_id}/transactions/#{transaction_id}", params) do |data, resp|
274
+ out = APIObject.new(self, resp.data)
275
+ yield(out, resp) if block_given?
276
+ end
277
+ out
278
+ end
279
+
280
+ def complete_request(account_id, transaction_id, params = {})
281
+ out = nil
282
+ post("/v2/accounts/#{account_id}/transactions/#{transaction_id}/complete", params) do |data, resp|
283
+ out = APIObject.new(self, resp.data)
284
+ yield(out, resp) if block_given?
285
+ end
286
+ out
287
+ end
288
+
289
+ #
290
+ # Buys
291
+ #
292
+ def list_buys(account_id, pararms={})
293
+ out = nil
294
+ get("/v2/accounts/#{account_id}/buys") do |data, resp|
295
+ out = resp.data.map { |item| Transfer.new(self, item) }
296
+ yield(data, resp) if block_given?
297
+ end
298
+ out
299
+ end
300
+
301
+ def list_buy(account_id, transaction_id, params = {})
302
+ out = nil
303
+ get("/v2/accounts/#{account_id}/buys/#{transaction_id}") do |data, resp|
304
+ out = Transfer.new(self, resp.data)
305
+ yield(out, resp) if block_given?
306
+ end
307
+ out
308
+ end
309
+
310
+ def buy(account_id, params = {})
311
+ [ :amount ].each do |param|
312
+ raise APIError, "Missing parameter: #{param}" unless params.include? param
313
+ end
314
+
315
+ out = nil
316
+ post("/v2/accounts/#{account_id}/buys") do |data, resp|
317
+ out = Transfer.new(self, resp.data)
318
+ yield(out, resp) if block_given?
319
+ end
320
+ out
321
+ end
322
+
323
+ def commit_buy(account_id, transaction_id, params = {})
324
+ out = nil
325
+ post("/v2/accounts/#{account_id}/buys/#{transaction_id}/commit") do |data, resp|
326
+ out = Transfer.new(self, resp.data)
327
+ yield(out, resp) if block_given?
328
+ end
329
+ out
330
+ end
331
+
332
+ #
333
+ # Sells
334
+ #
335
+ def list_sells(account_id, params = {})
336
+ out = nil
337
+ get("/v2/accounts/#{account_id}/sells") do |data, resp|
338
+ out = resp.data.map { |item| Transfer.new(self, item) }
339
+ yield(data, resp) if block_given?
340
+ end
341
+ out
342
+ end
343
+
344
+ def list_sell(account_id, transaction_id, params = {})
345
+ out = nil
346
+ get("/v2/accounts/#{account_id}/sells/#{transaction_id}") do |data, resp|
347
+ out = Transfer.new(self, resp.data)
348
+ yield(out, resp) if block_given?
349
+ end
350
+ out
351
+ end
352
+
353
+ def sell(account_id, params = {})
354
+ [ :amount ].each do |param|
355
+ raise APIError, "Missing parameter: #{param}" unless params.include? param
356
+ end
357
+
358
+ out = nil
359
+ post("/v2/accounts/#{account_id}/sells") do |data, resp|
360
+ out = Transfer.new(self, resp.data)
361
+ yield(out, resp) if block_given?
362
+ end
363
+ out
364
+ end
365
+
366
+ def commit_sell(account_id, transaction_id, params = {})
367
+ out = nil
368
+ post("/v2/accounts/#{account_id}/sells/#{transaction_id}/commit") do |data, resp|
369
+ out = Transfer.new(self, resp.data)
370
+ yield(out, resp) if block_given?
371
+ end
372
+ out
373
+ end
374
+
375
+ #
376
+ # Deposits
377
+ #
378
+ def list_deposits(account_id, pararms={})
379
+ out = nil
380
+ get("/v2/accounts/#{account_id}/deposits") do |data, resp|
381
+ out = resp.data.map { |item| Transfer.new(self, item) }
382
+ yield(data, resp) if block_given?
383
+ end
384
+ out
385
+ end
386
+
387
+ def list_deposit(account_id, transaction_id, params = {})
388
+ out = nil
389
+ get("/v2/accounts/#{account_id}/deposits/#{transaction_id}") do |data, resp|
390
+ out = Transfer.new(self, resp.data)
391
+ yield(out, resp) if block_given?
392
+ end
393
+ out
394
+ end
395
+
396
+ def deposit(account_id, params = {})
397
+ [ :amount ].each do |param|
398
+ raise APIError, "Missing parameter: #{param}" unless params.include? param
399
+ end
400
+
401
+ out = nil
402
+ post("/v2/accounts/#{account_id}/deposits") do |data, resp|
403
+ out = Transfer.new(self, resp.data)
404
+ yield(out, resp) if block_given?
405
+ end
406
+ out
407
+ end
408
+
409
+ def commit_deposit(account_id, transaction_id, params = {})
410
+ out = nil
411
+ post("/v2/accounts/#{account_id}/deposits/#{transaction_id}/commit") do |data, resp|
412
+ out = APIObject.new(self, resp.data)
413
+ yield(out, resp) if block_given?
414
+ end
415
+ out
416
+ end
417
+
418
+ #
419
+ # withdrawals
420
+ #
421
+ def list_withdrawals(account_id, pararms={})
422
+ out = nil
423
+ get("/v2/accounts/#{account_id}/withdrawals") do |data, resp|
424
+ out = resp.data.map { |item| Transfer.new(self, item) }
425
+ yield(data, resp) if block_given?
426
+ end
427
+ out
428
+ end
429
+
430
+ def list_withdrawal(account_id, transaction_id, params = {})
431
+ out = nil
432
+ get("/v2/accounts/#{account_id}/withdrawals/#{transaction_id}") do |data, resp|
433
+ out = Transfer.new(self, resp.data)
434
+ yield(out, resp) if block_given?
435
+ end
436
+ out
437
+ end
438
+
439
+ def withdraw(account_id, params = {})
440
+ [ :amount ].each do |param|
441
+ raise APIError, "Missing parameter: #{param}" unless params.include? param
442
+ end
443
+
444
+ out = nil
445
+ post("/v2/accounts/#{account_id}/withdrawals") do |data, resp|
446
+ out = Transfer.new(self, resp.data)
447
+ yield(out, resp) if block_given?
448
+ end
449
+ out
450
+ end
451
+
452
+ def commit_withdrawal(account_id, transaction_id, params = {})
453
+ out = nil
454
+ post("/v2/accounts/#{account_id}/withdrawals/#{transaction_id}/commit") do |data, resp|
455
+ out = APIObject.new(self, resp.data)
456
+ yield(out, resp) if block_given?
457
+ end
458
+ out
459
+ end
460
+
461
+ #
462
+ # Payment Methods
463
+ #
464
+ def payment_methods(params = {})
465
+ out = nil
466
+ get("/v2/payment-methods", params) do |data, resp|
467
+ out = resp.data.map { |item| APIObject.new(self, item) }
468
+ yield(out, resp) if block_given?
469
+ end
470
+ out
471
+ end
472
+
473
+ def payment_method(payment_method_id, params = {})
474
+ out = nil
475
+ get("/v2/payment-methods/#{payment_method_id}", params) do |data, resp|
476
+ out = APIObject.new(self, resp.data)
477
+ yield(out, resp) if block_given?
478
+ end
479
+ out
480
+ end
481
+
482
+ #
483
+ # Merchants
484
+ #
485
+ def merchant(merchant_id, params = {})
486
+ out = nil
487
+ get("/v2/merchants/#{merchant_id}", params) do |data, resp|
488
+ out = APIObject.new(self, resp.data)
489
+ yield(out, resp) if block_given?
490
+ end
491
+ out
492
+ end
493
+
494
+ #
495
+ # Orders
496
+ #
497
+ def orders(params = {})
498
+ out = nil
499
+ get("/v2/orders", params) do |data, resp|
500
+ out = resp.data.map { |item| Order.new(self, item) }
501
+ yield(out, resp) if block_given?
502
+ end
503
+ out
504
+ end
505
+
506
+ def order(order_id, params = {})
507
+ out = nil
508
+ get("/v2/orders/#{order_id}", params) do |data, resp|
509
+ out = Order.new(self, resp.data)
510
+ yield(out, resp) if block_given?
511
+ end
512
+ out
513
+ end
514
+
515
+ def create_order(params = {})
516
+ [ :amount, :currency, :name ].each do |param|
517
+ fail APIError, "Missing parameter: #{param}" unless params.include? param
518
+ end
519
+
520
+ out = nil
521
+ post("/v2/orders") do |data, resp|
522
+ out = Order.new(self, resp.data)
523
+ yield(out, resp) if block_given?
524
+ end
525
+ out
526
+ end
527
+
528
+ def refund_order(order_id, params={})
529
+ [ :currency ].each do |param|
530
+ fail APIError, "Missing parameter: #{param}" unless params.include? param
531
+ end
532
+
533
+ out = nil
534
+ post("/v2/orders/#{order_id}/refund") do |data, resp|
535
+ out = Order.new(self, resp.data)
536
+ yield(out, resp) if block_given?
537
+ end
538
+ out
539
+ end
540
+
541
+ #
542
+ # Checkouts
543
+ #
544
+ def checkouts(params = {})
545
+ out = nil
546
+ get("/v2/checkouts", params) do |data, resp|
547
+ out = resp.data.map { |item| Checkout.new(self, item) }
548
+ yield(out, resp) if block_given?
549
+ end
550
+ out
551
+ end
552
+
553
+ def checkout(checkout_id, params = {})
554
+ out = nil
555
+ get("/v2/checkouts/#{checkout_id}", params) do |data, resp|
556
+ out = Checkout.new(self, resp.data)
557
+ yield(out, resp) if block_given?
558
+ end
559
+ out
560
+ end
561
+
562
+ def create_checkout(params = {})
563
+ [ :amount, :currency, :name ].each do |param|
564
+ fail APIError, "Missing parameter: #{param}" unless params.include? param
565
+ end
566
+
567
+ out = nil
568
+ post("/v2/checkouts", params) do |data, resp|
569
+ out = Checkout.new(self, resp.data)
570
+ yield(out, resp) if block_given?
571
+ end
572
+ out
573
+ end
574
+
575
+ def checkout_orders(checkout_id, params = {})
576
+ out = nil
577
+ get("/v2/checkouts/#{checkout_id}/orders", params) do |data, resp|
578
+ out = resp.data.map { |item| Order.new(self, item) }
579
+ yield(out, resp) if block_given?
580
+ end
581
+ out
582
+ end
583
+
584
+ def create_checkout_order(checkout_id, params={})
585
+ out = nil
586
+ post("/v2/checkouts/#{checkout_id}/orders", params) do |data, resp|
587
+ out = Order.new(self, resp.data)
588
+ yield(out, resp) if block_given?
589
+ end
590
+ out
591
+ end
592
+
593
+ #
594
+ # HTTP Stuff
595
+ #
596
+ def get(path, params = {})
597
+ uri = path
598
+ if params.count > 0
599
+ uri += "?#{URI.encode_www_form(params)}"
600
+ end
601
+
602
+ headers = {}
603
+ if params.has_key? :two_factor_token
604
+ headers['CB-2FA-TOKEN'] = params[:two_factor_token]
605
+ params.delete(:two_factor_token)
606
+ end
607
+
608
+ http_verb('GET', uri, nil, headers) do |resp|
609
+ if params[:fetch_all] == true &&
610
+ resp.body.has_key?('pagination') &&
611
+ resp.body['pagination']['next_uri'] != nil
612
+ params[:starting_after] = resp.body['data'].last['id']
613
+ get(path, params) do |p_data, p_resp|
614
+ yield(resp.body['data'] + p_data, resp)
615
+ end
616
+ else
617
+ yield(resp.body['data'], resp)
618
+ end
619
+ end
620
+ end
621
+
622
+ def put(path, params = {})
623
+ headers = {}
624
+ if params.has_key? :two_factor_token
625
+ headers['CB-2FA-TOKEN'] = params[:two_factor_token]
626
+ params.delete(:two_factor_token)
627
+ end
628
+
629
+ http_verb('PUT', path, params.to_json, headers) do |resp|
630
+ yield(resp.body['data'], resp)
631
+ end
632
+ end
633
+
634
+ def post(path, params = {})
635
+ headers = {}
636
+ if params.has_key? :two_factor_token
637
+ headers['CB-2FA-TOKEN'] = params[:two_factor_token]
638
+ params.delete(:two_factor_token)
639
+ end
640
+
641
+ http_verb('POST', path, params.to_json, headers) do |resp|
642
+ yield(resp.body['data'], resp)
643
+ end
644
+ end
645
+
646
+ def delete(path, params = {})
647
+ headers = {}
648
+ if params.has_key? :two_factor_token
649
+ headers['CB-2FA-TOKEN'] = params[:two_factor_token]
650
+ params.delete(:two_factor_token)
651
+ end
652
+
653
+ http_verb('DELETE', path, nil, headers) do |resp|
654
+ yield(resp.body['data'], resp)
655
+ end
656
+ end
657
+ end
658
+ end
659
+ end