coinbase 0.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of coinbase might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +330 -0
- data/Rakefile +6 -0
- data/coinbase.gemspec +33 -0
- data/lib/coinbase/ca-coinbase.crt +629 -0
- data/lib/coinbase/client.rb +290 -0
- data/lib/coinbase/oauth_client.rb +77 -0
- data/lib/coinbase/version.rb +3 -0
- data/lib/coinbase.rb +5 -18
- data/spec/client_spec.rb +358 -0
- data/spec/oauth_client_spec.rb +81 -0
- data/supported_currencies.json +163 -0
- metadata +71 -90
- data/lib/coinbase/address.rb +0 -127
- data/lib/coinbase/asset.rb +0 -20
- data/lib/coinbase/balance_map.rb +0 -48
- data/lib/coinbase/constants.rb +0 -38
- data/lib/coinbase/network.rb +0 -55
- data/lib/coinbase/transfer.rb +0 -153
- data/lib/coinbase/wallet.rb +0 -160
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,358 @@
|
|
1
|
+
require 'fakeweb'
|
2
|
+
require 'coinbase'
|
3
|
+
|
4
|
+
describe Coinbase::Client do
|
5
|
+
BASE_URI = 'http://fake.com/api/v1' # switching to http (instead of https) seems to help FakeWeb
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
@c = Coinbase::Client.new 'api key', 'api secret', {base_uri: BASE_URI}
|
9
|
+
FakeWeb.allow_net_connect = false
|
10
|
+
end
|
11
|
+
|
12
|
+
# Auth and Errors
|
13
|
+
|
14
|
+
it "raise errors" do
|
15
|
+
fake :get, '/account/balance', {error: "some error"}
|
16
|
+
expect{ @c.balance }.to raise_error(Coinbase::Client::Error, 'some error')
|
17
|
+
fake :get, '/account/balance', {errors: ["some", "error"]}
|
18
|
+
expect{ @c.balance }.to raise_error(Coinbase::Client::Error, 'some, error')
|
19
|
+
end
|
20
|
+
|
21
|
+
# Account
|
22
|
+
|
23
|
+
it "should get balance" do
|
24
|
+
fake :get, '/account/balance', {amount: "50.00000000", currency: 'BTC'}
|
25
|
+
@c.balance.should == 50.to_money('BTC')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should get a receive address" do
|
29
|
+
fake :get, '/account/receive_address', {address: "muVu2JZo8PbewBHRp6bpqFvVD87qvqEHWA", callback_url: nil}
|
30
|
+
a = @c.receive_address
|
31
|
+
a.address.should == "muVu2JZo8PbewBHRp6bpqFvVD87qvqEHWA"
|
32
|
+
a.callback_url.should == nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should generate new receive addresses" do
|
36
|
+
fake :post, '/account/generate_receive_address', {address: "mmxJyTdxHUJUDoptwLHAGxLEd1rAxDJ7EV", callback_url: "http://example.com/callback"}
|
37
|
+
a = @c.generate_receive_address
|
38
|
+
a.address.should == "mmxJyTdxHUJUDoptwLHAGxLEd1rAxDJ7EV"
|
39
|
+
a.callback_url.should == "http://example.com/callback"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should list accounts" do
|
43
|
+
accounts_response = <<-eos
|
44
|
+
{
|
45
|
+
"accounts": [
|
46
|
+
{
|
47
|
+
"id": "536a541fa9393bb3c7000023",
|
48
|
+
"name": "My Wallet",
|
49
|
+
"balance": {
|
50
|
+
"amount": "50.00000000",
|
51
|
+
"currency": "BTC"
|
52
|
+
},
|
53
|
+
"native_balance": {
|
54
|
+
"amount": "500.12",
|
55
|
+
"currency": "USD"
|
56
|
+
},
|
57
|
+
"created_at": "2014-05-07T08:41:19-07:00",
|
58
|
+
"primary": true,
|
59
|
+
"active": true
|
60
|
+
},
|
61
|
+
{
|
62
|
+
"id": "536a541fa9393bb3c7000034",
|
63
|
+
"name": "Savings",
|
64
|
+
"balance": {
|
65
|
+
"amount": "0.00000000",
|
66
|
+
"currency": "BTC"
|
67
|
+
},
|
68
|
+
"native_balance": {
|
69
|
+
"amount": "0.00",
|
70
|
+
"currency": "USD"
|
71
|
+
},
|
72
|
+
"created_at": "2014-05-07T08:50:10-07:00",
|
73
|
+
"primary": false,
|
74
|
+
"active": true
|
75
|
+
}
|
76
|
+
],
|
77
|
+
"total_count": 2,
|
78
|
+
"num_pages": 1,
|
79
|
+
"current_page": 1
|
80
|
+
}
|
81
|
+
eos
|
82
|
+
|
83
|
+
fake :get, '/accounts', JSON.parse(accounts_response)
|
84
|
+
r = @c.accounts
|
85
|
+
r.total_count.should == 2
|
86
|
+
primary_account = r.accounts.select { |acct| acct.primary }.first
|
87
|
+
primary_account.id.should == "536a541fa9393bb3c7000023"
|
88
|
+
primary_account.balance.should == 50.to_money("BTC")
|
89
|
+
|
90
|
+
# Make sure paging works
|
91
|
+
fake :get, '/accounts?page=2', JSON.parse(accounts_response)
|
92
|
+
r = @c.accounts :page => 2
|
93
|
+
FakeWeb.last_request.path.should include("page=2")
|
94
|
+
end
|
95
|
+
|
96
|
+
# Buttons
|
97
|
+
|
98
|
+
it "should create a new button" do
|
99
|
+
response = {:success=>true, :button=>{:code=>"93865b9cae83706ae59220c013bc0afd", :type=>"buy_now", :style=>"custom_large", :text=>"Pay With Bitcoin", :name=>"Order 123", :description=>"Sample description", :custom=>"Order123", :price=>{:cents=>123, :currency_iso=>"BTC"}}}
|
100
|
+
fake :post, '/buttons', response
|
101
|
+
r = @c.create_button "Order 123", 1.23, "Sample description"
|
102
|
+
|
103
|
+
# Ensure BTC is assumed to be the default currency
|
104
|
+
post_params = JSON.parse(FakeWeb.last_request.body)
|
105
|
+
post_params['button']['price_currency_iso'].should == "BTC"
|
106
|
+
post_params['button']['price_string'].should == "1.23000000"
|
107
|
+
|
108
|
+
r.success?.should == true
|
109
|
+
r.button.name.should == "Order 123"
|
110
|
+
r.embed_html.should == %[<div class="coinbase-button" data-code="93865b9cae83706ae59220c013bc0afd"></div><script src="https://coinbase.com/assets/button.js" type="text/javascript"></script>]
|
111
|
+
|
112
|
+
r = @c.create_button "Order 123", 1.23, "Sample description", nil, button_mode: 'page'
|
113
|
+
r.success?.should == true
|
114
|
+
r.button.name.should == "Order 123"
|
115
|
+
r.embed_html.should == %[<a href="https://coinbase.com/checkouts/93865b9cae83706ae59220c013bc0afd" target="_blank"><img alt="Pay With Bitcoin" src="https://coinbase.com/assets/buttons/custom_large.png"></a>]
|
116
|
+
|
117
|
+
r = @c.create_button "Order 123", 1.23, "Sample description", nil, button_mode: 'iframe'
|
118
|
+
r.success?.should == true
|
119
|
+
r.button.name.should == "Order 123"
|
120
|
+
r.embed_html.should == %[<iframe src="https://coinbase.com/inline_payments/93865b9cae83706ae59220c013bc0afd" style="width:500px;height:160px;border:none;box-shadow:0 1px 3px rgba(0,0,0,0.25);overflow:hidden;" scrolling="no" allowtransparency="true" frameborder="0"></iframe>]
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should create a new button with price in USD" do
|
124
|
+
response = {:success=>true, :button=>{:code=>"93865b9cae83706ae59220c013bc0afd", :type=>"buy_now", :style=>"custom_large", :text=>"Pay With Bitcoin", :name=>"Order 123", :description=>"Sample description", :custom=>"Order123", :price=>{:cents=>123, :currency_iso=>"USD"}}}
|
125
|
+
fake :post, '/buttons', response
|
126
|
+
r = @c.create_button "Order 123", 1.23.to_money("USD"), "Sample description"
|
127
|
+
|
128
|
+
post_params = JSON.parse(FakeWeb.last_request.body)
|
129
|
+
post_params['button']['price_currency_iso'].should == "USD"
|
130
|
+
post_params['button']['price_string'].should == "1.23"
|
131
|
+
|
132
|
+
r.success?.should == true
|
133
|
+
r.button.name.should == "Order 123"
|
134
|
+
r.embed_html.should == %[<div class="coinbase-button" data-code="93865b9cae83706ae59220c013bc0afd"></div><script src="https://coinbase.com/assets/button.js" type="text/javascript"></script>]
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should create order for the button" do
|
138
|
+
response = {"success"=>true, "order"=>{"id"=>"UAHXEK24", "created_at"=>"2013-12-13T01:15:56-08:00", "status"=>"new", "total_btc"=>{"cents"=>123, "currency_iso"=>"BTC"}, "total_native"=>{"cents"=>123, "currency_iso"=>"BTC"}, "custom"=>"Order123", "receive_address"=>"1EWxf61QGAkQDNUDq6XynH2PdFRyZUm111", "button"=>{"type"=>"buy_now", "name"=>"Order 123", "description"=>"Sample description", "id"=>"93865b9cae83706ae59220c013bc0afd"}, "transaction"=>nil}}
|
139
|
+
fake :post, '/buttons/93865b9cae83706ae59220c013bc0afd/create_order', response
|
140
|
+
r = @c.create_order_for_button "93865b9cae83706ae59220c013bc0afd"
|
141
|
+
r.order.button.id.should == "93865b9cae83706ae59220c013bc0afd"
|
142
|
+
r.order.status.should == "new"
|
143
|
+
end
|
144
|
+
|
145
|
+
# Transactions
|
146
|
+
|
147
|
+
it "should get transaction list" do
|
148
|
+
response = {"current_user"=>{"id"=>"5011f33df8182b142400000e", "email"=>"user2@example.com", "name"=>"user2@example.com"}, "balance"=>{"amount"=>"50.00000000", "currency"=>"BTC"}, "total_count"=>2, "num_pages"=>1, "current_page"=>1, "transactions"=>[{"transaction"=>{"id"=>"5018f833f8182b129c00002f", "created_at"=>"2012-08-01T02:34:43-07:00", "amount"=>{"amount"=>"-1.10000000", "currency"=>"BTC"}, "request"=>true, "status"=>"pending", "sender"=>{"id"=>"5011f33df8182b142400000e", "name"=>"User Two", "email"=>"user2@example.com"}, "recipient"=>{"id"=>"5011f33df8182b142400000a", "name"=>"User One", "email"=>"user1@example.com"}}}, {"transaction"=>{"id"=>"5018f833f8182b129c00002e", "created_at"=>"2012-08-01T02:36:43-07:00", "hsh" => "9d6a7d1112c3db9de5315b421a5153d71413f5f752aff75bf504b77df4e646a3", "amount"=>{"amount"=>"-1.00000000", "currency"=>"BTC"}, "request"=>false, "status"=>"complete", "sender"=>{"id"=>"5011f33df8182b142400000e", "name"=>"User Two", "email"=>"user2@example.com"}, "recipient_address"=>"37muSN5ZrukVTvyVh3mT5Zc5ew9L9CBare"}}]}
|
149
|
+
fake :get, '/transactions?page=1', response
|
150
|
+
r = @c.transactions
|
151
|
+
r.transactions.first.transaction.id.should == '5018f833f8182b129c00002f'
|
152
|
+
r.transactions.last.transaction.hsh.should == '9d6a7d1112c3db9de5315b421a5153d71413f5f752aff75bf504b77df4e646a3'
|
153
|
+
r.transactions.first.transaction.amount.should == "-1.1".to_money("BTC")
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should get transaction detail" do
|
157
|
+
response = {"transaction"=>{"id"=>"5011f33df8182b142400000e", "created_at"=>"2013-12-19T05:20:15-08:00", "hsh"=>"ff11a892bc6f7c345a5d74d52b0878f6a7e5011f33df8182b142400000e", "amount"=>{"amount"=>"-0.01000000", "currency"=>"BTC"}, "request"=>false, "status"=>"pending", "sender"=>{"id"=>"5011f33df8182b142400000e", "email"=>"tuser2@example.com", "name"=>"User Two"}, "recipient_address"=>"1EWxf61QGAkQDNUDq6XynH2PdFRyZUm111", "notes"=>""}}
|
158
|
+
fake :get, "/transactions/5011f33df8182b142400000e", response
|
159
|
+
r = @c.transaction "5011f33df8182b142400000e"
|
160
|
+
r.transaction.id.should == "5011f33df8182b142400000e"
|
161
|
+
r.transaction.status.should == "pending"
|
162
|
+
r.transaction.amount.should == "-0.01".to_money("BTC")
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should not fail if there are no transactions" do
|
166
|
+
response = {"current_user"=>{"id"=>"5011f33df8182b142400000e", "email"=>"user2@example.com", "name"=>"user2@example.com"}, "balance"=>{"amount"=>"0.00000000", "currency"=>"BTC"}, "total_count"=>0, "num_pages"=>0, "current_page"=>1}
|
167
|
+
fake :get, '/transactions?page=1', response
|
168
|
+
r = @c.transactions
|
169
|
+
r.transactions.should_not be_nil
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should send money in BTC" do
|
173
|
+
response = {"success"=>true, "transaction"=>{"id"=>"501a1791f8182b2071000087", "created_at"=>"2012-08-01T23:00:49-07:00", "notes"=>"Sample transaction for you!", "amount"=>{"amount"=>"-1.23400000", "currency"=>"BTC"}, "request"=>false, "status"=>"pending", "sender"=>{"id"=>"5011f33df8182b142400000e", "name"=>"User Two", "email"=>"user2@example.com"}, "recipient"=>{"id"=>"5011f33df8182b142400000a", "name"=>"User One", "email"=>"user1@example.com"}}}
|
174
|
+
fake :post, '/transactions/send_money', response
|
175
|
+
r = @c.send_money "user1@example.com", 1.2345, "Sample transaction for you"
|
176
|
+
|
177
|
+
# Ensure BTC is assumed to be the default currency
|
178
|
+
post_params = JSON.parse(FakeWeb.last_request.body)
|
179
|
+
post_params['transaction']['amount_currency_iso'].should == "BTC"
|
180
|
+
post_params['transaction']['amount_string'].should == "1.23450000"
|
181
|
+
|
182
|
+
r.success.should == true
|
183
|
+
r.transaction.id.should == '501a1791f8182b2071000087'
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should send money in USD" do
|
187
|
+
response = {"success"=>true, "transaction"=>{"id"=>"501a1791f8182b2071000087", "created_at"=>"2012-08-01T23:00:49-07:00", "notes"=>"Sample transaction for you!", "amount"=>{"amount"=>"-1.23400000", "currency"=>"BTC"}, "request"=>false, "status"=>"pending", "sender"=>{"id"=>"5011f33df8182b142400000e", "name"=>"User Two", "email"=>"user2@example.com"}, "recipient"=>{"id"=>"5011f33df8182b142400000a", "name"=>"User One", "email"=>"user1@example.com"}}}
|
188
|
+
fake :post, '/transactions/send_money', response
|
189
|
+
r = @c.send_money "user1@example.com", 500.to_money("USD"), "Sample transaction for you"
|
190
|
+
|
191
|
+
post_params = JSON.parse(FakeWeb.last_request.body)
|
192
|
+
post_params['transaction']['amount_currency_iso'].should == "USD"
|
193
|
+
post_params['transaction']['amount_string'].should == "500.00"
|
194
|
+
|
195
|
+
r.success.should == true
|
196
|
+
r.transaction.id.should == '501a1791f8182b2071000087'
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should request money" do
|
200
|
+
response = {"success"=>true, "transaction"=>{"id"=>"501a3554f8182b2754000003", "created_at"=>"2012-08-02T01:07:48-07:00", "notes"=>"Sample request for you!", "amount"=>{"amount"=>"1.23400000", "currency"=>"BTC"}, "request"=>true, "status"=>"pending", "sender"=>{"id"=>"5011f33df8182b142400000a", "name"=>"User One", "email"=>"user1@example.com"}, "recipient"=>{"id"=>"5011f33df8182b142400000e", "name"=>"User Two", "email"=>"user2@example.com"}}}
|
201
|
+
fake :post, '/transactions/request_money', response
|
202
|
+
r = @c.request_money "user1@example.com", 1.2345, "Sample transaction for you"
|
203
|
+
|
204
|
+
# Ensure BTC is assumed to be the default currency
|
205
|
+
post_params = JSON.parse(FakeWeb.last_request.body)
|
206
|
+
post_params['transaction']['amount_currency_iso'].should == "BTC"
|
207
|
+
post_params['transaction']['amount_string'].should == "1.23450000"
|
208
|
+
|
209
|
+
r.success.should == true
|
210
|
+
r.transaction.id.should == '501a3554f8182b2754000003'
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should request money in USD" do
|
214
|
+
response = {"success"=>true, "transaction"=>{"id"=>"501a3554f8182b2754000003", "created_at"=>"2012-08-02T01:07:48-07:00", "notes"=>"Sample request for you!", "amount"=>{"amount"=>"1.23400000", "currency"=>"BTC"}, "request"=>true, "status"=>"pending", "sender"=>{"id"=>"5011f33df8182b142400000a", "name"=>"User One", "email"=>"user1@example.com"}, "recipient"=>{"id"=>"5011f33df8182b142400000e", "name"=>"User Two", "email"=>"user2@example.com"}}}
|
215
|
+
fake :post, '/transactions/request_money', response
|
216
|
+
r = @c.request_money "user1@example.com", 500.to_money("USD"), "Sample transaction for you"
|
217
|
+
|
218
|
+
# Ensure BTC is assumed to be the default currency
|
219
|
+
post_params = JSON.parse(FakeWeb.last_request.body)
|
220
|
+
post_params['transaction']['amount_currency_iso'].should == "USD"
|
221
|
+
post_params['transaction']['amount_string'].should == "500.00"
|
222
|
+
|
223
|
+
r.success.should == true
|
224
|
+
r.transaction.id.should == '501a3554f8182b2754000003'
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should resend requests" do
|
228
|
+
response = {"success"=>true}
|
229
|
+
fake :put, "/transactions/501a3554f8182b2754000003/resend_request", response
|
230
|
+
r = @c.resend_request '501a3554f8182b2754000003'
|
231
|
+
r.success.should == true
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should cancel requests" do
|
235
|
+
response = {"success"=>true}
|
236
|
+
fake :delete, "/transactions/501a3554f8182b2754000003/cancel_request", response
|
237
|
+
r = @c.cancel_request '501a3554f8182b2754000003'
|
238
|
+
r.success.should == true
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should resend requests" do
|
242
|
+
response = {"success"=>true}
|
243
|
+
fake :put, "/transactions/501a3554f8182b2754000003/complete_request", response
|
244
|
+
r = @c.complete_request '501a3554f8182b2754000003'
|
245
|
+
r.success.should == true
|
246
|
+
end
|
247
|
+
|
248
|
+
# Users
|
249
|
+
|
250
|
+
it "should let you create users" do
|
251
|
+
response = {"success"=>true, "user"=>{"id"=>"501a3d22f8182b2754000011", "name"=>"New User", "email"=>"newuser@example.com", "receive_address"=>"mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"}}
|
252
|
+
fake :post, "/users", response
|
253
|
+
r = @c.create_user "newuser@example.com"
|
254
|
+
r.success.should == true
|
255
|
+
r.user.email.should == "newuser@example.com"
|
256
|
+
r.user.receive_address.should == "mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
|
257
|
+
end
|
258
|
+
|
259
|
+
it "should let you create users with OAuth" do
|
260
|
+
response = {
|
261
|
+
"success"=>true,
|
262
|
+
"oauth" => {
|
263
|
+
"access_token" => "the_access_token",
|
264
|
+
"refresh_token" => "the_refresh_token",
|
265
|
+
"scope" => "transactions buy sell",
|
266
|
+
"token_type" => "bearer"
|
267
|
+
},
|
268
|
+
"user" => {
|
269
|
+
"id"=>"501a3d22f8182b2754000011",
|
270
|
+
"name"=>"New User",
|
271
|
+
"email"=>"newuser@example.com",
|
272
|
+
"receive_address"=>"mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
|
273
|
+
}
|
274
|
+
}
|
275
|
+
fake :post, "/users", response
|
276
|
+
r = @c.create_user "newuser@example.com", "newpassword", "the_client_id", ['transactions', 'buy', 'sell']
|
277
|
+
r.success.should == true
|
278
|
+
r.oauth.access_token.should == "the_access_token"
|
279
|
+
r.user.email.should == "newuser@example.com"
|
280
|
+
r.user.receive_address.should == "mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should not let you specify client_id without scopes" do
|
284
|
+
expect{ @c.create_user "newuser@example.com", "newpassword", "the_client_id" }.to raise_error(Coinbase::Client::Error)
|
285
|
+
end
|
286
|
+
|
287
|
+
# Prices
|
288
|
+
|
289
|
+
it "should let you get buy, sell, and spot prices" do
|
290
|
+
fake :get, "/prices/buy?qty=1", {"amount"=>"13.85", "currency"=>"USD"}
|
291
|
+
r = @c.buy_price 1
|
292
|
+
r.to_f.should == 13.85
|
293
|
+
|
294
|
+
fake :get, "/prices/sell?qty=1", {"amount"=>"13.83", "currency"=>"USD"}
|
295
|
+
r = @c.sell_price 1
|
296
|
+
r.to_f.should == 13.83
|
297
|
+
|
298
|
+
fake :get, "/prices/spot_rate?currency=USD", {"amount"=>"13.84", "currency"=>"USD"}
|
299
|
+
r = @c.spot_price
|
300
|
+
r.to_f.should == 13.84
|
301
|
+
end
|
302
|
+
|
303
|
+
# Buys
|
304
|
+
|
305
|
+
it "should let you buy bitcoin" do
|
306
|
+
response = {"success"=>true, "transfer"=>{"_type"=>"AchDebit", "code"=>"6H7GYLXZ", "created_at"=>"2013-01-28T16:08:58-08:00", "fees"=>{"coinbase"=>{"cents"=>14, "currency_iso"=>"USD"}, "bank"=>{"cents"=>15, "currency_iso"=>"USD"}}, "status"=>"created", "payout_date"=>"2013-02-01T18:00:00-08:00", "btc"=>{"amount"=>"1.00000000", "currency"=>"BTC"}, "subtotal"=>{"amount"=>"13.55", "currency"=>"USD"}, "total"=>{"amount"=>"13.84", "currency"=>"USD"}}}
|
307
|
+
fake :post, "/buys", response
|
308
|
+
r = @c.buy! 1
|
309
|
+
r.success?.should == true
|
310
|
+
r.transfer.code.should == '6H7GYLXZ'
|
311
|
+
r.transfer.status.should == 'created'
|
312
|
+
r.transfer.btc.should == 1.to_money("BTC")
|
313
|
+
end
|
314
|
+
|
315
|
+
# Sells
|
316
|
+
|
317
|
+
it "should let you sell bitcoin" do
|
318
|
+
response = {"success"=>true, "transfer"=>{"_type"=>"AchCredit", "code"=>"RD2OC8AL", "created_at"=>"2013-01-28T16:32:35-08:00", "fees"=>{"coinbase"=>{"cents"=>14, "currency_iso"=>"USD"}, "bank"=>{"cents"=>15, "currency_iso"=>"USD"}}, "status"=>"created", "payout_date"=>"2013-02-01T18:00:00-08:00", "btc"=>{"amount"=>"1.00000000", "currency"=>"BTC"}, "subtotal"=>{"amount"=>"13.50", "currency"=>"USD"}, "total"=>{"amount"=>"13.21", "currency"=>"USD"}}}
|
319
|
+
fake :post, "/sells", response
|
320
|
+
r = @c.sell! 1
|
321
|
+
r.success?.should == true
|
322
|
+
r.transfer.code.should == 'RD2OC8AL'
|
323
|
+
r.transfer.status.should == 'created'
|
324
|
+
r.transfer.btc.should == 1.to_money("BTC")
|
325
|
+
end
|
326
|
+
|
327
|
+
# Transfers
|
328
|
+
|
329
|
+
it "should get recent transfers" do
|
330
|
+
response = {"transfers" => [{"transfer" => {"type" => "Buy", "code" => "QPCUCZHR", "created_at" => "2013-02-27T23:28:18-08:00", "fees" => {"coinbase" => {"cents" => 14, "currency_iso" => "USD"}, "bank" => {"cents" => 15, "currency_iso" => "USD"} }, "payout_date" => "2013-03-05T18:00:00-08:00", "transaction_id" => "5011f33df8182b142400000e", "status" => "Pending", "btc" => {"amount" => "1.00000000", "currency" => "BTC"}, "subtotal" => {"amount" => "13.55", "currency" => "USD"}, "total" => {"amount" => "13.84", "currency" => "USD"}, "description" => "Paid for with $13.84 from Test xxxxx3111."} } ], "total_count" => 1, "num_pages" => 1, "current_page" => 1 }
|
331
|
+
fake :get, "/transfers", response
|
332
|
+
r = @c.transfers
|
333
|
+
t = r.transfers.first.transfer
|
334
|
+
t.type.should == "Buy"
|
335
|
+
t.code.should == "QPCUCZHR"
|
336
|
+
t.status.should == "Pending"
|
337
|
+
t.btc.should == 1.to_money("BTC")
|
338
|
+
end
|
339
|
+
|
340
|
+
it "should support pagination" do
|
341
|
+
response = {"transfers" => [{"transfer" => {"type" => "Buy", "code" => "QPCUCZHZ", "created_at" => "2013-02-27T23:28:18-08:00", "fees" => {"coinbase" => {"cents" => 14, "currency_iso" => "USD"}, "bank" => {"cents" => 15, "currency_iso" => "USD"} }, "payout_date" => "2013-03-05T18:00:00-08:00", "transaction_id" => "5011f33df8182b142400000e", "status" => "Pending", "btc" => {"amount" => "1.00000000", "currency" => "BTC"}, "subtotal" => {"amount" => "13.55", "currency" => "USD"}, "total" => {"amount" => "13.84", "currency" => "USD"}, "description" => "Paid for with $13.84 from Test xxxxx3111."} } ], "total_count" => 1, "num_pages" => 1, "current_page" => 1 }
|
342
|
+
fake :get, "/transfers?page=2", response
|
343
|
+
r = @c.transfers :page => 2
|
344
|
+
t = r.transfers.first.transfer
|
345
|
+
t.type.should == "Buy"
|
346
|
+
t.code.should == "QPCUCZHZ"
|
347
|
+
t.status.should == "Pending"
|
348
|
+
t.btc.should == 1.to_money("BTC")
|
349
|
+
FakeWeb.last_request.path.should include("page=2")
|
350
|
+
end
|
351
|
+
|
352
|
+
private
|
353
|
+
|
354
|
+
def fake method, path, body
|
355
|
+
FakeWeb.register_uri(method, "#{BASE_URI}#{path}", body: body.to_json)
|
356
|
+
end
|
357
|
+
|
358
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'fakeweb'
|
2
|
+
require 'coinbase'
|
3
|
+
|
4
|
+
describe Coinbase::Client do
|
5
|
+
BASE_URI = 'http://fake.com/api/v1' # switching to http (instead of https) seems to help FakeWeb
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
@credentials = {
|
9
|
+
:token => 'access_token',
|
10
|
+
:refresh_token => 'refresh_token',
|
11
|
+
:expires_at => Time.now.to_i + 100
|
12
|
+
}
|
13
|
+
@client_options = {
|
14
|
+
base_uri: BASE_URI,
|
15
|
+
authorize_url: "http://fake.com/oauth/authorize",
|
16
|
+
token_url: "http://fake.com/oauth/token"
|
17
|
+
}
|
18
|
+
@c = Coinbase::OAuthClient.new 'api key', 'api secret', @credentials, @client_options
|
19
|
+
FakeWeb.allow_net_connect = false
|
20
|
+
end
|
21
|
+
|
22
|
+
# Auth and Errors
|
23
|
+
|
24
|
+
it "raise errors" do
|
25
|
+
fake :get, '/account/balance', {error: "some error"}
|
26
|
+
expect{ @c.balance }.to raise_error(Coinbase::Client::Error, 'some error')
|
27
|
+
fake :get, '/account/balance', {errors: ["some", "error"]}
|
28
|
+
expect{ @c.balance }.to raise_error(Coinbase::Client::Error, 'some, error')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should get balance" do
|
32
|
+
fake :get, '/account/balance', {amount: "50.00000000", currency: 'BTC'}
|
33
|
+
@c.balance.should == 50.to_money('BTC')
|
34
|
+
|
35
|
+
# Ensure we're passing the access token
|
36
|
+
FakeWeb.last_request['Authorization'].should == 'Bearer access_token'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should support pagination" do
|
40
|
+
response = {"transfers" => [{"transfer" => {"type" => "Buy", "code" => "QPCUCZHL", "created_at" => "2013-02-27T23:28:18-08:00", "fees" => {"coinbase" => {"cents" => 14, "currency_iso" => "USD"}, "bank" => {"cents" => 15, "currency_iso" => "USD"} }, "payout_date" => "2013-03-05T18:00:00-08:00", "transaction_id" => "5011f33df8182b142400000e", "status" => "Pending", "btc" => {"amount" => "1.00000000", "currency" => "BTC"}, "subtotal" => {"amount" => "13.55", "currency" => "USD"}, "total" => {"amount" => "13.84", "currency" => "USD"}, "description" => "Paid for with $13.84 from Test xxxxx3111."} } ], "total_count" => 1, "num_pages" => 1, "current_page" => 1 }
|
41
|
+
fake :get, "/transfers?page=3", response
|
42
|
+
r = @c.transfers :page => 3
|
43
|
+
t = r.transfers.first.transfer
|
44
|
+
t.type.should == "Buy"
|
45
|
+
t.code.should == "QPCUCZHL"
|
46
|
+
t.status.should == "Pending"
|
47
|
+
t.btc.should == 1.to_money("BTC")
|
48
|
+
FakeWeb.last_request.path.should include("page=3")
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should refresh an expired token" do
|
52
|
+
credentials = @credentials.dup
|
53
|
+
credentials[:expires_at] = Time.now.to_i - 100
|
54
|
+
c = Coinbase::OAuthClient.new 'api key', 'api secret', credentials, @client_options
|
55
|
+
|
56
|
+
|
57
|
+
token_refresh_response = {
|
58
|
+
:access_token => "new_access_token",
|
59
|
+
:refresh_token => "new_refresh_token",
|
60
|
+
:token_type => "bearer",
|
61
|
+
:expires_in => 7200,
|
62
|
+
}
|
63
|
+
FakeWeb.register_uri(:post, @client_options[:token_url], body: token_refresh_response.to_json, :content_type => "application/json")
|
64
|
+
|
65
|
+
fake :get, '/account/balance', {amount: "50.00000000", currency: 'BTC'}
|
66
|
+
c.balance.should == 50.to_money('BTC')
|
67
|
+
|
68
|
+
# Ensure we're passing the new access token
|
69
|
+
FakeWeb.last_request['Authorization'].should == 'Bearer new_access_token'
|
70
|
+
|
71
|
+
# Make sure we can retrieve the new credentials for persistance after a refresh
|
72
|
+
c.credentials[:refresh_token].should == "new_refresh_token"
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def fake method, path, body
|
78
|
+
FakeWeb.register_uri(method, "#{BASE_URI}#{path}", body: body.to_json)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
{
|
2
|
+
"AED": "United Arab Emirates Dirham",
|
3
|
+
"AFN": "Afghan Afghani",
|
4
|
+
"ALL": "Albanian Lek",
|
5
|
+
"AMD": "Armenian Dram",
|
6
|
+
"ANG": "Netherlands Antillean Guilder",
|
7
|
+
"AOA": "Angolan Kwanza",
|
8
|
+
"ARS": "Argentine Peso",
|
9
|
+
"AUD": "Australian Dollar",
|
10
|
+
"AWG": "Aruban Florin",
|
11
|
+
"AZN": "Azerbaijani Manat",
|
12
|
+
"BAM": "Bosnia-Herzegovina Convertible Mark",
|
13
|
+
"BBD": "Barbadian Dollar",
|
14
|
+
"BDT": "Bangladeshi Taka",
|
15
|
+
"BGN": "Bulgarian Lev",
|
16
|
+
"BHD": "Bahraini Dinar",
|
17
|
+
"BIF": "Burundian Franc",
|
18
|
+
"BMD": "Bermudan Dollar",
|
19
|
+
"BND": "Brunei Dollar",
|
20
|
+
"BOB": "Bolivian Boliviano",
|
21
|
+
"BRL": "Brazilian Real",
|
22
|
+
"BSD": "Bahamian Dollar",
|
23
|
+
"BTC": "Bitcoin",
|
24
|
+
"BTN": "Bhutanese Ngultrum",
|
25
|
+
"BWP": "Botswanan Pula",
|
26
|
+
"BYR": "Belarusian Ruble",
|
27
|
+
"BZD": "Belize Dollar",
|
28
|
+
"CAD": "Canadian Dollar",
|
29
|
+
"CDF": "Congolese Franc",
|
30
|
+
"CHF": "Swiss Franc",
|
31
|
+
"CLF": "Chilean Unit of Account (UF)",
|
32
|
+
"CLP": "Chilean Peso",
|
33
|
+
"CNY": "Chinese Yuan",
|
34
|
+
"COP": "Colombian Peso",
|
35
|
+
"CRC": "Costa Rican Colón",
|
36
|
+
"CUP": "Cuban Peso",
|
37
|
+
"CVE": "Cape Verdean Escudo",
|
38
|
+
"CZK": "Czech Republic Koruna",
|
39
|
+
"DJF": "Djiboutian Franc",
|
40
|
+
"DKK": "Danish Krone",
|
41
|
+
"DOP": "Dominican Peso",
|
42
|
+
"DZD": "Algerian Dinar",
|
43
|
+
"EEK": "Estonian Kroon",
|
44
|
+
"EGP": "Egyptian Pound",
|
45
|
+
"ETB": "Ethiopian Birr",
|
46
|
+
"EUR": "Euro",
|
47
|
+
"FJD": "Fijian Dollar",
|
48
|
+
"FKP": "Falkland Islands Pound",
|
49
|
+
"GBP": "British Pound Sterling",
|
50
|
+
"GEL": "Georgian Lari",
|
51
|
+
"GHS": "Ghanaian Cedi",
|
52
|
+
"GIP": "Gibraltar Pound",
|
53
|
+
"GMD": "Gambian Dalasi",
|
54
|
+
"GNF": "Guinean Franc",
|
55
|
+
"GTQ": "Guatemalan Quetzal",
|
56
|
+
"GYD": "Guyanaese Dollar",
|
57
|
+
"HKD": "Hong Kong Dollar",
|
58
|
+
"HNL": "Honduran Lempira",
|
59
|
+
"HRK": "Croatian Kuna",
|
60
|
+
"HTG": "Haitian Gourde",
|
61
|
+
"HUF": "Hungarian Forint",
|
62
|
+
"IDR": "Indonesian Rupiah",
|
63
|
+
"ILS": "Israeli New Sheqel",
|
64
|
+
"INR": "Indian Rupee",
|
65
|
+
"IQD": "Iraqi Dinar",
|
66
|
+
"IRR": "Iranian Rial",
|
67
|
+
"ISK": "Icelandic Króna",
|
68
|
+
"JEP": "Jersey Pound",
|
69
|
+
"JMD": "Jamaican Dollar",
|
70
|
+
"JOD": "Jordanian Dinar",
|
71
|
+
"JPY": "Japanese Yen",
|
72
|
+
"KES": "Kenyan Shilling",
|
73
|
+
"KGS": "Kyrgystani Som",
|
74
|
+
"KHR": "Cambodian Riel",
|
75
|
+
"KMF": "Comorian Franc",
|
76
|
+
"KPW": "North Korean Won",
|
77
|
+
"KRW": "South Korean Won",
|
78
|
+
"KWD": "Kuwaiti Dinar",
|
79
|
+
"KYD": "Cayman Islands Dollar",
|
80
|
+
"KZT": "Kazakhstani Tenge",
|
81
|
+
"LAK": "Laotian Kip",
|
82
|
+
"LBP": "Lebanese Pound",
|
83
|
+
"LKR": "Sri Lankan Rupee",
|
84
|
+
"LRD": "Liberian Dollar",
|
85
|
+
"LSL": "Lesotho Loti",
|
86
|
+
"LTL": "Lithuanian Litas",
|
87
|
+
"LVL": "Latvian Lats",
|
88
|
+
"LYD": "Libyan Dinar",
|
89
|
+
"MAD": "Moroccan Dirham",
|
90
|
+
"MDL": "Moldovan Leu",
|
91
|
+
"MGA": "Malagasy Ariary",
|
92
|
+
"MKD": "Macedonian Denar",
|
93
|
+
"MMK": "Myanma Kyat",
|
94
|
+
"MNT": "Mongolian Tugrik",
|
95
|
+
"MOP": "Macanese Pataca",
|
96
|
+
"MRO": "Mauritanian Ouguiya",
|
97
|
+
"MUR": "Mauritian Rupee",
|
98
|
+
"MVR": "Maldivian Rufiyaa",
|
99
|
+
"MWK": "Malawian Kwacha",
|
100
|
+
"MXN": "Mexican Peso",
|
101
|
+
"MYR": "Malaysian Ringgit",
|
102
|
+
"MZN": "Mozambican Metical",
|
103
|
+
"NAD": "Namibian Dollar",
|
104
|
+
"NGN": "Nigerian Naira",
|
105
|
+
"NIO": "Nicaraguan Córdoba",
|
106
|
+
"NOK": "Norwegian Krone",
|
107
|
+
"NPR": "Nepalese Rupee",
|
108
|
+
"NZD": "New Zealand Dollar",
|
109
|
+
"OMR": "Omani Rial",
|
110
|
+
"PAB": "Panamanian Balboa",
|
111
|
+
"PEN": "Peruvian Nuevo Sol",
|
112
|
+
"PGK": "Papua New Guinean Kina",
|
113
|
+
"PHP": "Philippine Peso",
|
114
|
+
"PKR": "Pakistani Rupee",
|
115
|
+
"PLN": "Polish Zloty",
|
116
|
+
"PYG": "Paraguayan Guarani",
|
117
|
+
"QAR": "Qatari Rial",
|
118
|
+
"RON": "Romanian Leu",
|
119
|
+
"RSD": "Serbian Dinar",
|
120
|
+
"RUB": "Russian Ruble",
|
121
|
+
"RWF": "Rwandan Franc",
|
122
|
+
"SAR": "Saudi Riyal",
|
123
|
+
"SBD": "Solomon Islands Dollar",
|
124
|
+
"SCR": "Seychellois Rupee",
|
125
|
+
"SDG": "Sudanese Pound",
|
126
|
+
"SEK": "Swedish Krona",
|
127
|
+
"SGD": "Singapore Dollar",
|
128
|
+
"SHP": "Saint Helena Pound",
|
129
|
+
"SLL": "Sierra Leonean Leone",
|
130
|
+
"SOS": "Somali Shilling",
|
131
|
+
"SRD": "Surinamese Dollar",
|
132
|
+
"STD": "São Tomé and Príncipe Dobra",
|
133
|
+
"SVC": "Salvadoran Colón",
|
134
|
+
"SYP": "Syrian Pound",
|
135
|
+
"SZL": "Swazi Lilangeni",
|
136
|
+
"THB": "Thai Baht",
|
137
|
+
"TJS": "Tajikistani Somoni",
|
138
|
+
"TMT": "Turkmenistani Manat",
|
139
|
+
"TND": "Tunisian Dinar",
|
140
|
+
"TOP": "Tongan Paʻanga",
|
141
|
+
"TRY": "Turkish Lira",
|
142
|
+
"TTD": "Trinidad and Tobago Dollar",
|
143
|
+
"TWD": "New Taiwan Dollar",
|
144
|
+
"TZS": "Tanzanian Shilling",
|
145
|
+
"UAH": "Ukrainian Hryvnia",
|
146
|
+
"UGX": "Ugandan Shilling",
|
147
|
+
"USD": "United States Dollar",
|
148
|
+
"UYU": "Uruguayan Peso",
|
149
|
+
"UZS": "Uzbekistan Som",
|
150
|
+
"VEF": "Venezuelan Bolívar",
|
151
|
+
"VND": "Vietnamese Dong",
|
152
|
+
"VUV": "Vanuatu Vatu",
|
153
|
+
"WST": "Samoan Tala",
|
154
|
+
"XAF": "CFA Franc BEAC",
|
155
|
+
"XCD": "East Caribbean Dollar",
|
156
|
+
"XDR": "Special Drawing Rights",
|
157
|
+
"XOF": "CFA Franc BCEAO",
|
158
|
+
"XPF": "CFP Franc",
|
159
|
+
"YER": "Yemeni Rial",
|
160
|
+
"ZAR": "South African Rand",
|
161
|
+
"ZMK": "Zambian Kwacha",
|
162
|
+
"ZWL": "Zimbabwean Dollar"
|
163
|
+
}
|