mt_gox 0.7.6 → 0.7.7

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -10,3 +10,7 @@
10
10
  == 0.7.6 / 2012-01-17
11
11
 
12
12
  * Multi-currency ticker and Depth streaming Models added
13
+
14
+ == 0.7.7 / 2012-01-17
15
+
16
+ * Multi-currency ticker and Depth string coercion /to_s/ added
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.6
1
+ 0.7.7
data/lib/mtgox/client.rb CHANGED
@@ -10,6 +10,8 @@ module MtGox
10
10
 
11
11
  ORDER_TYPES = {:sell => 1, :buy => 2}
12
12
 
13
+ @@ticker = {}
14
+
13
15
  # Fetch a deposit address
14
16
  # @authenticated true
15
17
  # @return [String]
@@ -26,10 +28,18 @@ module MtGox
26
28
  # @return [MtGox::Ticker]
27
29
  # @example
28
30
  # MtGox.ticker
29
- def ticker
30
- ticker = get('/api/0/data/ticker.php')['ticker']
31
- Ticker.instance.set_attributes ticker
32
- Ticker.instance
31
+ # MtGox.ticker('EUR')
32
+ def ticker(currency=nil)
33
+ if currency
34
+ ticker = get("/api/1/BTC#{currency}/public/ticker")['return']
35
+ @@ticker[currency] ||= MultiTicker.new :currency => currency
36
+ @@ticker[currency].set_attributes ticker
37
+ @@ticker[currency]
38
+ else
39
+ ticker = get('/api/0/data/ticker.php')['ticker']
40
+ Ticker.instance.set_attributes ticker
41
+ Ticker.instance
42
+ end
33
43
  end
34
44
 
35
45
  # Fetch both bids and asks in one call, for network efficiency
@@ -3,9 +3,7 @@ require 'mtgox/models/model'
3
3
  module MtGox
4
4
  module Models
5
5
  class Depth < Model
6
- prop :buy,
7
- :sell,
8
- :currency,
6
+ prop :currency,
9
7
  :item,
10
8
  [:vol, :volume] => :f,
11
9
  :volume_int => :i,
@@ -14,6 +12,12 @@ module MtGox
14
12
  :price_int => :i,
15
13
  [:id, :now] => :i,
16
14
  [:type, :type_str] => proc {|val| val == 1 || val == 'bid' ? :bid : :ask}
15
+
16
+ def to_s
17
+ "<Depth: vol change #{ vol.round(2) } at #{ price.round(3) }"+
18
+ "(#{type}) #{currency}/#{item} >"
19
+ end
20
+
17
21
  end
18
22
  end
19
23
  end
@@ -18,6 +18,12 @@ module MtGox
18
18
  :low => proc { |val| val['value'].to_f },
19
19
  :avg => proc { |val| val['value'].to_f },
20
20
  :vwap => proc { |val| val['value'].to_f }
21
+
22
+ def to_s
23
+ "<MultiTicker: #{currency} vol #{ vol.round(2) } last #{ last.round(3) }"+
24
+ " buy #{ buy.round(3) } sell #{ sell.round(3) } low #{ low.round(3) }"+
25
+ " high #{ high.round(3) } avg #{ avg.round(3) } vwap #{ vwap.round(3) } >"
26
+ end
21
27
  end
22
28
  end
23
29
  end
@@ -13,6 +13,8 @@ module MtGox
13
13
  :low,
14
14
  [:vol, :volume],
15
15
  [:last, :price],
16
+ :last_all,
17
+ :last_local,
16
18
  :avg,
17
19
  :vwap
18
20
 
@@ -0,0 +1 @@
1
+ {"result":"success", "return":{"high":{"value":"5.50000", "value_int":"550000", "display":"5.50000��", "currency":"EUR"}, "low":{"value":"5.12541", "value_int":"512541", "display":"5.12541��", "currency":"EUR"}, "avg":{"value":"5.31551", "value_int":"531551", "display":"5.31551��", "currency":"EUR"}, "vwap":{"value":"5.29709", "value_int":"529709", "display":"5.29709��", "currency":"EUR"}, "vol":{"value":"1277.68857150", "value_int":"127768857150", "display":"1,277.68857150�BTC", "currency":"BTC"}, "last_local":{"value":"5.34000", "value_int":"534000", "display":"5.34000��", "currency":"EUR"}, "last":{"value":"5.34000", "value_int":"534000", "display":"5.34000��", "currency":"EUR"}, "last_orig":{"value":"6.65001", "value_int":"665001", "display":"$6.65001", "currency":"USD"}, "last_all":{"value":"5.24904", "value_int":"524904", "display":"5.24904��", "currency":"EUR"}, "buy":{"value":"5.34000", "value_int":"534000", "display":"5.34000��", "currency":"EUR"}, "sell":{"value":"5.35000", "value_int":"535000", "display":"5.35000��", "currency":"EUR"}}}
@@ -1 +1 @@
1
- {"ticker":{"high":28.678,"low":18.4,"vol":80531,"buy":26.4,"sell":26.6099,"last":26.5,"vwap":3.81561}}
1
+ {"ticker":{"high":6.95, "low":6.4501, "avg":6.691234882, "vwap":6.668617425, "vol":113312, "last_all":6.69, "last_local":6.69, "last":6.69, "buy":6.6809, "sell":6.69803}}
@@ -12,48 +12,73 @@ describe MtGox::Client do
12
12
  describe '#address' do
13
13
  before do
14
14
  stub_post('/api/0/btcAddress.php').
15
- to_return(:status => 200, :body => fixture('address.json'))
15
+ to_return(:status => 200, :body => fixture('address.json'))
16
16
  end
17
17
 
18
18
  it "should fetch a deposit address" do
19
19
  address = @client.address
20
20
  a_post('/api/0/btcAddress.php').
21
- should have_been_made
21
+ should have_been_made
22
22
  address.should == '171dH9Uum6wWLSwH2g8g2yto6SG7NEGyXG'
23
23
  end
24
24
  end
25
25
 
26
26
  describe '#ticker' do
27
27
  before do
28
+ #WebMock.allow_net_connect!
28
29
  stub_get('/api/0/data/ticker.php').
29
- to_return(:status => 200, :body => fixture('ticker.json'))
30
+ to_return(:status => 200, :body => fixture('ticker.json'))
31
+ stub_get('/api/1/BTCEUR/public/ticker').
32
+ to_return(:status => 200, :body => fixture('multi-ticker.json'))
30
33
  end
31
34
 
32
35
  it "should fetch the ticker" do
33
36
  ticker = @client.ticker
34
37
  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
38
+ should have_been_made
39
+ ticker.buy.should == 6.6809
40
+ ticker.sell.should == 6.69803
41
+ ticker.high.should == 6.95
42
+ ticker.low.should == 6.4501
43
+ ticker.price.should == 6.69
44
+ ticker.last_all.should == 6.69
45
+ ticker.last_local.should == 6.69
46
+ ticker.volume.should == 113312
47
+ ticker.avg.should == 6.691234882
48
+ ticker.vwap.should == 6.668617425
49
+ end
50
+
51
+ it "should fetch the multi-currency ticker" do
52
+ ticker = @client.ticker('EUR')
53
+ a_get('/api/1/BTCEUR/public/ticker').
54
+ should have_been_made
55
+ ticker.buy.should == 5.340
56
+ ticker.sell.should == 5.350
57
+ ticker.high.should == 5.500
58
+ ticker.low.should == 5.12541
59
+ ticker.price.should == 5.340
60
+ ticker.last.should == 5.340
61
+ ticker.last_local.should == 5.340
62
+ ticker.last_orig.should == 6.65001 # "currency":"USD"
63
+ ticker.last_all.should == 5.24904 # ?
64
+ ticker.volume.should == 1277.68857150
65
+ ticker.avg.should == 5.31551
66
+ ticker.vwap.should == 5.29709
67
+ ticker.currency.should == 'EUR'
43
68
  end
44
69
  end
45
70
 
46
71
  describe 'depth methods' do
47
72
  before :each do
48
73
  stub_get('/api/0/data/getDepth.php').
49
- to_return(:status => 200, :body => fixture('depth.json'))
74
+ to_return(:status => 200, :body => fixture('depth.json'))
50
75
  end
51
76
 
52
77
  describe '#asks' do
53
78
  it "should fetch open asks" do
54
79
  asks = @client.asks
55
80
  a_get('/api/0/data/getDepth.php').
56
- should have_been_made
81
+ should have_been_made
57
82
  asks.last.price.should == 23.75
58
83
  asks.last.eprice.should == 23.905385002516354
59
84
  asks.last.amount.should == 50
@@ -61,7 +86,7 @@ describe MtGox::Client do
61
86
 
62
87
  it "should be sorted in price-ascending order" do
63
88
  asks = @client.asks
64
- asks.sort_by{|ask| ask.price}.should == asks
89
+ asks.sort_by { |ask| ask.price }.should == asks
65
90
  end
66
91
 
67
92
  end
@@ -70,7 +95,7 @@ describe MtGox::Client do
70
95
  it "should fetch open bids" do
71
96
  bids = @client.bids
72
97
  a_get('/api/0/data/getDepth.php').
73
- should have_been_made
98
+ should have_been_made
74
99
  bids.last.price.should == 14.62101
75
100
  bids.last.eprice.should == 14.525973435000001
76
101
  bids.last.amount.should == 5
@@ -78,7 +103,7 @@ describe MtGox::Client do
78
103
 
79
104
  it "should be sorted in price-descending order" do
80
105
  bids = @client.bids
81
- bids.sort_by{|bid| bid.price}.reverse.should == bids
106
+ bids.sort_by { |bid| bid.price }.reverse.should == bids
82
107
  end
83
108
  end
84
109
 
@@ -86,7 +111,7 @@ describe MtGox::Client do
86
111
  it "should fetch both bids and asks, with only one call" do
87
112
  offers = @client.offers
88
113
  a_get('/api/0/data/getDepth.php').
89
- should have_been_made.once
114
+ should have_been_made.once
90
115
  offers[:asks].last.price.should == 23.75
91
116
  offers[:asks].last.eprice.should == 23.905385002516354
92
117
  offers[:asks].last.amount.should == 50
@@ -100,7 +125,7 @@ describe MtGox::Client do
100
125
  it "should fetch the lowest priced ask" do
101
126
  min_ask = @client.min_ask
102
127
  a_get('/api/0/data/getDepth.php').
103
- should have_been_made.once
128
+ should have_been_made.once
104
129
  min_ask.price.should == 17.00009
105
130
  min_ask.eprice.should == 17.11131353799698
106
131
  min_ask.amount.should == 36.22894353
@@ -111,7 +136,7 @@ describe MtGox::Client do
111
136
  it "should fetch the highest priced bid" do
112
137
  max_bid = @client.max_bid
113
138
  a_get('/api/0/data/getDepth.php').
114
- should have_been_made.once
139
+ should have_been_made.once
115
140
  max_bid.price.should == 17.0
116
141
  max_bid.eprice.should == 16.8895
117
142
  max_bid.amount.should == 82.53875035
@@ -123,13 +148,13 @@ describe MtGox::Client do
123
148
  describe '#trades' do
124
149
  before do
125
150
  stub_get('/api/0/data/getTrades.php').
126
- to_return(:status => 200, :body => fixture('trades.json'))
151
+ to_return(:status => 200, :body => fixture('trades.json'))
127
152
  end
128
153
 
129
154
  it "should fetch trades" do
130
155
  trades = @client.trades
131
156
  a_get('/api/0/data/getTrades.php').
132
- should have_been_made
157
+ should have_been_made
133
158
  trades.last.date.should == Time.utc(2011, 6, 27, 18, 28, 8)
134
159
  trades.last.price.should == 17.00009
135
160
  trades.last.amount.should == 0.5
@@ -140,15 +165,15 @@ describe MtGox::Client do
140
165
  describe '#balance' do
141
166
  before do
142
167
  stub_post('/api/0/getFunds.php').
143
- with(:body => test_body, :headers => test_headers).
144
- to_return(:status => 200, :body => fixture('balance.json'))
168
+ with(:body => test_body, :headers => test_headers).
169
+ to_return(:status => 200, :body => fixture('balance.json'))
145
170
  end
146
171
 
147
172
  it "should fetch balance" do
148
173
  balance = @client.balance
149
174
  a_post("/api/0/getFunds.php").
150
- with(:body => test_body, :headers => test_headers).
151
- should have_been_made
175
+ with(:body => test_body, :headers => test_headers).
176
+ should have_been_made
152
177
  balance.first.currency.should == "BTC"
153
178
  balance.first.amount.should == 22.0
154
179
  balance.last.currency.should == "USD"
@@ -159,16 +184,16 @@ describe MtGox::Client do
159
184
  describe "order methods" do
160
185
  before :each do
161
186
  stub_post('/api/0/getOrders.php').
162
- with(:body => test_body, :headers => test_headers).
163
- to_return(:status => 200, :body => fixture('orders.json'))
187
+ with(:body => test_body, :headers => test_headers).
188
+ to_return(:status => 200, :body => fixture('orders.json'))
164
189
  end
165
190
 
166
191
  describe "#buys" do
167
192
  it "should fetch orders" do
168
193
  buys = @client.buys
169
194
  a_post("/api/0/getOrders.php").
170
- with(:body => test_body, :headers => test_headers).
171
- should have_been_made
195
+ with(:body => test_body, :headers => test_headers).
196
+ should have_been_made
172
197
  buys.last.price.should == 7
173
198
  buys.last.date.should == Time.utc(2011, 6, 27, 18, 20, 38)
174
199
  end
@@ -178,8 +203,8 @@ describe MtGox::Client do
178
203
  it "should fetch sells" do
179
204
  sells = @client.sells
180
205
  a_post("/api/0/getOrders.php").
181
- with(:body => test_body, :headers => test_headers).
182
- should have_been_made
206
+ with(:body => test_body, :headers => test_headers).
207
+ should have_been_made
183
208
  sells.last.price.should == 99.0
184
209
  sells.last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
185
210
  end
@@ -189,8 +214,8 @@ describe MtGox::Client do
189
214
  it "should fetch both buys and sells, with only one call" do
190
215
  orders = @client.orders
191
216
  a_post("/api/0/getOrders.php").
192
- with(:body => test_body, :headers => test_headers).
193
- should have_been_made
217
+ with(:body => test_body, :headers => test_headers).
218
+ should have_been_made
194
219
  orders[:buys].last.price.should == 7.0
195
220
  orders[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 20, 38)
196
221
  orders[:sells].last.price.should == 99.0
@@ -203,16 +228,16 @@ describe MtGox::Client do
203
228
  before do
204
229
  body = test_body({"amount" => "0.88", "price" => "0.89"})
205
230
  stub_post('/api/0/buyBTC.php').
206
- with(:body => body, :headers => test_headers(body)).
207
- to_return(:status => 200, :body => fixture('buy.json'))
231
+ with(:body => body, :headers => test_headers(body)).
232
+ to_return(:status => 200, :body => fixture('buy.json'))
208
233
  end
209
234
 
210
235
  it "should place a bid" do
211
236
  buy = @client.buy!(0.88, 0.89)
212
237
  body = test_body({"amount" => "0.88", "price" => "0.89"})
213
238
  a_post("/api/0/buyBTC.php").
214
- with(:body => body, :headers => test_headers(body)).
215
- should have_been_made
239
+ with(:body => body, :headers => test_headers(body)).
240
+ should have_been_made
216
241
  buy[:buys].last.price.should == 2.0
217
242
  buy[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 26, 21)
218
243
  buy[:sells].last.price.should == 99.0
@@ -224,16 +249,16 @@ describe MtGox::Client do
224
249
  before do
225
250
  body = test_body({"amount" => "0.88", "price" => "89.0"})
226
251
  stub_post('/api/0/sellBTC.php').
227
- with(:body => body, :headers => test_headers(body)).
228
- to_return(:status => 200, :body => fixture('sell.json'))
252
+ with(:body => body, :headers => test_headers(body)).
253
+ to_return(:status => 200, :body => fixture('sell.json'))
229
254
  end
230
255
 
231
256
  it "should place an ask" do
232
257
  body = test_body({"amount" => "0.88", "price" => "89.0"})
233
258
  sell = @client.sell!(0.88, 89.0)
234
259
  a_post("/api/0/sellBTC.php").
235
- with(:body => body, :headers => test_headers(body)).
236
- should have_been_made
260
+ with(:body => body, :headers => test_headers(body)).
261
+ should have_been_made
237
262
  sell[:buys].last.price.should == 2.0
238
263
  sell[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 26, 21)
239
264
  sell[:sells].last.price.should == 200
@@ -245,11 +270,11 @@ describe MtGox::Client do
245
270
  before do
246
271
  cancel_body = test_body({"oid" => "bddd042c-e837-4a88-a92e-3b7c05e483df", "type" => "2"})
247
272
  stub_post('/api/0/getOrders.php').
248
- with(:body => test_body, :headers => test_headers).
249
- to_return(:status => 200, :body => fixture('orders.json'))
273
+ with(:body => test_body, :headers => test_headers).
274
+ to_return(:status => 200, :body => fixture('orders.json'))
250
275
  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'))
276
+ with(:body => cancel_body, :headers => test_headers(cancel_body)).
277
+ to_return(:status => 200, :body => fixture('cancel.json'))
253
278
  end
254
279
 
255
280
  context "with a valid oid passed" do
@@ -257,11 +282,11 @@ describe MtGox::Client do
257
282
  cancel = @client.cancel("bddd042c-e837-4a88-a92e-3b7c05e483df")
258
283
  cancel_body = test_body({"oid" => "bddd042c-e837-4a88-a92e-3b7c05e483df", "type" => "2"})
259
284
  a_post("/api/0/getOrders.php").
260
- with(:body => test_body, :headers => test_headers).
261
- should have_been_made.once
285
+ with(:body => test_body, :headers => test_headers).
286
+ should have_been_made.once
262
287
  a_post('/api/0/cancelOrder.php').
263
- with(:body => cancel_body, :headers => test_headers(cancel_body)).
264
- should have_been_made
288
+ with(:body => cancel_body, :headers => test_headers(cancel_body)).
289
+ should have_been_made
265
290
  cancel[:buys].last.price.should == 7.0
266
291
  cancel[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 20, 38)
267
292
  cancel[:sells].last.price.should == 99.0
@@ -282,8 +307,8 @@ describe MtGox::Client do
282
307
  cancel = @client.cancel({'oid' => "bddd042c-e837-4a88-a92e-3b7c05e483df", 'type' => 2})
283
308
  body = test_body({"oid" => "bddd042c-e837-4a88-a92e-3b7c05e483df", "type" => "2"})
284
309
  a_post('/api/0/cancelOrder.php').
285
- with(:body => body, :headers => test_headers(body)).
286
- should have_been_made
310
+ with(:body => body, :headers => test_headers(body)).
311
+ should have_been_made
287
312
  cancel[:buys].last.price.should == 7.0
288
313
  cancel[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 20, 38)
289
314
  cancel[:sells].last.price.should == 99.0
@@ -296,16 +321,16 @@ describe MtGox::Client do
296
321
  before do
297
322
  body = test_body({"group1" => "BTC", "amount" => "1.0", "btca" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
298
323
  stub_post('/api/0/withdraw.php').
299
- with(:body => body, :headers => test_headers(body)).
300
- to_return(:status => 200, :body => fixture('withdraw.json'))
324
+ with(:body => body, :headers => test_headers(body)).
325
+ to_return(:status => 200, :body => fixture('withdraw.json'))
301
326
  end
302
327
 
303
328
  it "should withdraw funds" do
304
329
  withdraw = @client.withdraw!(1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L")
305
330
  body = test_body({"group1" => "BTC", "amount" => "1.0", "btca" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
306
331
  a_post("/api/0/withdraw.php").
307
- with(:body => body, :headers => test_headers(body)).
308
- should have_been_made
332
+ with(:body => body, :headers => test_headers(body)).
333
+ should have_been_made
309
334
  withdraw.first.currency.should == "BTC"
310
335
  withdraw.first.amount.should == 9.0
311
336
  withdraw.last.currency.should == "USD"
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mt_gox
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.7.6
5
+ version: 0.7.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Erik Michaels-Ober
@@ -177,6 +177,7 @@ files:
177
177
  - spec/fixtures/error.json
178
178
  - spec/fixtures/io-depth.json
179
179
  - spec/fixtures/io-ticker.json
180
+ - spec/fixtures/multi-ticker.json
180
181
  - spec/fixtures/mysql_error
181
182
  - spec/fixtures/orders.json
182
183
  - spec/fixtures/sell.json
@@ -230,6 +231,7 @@ test_files:
230
231
  - spec/fixtures/error.json
231
232
  - spec/fixtures/io-depth.json
232
233
  - spec/fixtures/io-ticker.json
234
+ - spec/fixtures/multi-ticker.json
233
235
  - spec/fixtures/mysql_error
234
236
  - spec/fixtures/orders.json
235
237
  - spec/fixtures/sell.json