mt_gox 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ {"usds":64.59,"btcs":9,"status":"Your funds are on their way..."}
data/spec/helper.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+ require 'base64'
4
+ require 'mtgox'
5
+ require 'rspec'
6
+ require 'webmock/rspec'
7
+
8
+ def a_get(path)
9
+ a_request(:get, 'https://mtgox.com' + path)
10
+ end
11
+
12
+ def stub_get(path)
13
+ stub_request(:get, 'https://mtgox.com' + path)
14
+ end
15
+
16
+ def a_post(path)
17
+ a_request(:post, 'https://mtgox.com' + path)
18
+ end
19
+
20
+ def stub_post(path)
21
+ stub_request(:post, 'https://mtgox.com' + path)
22
+ end
23
+
24
+ def fixture_path
25
+ File.expand_path('../fixtures', __FILE__)
26
+ end
27
+
28
+ def fixture(file)
29
+ File.new(fixture_path + '/' + file)
30
+ end
31
+
32
+ module MtGox
33
+ module Request
34
+ private
35
+ def add_nonce(options)
36
+ options.merge!({:nonce => 1321745961249676})
37
+ end
38
+ end
39
+ end
40
+
41
+ def test_headers(body=test_body)
42
+ signed_headers(body).merge!(
43
+ {
44
+ 'Accept' => 'application/json',
45
+ 'Content-Type' => 'application/x-www-form-urlencoded',
46
+ 'User-Agent' => "mt_gox gem #{MtGox::VERSION}",
47
+ }
48
+ )
49
+ end
50
+
51
+ def signed_headers(body)
52
+ signature = Base64.strict_encode64(
53
+ OpenSSL::HMAC.digest 'sha512',
54
+ Base64.decode64(MtGox.secret),
55
+ body
56
+ )
57
+ {'Rest-Key' => MtGox.key, 'Rest-Sign' => signature}
58
+ end
59
+
60
+ def test_body(options={})
61
+ options.merge!({:nonce => 1321745961249676}).collect{|k, v| "#{k}=#{v}"} * '&'
62
+ end
@@ -0,0 +1,315 @@
1
+ require 'helper'
2
+
3
+ describe MtGox::Client do
4
+ before do
5
+ @client = MtGox::Client.new
6
+ MtGox.configure do |config|
7
+ config.key = "key"
8
+ config.secret = "secret"
9
+ end
10
+ end
11
+
12
+ describe '#address' do
13
+ before do
14
+ stub_post('/api/0/btcAddress.php').
15
+ to_return(:status => 200, :body => fixture('address.json'))
16
+ end
17
+
18
+ it "should fetch a deposit address" do
19
+ address = @client.address
20
+ a_post('/api/0/btcAddress.php').
21
+ should have_been_made
22
+ address.should == '171dH9Uum6wWLSwH2g8g2yto6SG7NEGyXG'
23
+ end
24
+ end
25
+
26
+ describe '#ticker' do
27
+ before do
28
+ stub_get('/api/0/data/ticker.php').
29
+ to_return(:status => 200, :body => fixture('ticker.json'))
30
+ end
31
+
32
+ it "should fetch the ticker" do
33
+ ticker = @client.ticker
34
+ a_get('/api/0/data/ticker.php').
35
+ should have_been_made
36
+ ticker.buy.should == 26.4
37
+ ticker.sell.should == 26.6099
38
+ ticker.high.should == 28.678
39
+ ticker.low.should == 18.4
40
+ ticker.price.should == 26.5
41
+ ticker.volume.should == 80531.0
42
+ ticker.vwap.should == 3.81561
43
+ end
44
+ end
45
+
46
+ describe 'depth methods' do
47
+ before :each do
48
+ stub_get('/api/0/data/getDepth.php').
49
+ to_return(:status => 200, :body => fixture('depth.json'))
50
+ end
51
+
52
+ describe '#asks' do
53
+ it "should fetch open asks" do
54
+ asks = @client.asks
55
+ a_get('/api/0/data/getDepth.php').
56
+ should have_been_made
57
+ asks.last.price.should == 23.75
58
+ asks.last.eprice.should == 23.905385002516354
59
+ asks.last.amount.should == 50
60
+ end
61
+
62
+ it "should be sorted in price-ascending order" do
63
+ asks = @client.asks
64
+ asks.sort_by{|ask| ask.price}.should == asks
65
+ end
66
+
67
+ end
68
+
69
+ describe "#bids" do
70
+ it "should fetch open bids" do
71
+ bids = @client.bids
72
+ a_get('/api/0/data/getDepth.php').
73
+ should have_been_made
74
+ bids.last.price.should == 14.62101
75
+ bids.last.eprice.should == 14.525973435000001
76
+ bids.last.amount.should == 5
77
+ end
78
+
79
+ it "should be sorted in price-descending order" do
80
+ bids = @client.bids
81
+ bids.sort_by{|bid| bid.price}.reverse.should == bids
82
+ end
83
+ end
84
+
85
+ describe "#offers" do
86
+ it "should fetch both bids and asks, with only one call" do
87
+ offers = @client.offers
88
+ a_get('/api/0/data/getDepth.php').
89
+ should have_been_made.once
90
+ offers[:asks].last.price.should == 23.75
91
+ offers[:asks].last.eprice.should == 23.905385002516354
92
+ offers[:asks].last.amount.should == 50
93
+ offers[:bids].last.price.should == 14.62101
94
+ offers[:bids].last.eprice.should == 14.525973435000001
95
+ offers[:bids].last.amount.should == 5
96
+ end
97
+ end
98
+
99
+ describe '#min_ask' do
100
+ it "should fetch the lowest priced ask" do
101
+ min_ask = @client.min_ask
102
+ a_get('/api/0/data/getDepth.php').
103
+ should have_been_made.once
104
+ min_ask.price.should == 17.00009
105
+ min_ask.eprice.should == 17.11131353799698
106
+ min_ask.amount.should == 36.22894353
107
+ end
108
+ end
109
+
110
+ describe '#max_bid' do
111
+ it "should fetch the highest priced bid" do
112
+ max_bid = @client.max_bid
113
+ a_get('/api/0/data/getDepth.php').
114
+ should have_been_made.once
115
+ max_bid.price.should == 17.0
116
+ max_bid.eprice.should == 16.8895
117
+ max_bid.amount.should == 82.53875035
118
+ end
119
+ end
120
+
121
+ end
122
+
123
+ describe '#trades' do
124
+ before do
125
+ stub_get('/api/0/data/getTrades.php').
126
+ to_return(:status => 200, :body => fixture('trades.json'))
127
+ end
128
+
129
+ it "should fetch trades" do
130
+ trades = @client.trades
131
+ a_get('/api/0/data/getTrades.php').
132
+ should have_been_made
133
+ trades.last.date.should == Time.utc(2011, 6, 27, 18, 28, 8)
134
+ trades.last.price.should == 17.00009
135
+ trades.last.amount.should == 0.5
136
+ trades.last.id.should == 1309199288687054
137
+ end
138
+ end
139
+
140
+ describe '#balance' do
141
+ before do
142
+ stub_post('/api/0/getFunds.php').
143
+ with(:body => test_body, :headers => test_headers).
144
+ to_return(:status => 200, :body => fixture('balance.json'))
145
+ end
146
+
147
+ it "should fetch balance" do
148
+ balance = @client.balance
149
+ a_post("/api/0/getFunds.php").
150
+ with(:body => test_body, :headers => test_headers).
151
+ should have_been_made
152
+ balance.first.currency.should == "BTC"
153
+ balance.first.amount.should == 22.0
154
+ balance.last.currency.should == "USD"
155
+ balance.last.amount.should == 3.7
156
+ end
157
+ end
158
+
159
+ describe "order methods" do
160
+ before :each do
161
+ stub_post('/api/0/getOrders.php').
162
+ with(:body => test_body, :headers => test_headers).
163
+ to_return(:status => 200, :body => fixture('orders.json'))
164
+ end
165
+
166
+ describe "#buys" do
167
+ it "should fetch orders" do
168
+ buys = @client.buys
169
+ a_post("/api/0/getOrders.php").
170
+ with(:body => test_body, :headers => test_headers).
171
+ should have_been_made
172
+ buys.last.price.should == 7
173
+ buys.last.date.should == Time.utc(2011, 6, 27, 18, 20, 38)
174
+ end
175
+ end
176
+
177
+ describe "#sells" do
178
+ it "should fetch sells" do
179
+ sells = @client.sells
180
+ a_post("/api/0/getOrders.php").
181
+ with(:body => test_body, :headers => test_headers).
182
+ should have_been_made
183
+ sells.last.price.should == 99.0
184
+ sells.last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
185
+ end
186
+ end
187
+
188
+ describe "#orders" do
189
+ it "should fetch both buys and sells, with only one call" do
190
+ orders = @client.orders
191
+ a_post("/api/0/getOrders.php").
192
+ with(:body => test_body, :headers => test_headers).
193
+ should have_been_made
194
+ orders[:buys].last.price.should == 7.0
195
+ orders[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 20, 38)
196
+ orders[:sells].last.price.should == 99.0
197
+ orders[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
198
+ end
199
+ end
200
+ end
201
+
202
+ describe "#buy!" do
203
+ before do
204
+ body = test_body({"amount" => "0.88", "price" => "0.89"})
205
+ stub_post('/api/0/buyBTC.php').
206
+ with(:body => body, :headers => test_headers(body)).
207
+ to_return(:status => 200, :body => fixture('buy.json'))
208
+ end
209
+
210
+ it "should place a bid" do
211
+ buy = @client.buy!(0.88, 0.89)
212
+ body = test_body({"amount" => "0.88", "price" => "0.89"})
213
+ a_post("/api/0/buyBTC.php").
214
+ with(:body => body, :headers => test_headers(body)).
215
+ should have_been_made
216
+ buy[:buys].last.price.should == 2.0
217
+ buy[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 26, 21)
218
+ buy[:sells].last.price.should == 99.0
219
+ buy[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
220
+ end
221
+ end
222
+
223
+ describe "#sell!" do
224
+ before do
225
+ body = test_body({"amount" => "0.88", "price" => "89.0"})
226
+ stub_post('/api/0/sellBTC.php').
227
+ with(:body => body, :headers => test_headers(body)).
228
+ to_return(:status => 200, :body => fixture('sell.json'))
229
+ end
230
+
231
+ it "should place an ask" do
232
+ body = test_body({"amount" => "0.88", "price" => "89.0"})
233
+ sell = @client.sell!(0.88, 89.0)
234
+ a_post("/api/0/sellBTC.php").
235
+ with(:body => body, :headers => test_headers(body)).
236
+ should have_been_made
237
+ sell[:buys].last.price.should == 2.0
238
+ sell[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 26, 21)
239
+ sell[:sells].last.price.should == 200
240
+ sell[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 27, 54)
241
+ end
242
+ end
243
+
244
+ describe "#cancel" do
245
+ before do
246
+ cancel_body = test_body({"oid" => "bddd042c-e837-4a88-a92e-3b7c05e483df", "type" => "2"})
247
+ stub_post('/api/0/getOrders.php').
248
+ with(:body => test_body, :headers => test_headers).
249
+ to_return(:status => 200, :body => fixture('orders.json'))
250
+ stub_post('/api/0/cancelOrder.php').
251
+ with(:body => cancel_body, :headers => test_headers(cancel_body)).
252
+ to_return(:status => 200, :body => fixture('cancel.json'))
253
+ end
254
+
255
+ context "with a valid oid passed" do
256
+ it "should cancel an order" do
257
+ cancel = @client.cancel("bddd042c-e837-4a88-a92e-3b7c05e483df")
258
+ cancel_body = test_body({"oid" => "bddd042c-e837-4a88-a92e-3b7c05e483df", "type" => "2"})
259
+ a_post("/api/0/getOrders.php").
260
+ with(:body => test_body, :headers => test_headers).
261
+ should have_been_made.once
262
+ a_post('/api/0/cancelOrder.php').
263
+ with(:body => cancel_body, :headers => test_headers(cancel_body)).
264
+ should have_been_made
265
+ cancel[:buys].last.price.should == 7.0
266
+ cancel[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 20, 38)
267
+ cancel[:sells].last.price.should == 99.0
268
+ cancel[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
269
+ end
270
+ end
271
+
272
+ context "with an invalid oid passed" do
273
+ it "should raise an error" do
274
+ lambda do
275
+ @client.cancel(1234567890)
276
+ end.should raise_error(Faraday::Error::ResourceNotFound)
277
+ end
278
+ end
279
+
280
+ context "with an order passed" do
281
+ it "should cancel an order" do
282
+ cancel = @client.cancel({'oid' => "bddd042c-e837-4a88-a92e-3b7c05e483df", 'type' => 2})
283
+ body = test_body({"oid" => "bddd042c-e837-4a88-a92e-3b7c05e483df", "type" => "2"})
284
+ a_post('/api/0/cancelOrder.php').
285
+ with(:body => body, :headers => test_headers(body)).
286
+ should have_been_made
287
+ cancel[:buys].last.price.should == 7.0
288
+ cancel[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 20, 38)
289
+ cancel[:sells].last.price.should == 99.0
290
+ cancel[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
291
+ end
292
+ end
293
+ end
294
+
295
+ describe "#withdraw!" do
296
+ before do
297
+ body = test_body({"group1" => "BTC", "amount" => "1.0", "btca" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
298
+ stub_post('/api/0/withdraw.php').
299
+ with(:body => body, :headers => test_headers(body)).
300
+ to_return(:status => 200, :body => fixture('withdraw.json'))
301
+ end
302
+
303
+ it "should withdraw funds" do
304
+ withdraw = @client.withdraw!(1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L")
305
+ body = test_body({"group1" => "BTC", "amount" => "1.0", "btca" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
306
+ a_post("/api/0/withdraw.php").
307
+ with(:body => body, :headers => test_headers(body)).
308
+ should have_been_made
309
+ withdraw.first.currency.should == "BTC"
310
+ withdraw.first.amount.should == 9.0
311
+ withdraw.last.currency.should == "USD"
312
+ withdraw.last.amount.should == 64.59
313
+ end
314
+ end
315
+ end
@@ -0,0 +1,22 @@
1
+ require 'helper'
2
+
3
+ describe MtGox do
4
+ describe ".new" do
5
+ it "should return a MtGox::Client" do
6
+ MtGox.new.should be_a MtGox::Client
7
+ end
8
+ end
9
+
10
+ describe ".configure" do
11
+ it "should set 'key' and 'secret'" do
12
+ MtGox.configure do |config|
13
+ config.key = "key"
14
+ config.secret = "secret"
15
+ end
16
+
17
+ MtGox.key.should == "key"
18
+ MtGox.secret.should == "secret"
19
+ end
20
+ end
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,226 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mt_gox
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.7.3
6
+ platform: ruby
7
+ authors:
8
+ - Erik Michaels-Ober
9
+ - arvicco
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2012-01-16 00:00:00 +03:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: faraday
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: "0.7"
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: faraday_middleware
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: "0.7"
37
+ type: :runtime
38
+ version_requirements: *id002
39
+ - !ruby/object:Gem::Dependency
40
+ name: multi_json
41
+ prerelease: false
42
+ requirement: &id003 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: "1.0"
48
+ type: :runtime
49
+ version_requirements: *id003
50
+ - !ruby/object:Gem::Dependency
51
+ name: json
52
+ prerelease: false
53
+ requirement: &id004 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id004
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ prerelease: false
64
+ requirement: &id005 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id005
72
+ - !ruby/object:Gem::Dependency
73
+ name: rdiscount
74
+ prerelease: false
75
+ requirement: &id006 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id006
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ prerelease: false
86
+ requirement: &id007 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ type: :development
93
+ version_requirements: *id007
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ prerelease: false
97
+ requirement: &id008 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "0"
103
+ type: :development
104
+ version_requirements: *id008
105
+ - !ruby/object:Gem::Dependency
106
+ name: webmock
107
+ prerelease: false
108
+ requirement: &id009 !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ type: :development
115
+ version_requirements: *id009
116
+ - !ruby/object:Gem::Dependency
117
+ name: yard
118
+ prerelease: false
119
+ requirement: &id010 !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: "0"
125
+ type: :development
126
+ version_requirements: *id010
127
+ description: Ruby wrapper for the Mt. Gox Trade API. Extended with Models.
128
+ email: arvicco@gmail.com
129
+ executables: []
130
+
131
+ extensions: []
132
+
133
+ extra_rdoc_files: []
134
+
135
+ files:
136
+ - .gitignore
137
+ - .rspec
138
+ - .rvmrc
139
+ - .travis.yml
140
+ - .yardopts
141
+ - Gemfile
142
+ - LICENSE.md
143
+ - README.md
144
+ - Rakefile
145
+ - VERSION
146
+ - lib/faraday/response/raise_mtgox_error.rb
147
+ - lib/mtgox.rb
148
+ - lib/mtgox/ask.rb
149
+ - lib/mtgox/balance.rb
150
+ - lib/mtgox/bid.rb
151
+ - lib/mtgox/buy.rb
152
+ - lib/mtgox/client.rb
153
+ - lib/mtgox/configuration.rb
154
+ - lib/mtgox/connection.rb
155
+ - lib/mtgox/error.rb
156
+ - lib/mtgox/max_bid.rb
157
+ - lib/mtgox/min_ask.rb
158
+ - lib/mtgox/offer.rb
159
+ - lib/mtgox/order.rb
160
+ - lib/mtgox/price_ticker.rb
161
+ - lib/mtgox/request.rb
162
+ - lib/mtgox/sell.rb
163
+ - lib/mtgox/ticker.rb
164
+ - lib/mtgox/trade.rb
165
+ - lib/mtgox/version.rb
166
+ - spec/faraday/response_spec.rb
167
+ - spec/fixtures/address.json
168
+ - spec/fixtures/balance.json
169
+ - spec/fixtures/buy.json
170
+ - spec/fixtures/cancel.json
171
+ - spec/fixtures/depth.json
172
+ - spec/fixtures/error.json
173
+ - spec/fixtures/mysql_error
174
+ - spec/fixtures/orders.json
175
+ - spec/fixtures/sell.json
176
+ - spec/fixtures/ticker.json
177
+ - spec/fixtures/trades.json
178
+ - spec/fixtures/withdraw.json
179
+ - spec/helper.rb
180
+ - spec/mtgox/client_spec.rb
181
+ - spec/mtgox_spec.rb
182
+ has_rdoc: true
183
+ homepage: https://github.com/arvicco/mtgox
184
+ licenses: []
185
+
186
+ post_install_message:
187
+ rdoc_options: []
188
+
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ none: false
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: 1.9.2
197
+ required_rubygems_version: !ruby/object:Gem::Requirement
198
+ none: false
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: "0"
203
+ requirements: []
204
+
205
+ rubyforge_project:
206
+ rubygems_version: 1.6.2
207
+ signing_key:
208
+ specification_version: 3
209
+ summary: Ruby wrapper for the Mt. Gox Trade API.
210
+ test_files:
211
+ - spec/faraday/response_spec.rb
212
+ - spec/fixtures/address.json
213
+ - spec/fixtures/balance.json
214
+ - spec/fixtures/buy.json
215
+ - spec/fixtures/cancel.json
216
+ - spec/fixtures/depth.json
217
+ - spec/fixtures/error.json
218
+ - spec/fixtures/mysql_error
219
+ - spec/fixtures/orders.json
220
+ - spec/fixtures/sell.json
221
+ - spec/fixtures/ticker.json
222
+ - spec/fixtures/trades.json
223
+ - spec/fixtures/withdraw.json
224
+ - spec/helper.rb
225
+ - spec/mtgox/client_spec.rb
226
+ - spec/mtgox_spec.rb