mtgox 0.9.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/lib/faraday/response/raise_mtgox_error.rb +5 -4
- data/lib/mtgox/balance.rb +3 -1
- data/lib/mtgox/client.rb +1 -1
- data/lib/mtgox/configuration.rb +2 -1
- data/lib/mtgox/error.rb +1 -0
- data/lib/mtgox/lag.rb +3 -1
- data/lib/mtgox/order.rb +7 -3
- data/lib/mtgox/price_ticker.rb +5 -3
- data/lib/mtgox/response/parse_json.rb +2 -2
- data/lib/mtgox/trade.rb +2 -2
- data/lib/mtgox/value.rb +12 -10
- data/lib/mtgox/version.rb +22 -20
- data/mtgox.gemspec +1 -1
- data/spec/faraday/response_spec.rb +12 -6
- data/spec/helper.rb +8 -1
- data/spec/mtgox/client_spec.rb +118 -151
- data/spec/mtgox_spec.rb +5 -5
- metadata +11 -5
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
@@ -1,14 +1,15 @@
|
|
1
1
|
require 'faraday'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
module Faraday
|
4
5
|
class Response::RaiseMtGoxError < Response::Middleware
|
5
6
|
def on_complete(env)
|
6
7
|
if 200 == env[:status] && 'MySQL error, please retry later' == env[:body]
|
7
8
|
raise MtGox::MysqlError, "MySQL error, please retry later"
|
8
|
-
elsif 403 == env[:status] &&
|
9
|
-
raise MtGox::UnauthorizedError,
|
10
|
-
elsif 404 != env[:status] &&
|
11
|
-
raise MtGox::Error,
|
9
|
+
elsif 403 == env[:status] && JSON.load(env[:body])["result"] == "error"
|
10
|
+
raise MtGox::UnauthorizedError, JSON.load(env[:body])["error"]
|
11
|
+
elsif 404 != env[:status] && JSON.load(env[:body])["result"] == "error"
|
12
|
+
raise MtGox::Error, JSON.load(env[:body])["error"]
|
12
13
|
end
|
13
14
|
end
|
14
15
|
end
|
data/lib/mtgox/balance.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
1
3
|
module MtGox
|
2
4
|
class Balance
|
3
5
|
attr_accessor :currency, :amount
|
4
6
|
|
5
7
|
def initialize(currency=nil, amount=nil)
|
6
8
|
self.currency = currency.to_s.upcase
|
7
|
-
self.amount = amount.
|
9
|
+
self.amount = BigDecimal(amount.to_s)
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
data/lib/mtgox/client.rb
CHANGED
@@ -267,7 +267,7 @@ module MtGox
|
|
267
267
|
orders.delete_if{|o| o['oid'] == res['oid']}
|
268
268
|
parse_orders(orders)
|
269
269
|
else
|
270
|
-
raise
|
270
|
+
raise MtGox::OrderNotFoundError
|
271
271
|
end
|
272
272
|
end
|
273
273
|
alias cancel_order cancel
|
data/lib/mtgox/configuration.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'mtgox/version'
|
2
|
+
require 'bigdecimal'
|
2
3
|
|
3
4
|
module MtGox
|
4
5
|
module Configuration
|
@@ -9,7 +10,7 @@ module MtGox
|
|
9
10
|
:secret,
|
10
11
|
]
|
11
12
|
|
12
|
-
DEFAULT_COMMISSION = 0.0065.freeze
|
13
|
+
DEFAULT_COMMISSION = BigDecimal('0.0065').freeze
|
13
14
|
|
14
15
|
attr_accessor *VALID_OPTIONS_KEYS
|
15
16
|
|
data/lib/mtgox/error.rb
CHANGED
data/lib/mtgox/lag.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
1
3
|
module MtGox
|
2
4
|
class Lag
|
3
5
|
attr_accessor :microseconds, :seconds, :text, :length
|
4
6
|
|
5
7
|
def initialize(lag=nil, lag_secs=nil, lag_text=nil, length=nil)
|
6
8
|
self.microseconds = lag.to_i
|
7
|
-
self.seconds = lag_secs.
|
9
|
+
self.seconds = BigDecimal(lag_secs.to_s)
|
8
10
|
self.text = lag_text
|
9
11
|
self.length = length.to_i
|
10
12
|
end
|
data/lib/mtgox/order.rb
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
require 'mtgox/offer'
|
2
|
+
require 'bigdecimal'
|
2
3
|
|
3
4
|
module MtGox
|
4
5
|
class Order < Offer
|
5
|
-
attr_accessor :id, :date
|
6
|
+
attr_accessor :id, :date, :item, :status, :currency
|
6
7
|
|
7
8
|
def initialize(order={})
|
8
9
|
self.id = order['oid']
|
9
10
|
self.date = Time.at(order['date'].to_i)
|
10
|
-
self.amount = order['amount']['value']
|
11
|
-
self.price = order['price']['value']
|
11
|
+
self.amount = BigDecimal(order['amount']['value'])
|
12
|
+
self.price = BigDecimal(order['price']['value'])
|
13
|
+
self.item = order['item']
|
14
|
+
self.status = order['status']
|
15
|
+
self.currency = order['currency']
|
12
16
|
end
|
13
17
|
end
|
14
18
|
end
|
data/lib/mtgox/price_ticker.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
1
3
|
module MtGox
|
2
4
|
module PriceTicker
|
3
5
|
attr_reader :previous_price, :price
|
@@ -8,15 +10,15 @@ module MtGox
|
|
8
10
|
end
|
9
11
|
|
10
12
|
def up?
|
11
|
-
price.
|
13
|
+
BigDecimal(price.to_s) > BigDecimal(previous_price.to_s)
|
12
14
|
end
|
13
15
|
|
14
16
|
def down?
|
15
|
-
price.
|
17
|
+
BigDecimal(price.to_s) < BigDecimal(previous_price.to_s)
|
16
18
|
end
|
17
19
|
|
18
20
|
def changed?
|
19
|
-
price.
|
21
|
+
BigDecimal(price.to_s) != BigDecimal(previous_price.to_s)
|
20
22
|
end
|
21
23
|
|
22
24
|
def unchanged?
|
data/lib/mtgox/trade.rb
CHANGED
@@ -6,8 +6,8 @@ module MtGox
|
|
6
6
|
def initialize(trade={})
|
7
7
|
self.id = trade['tid'].to_i
|
8
8
|
self.date = Time.at(trade['date'].to_i)
|
9
|
-
self.amount = trade['amount']
|
10
|
-
self.price = trade['price']
|
9
|
+
self.amount = BigDecimal(trade['amount'])
|
10
|
+
self.price = BigDecimal(trade['price'])
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
data/lib/mtgox/value.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
1
3
|
# In the "old API", currency- and amount-values (price, volume,...)
|
2
4
|
# were given as float. These values are likely being deprecated and
|
3
5
|
# replaced by fields of the same name with "_int" as suffix. These are
|
@@ -18,7 +20,7 @@ module MtGox
|
|
18
20
|
# @authenticated false
|
19
21
|
# @return [Float]
|
20
22
|
def value_currency(value, key = 'value_int')
|
21
|
-
|
23
|
+
decimalify(value[key].to_i, :usd)
|
22
24
|
end
|
23
25
|
|
24
26
|
# Takes a hash return by the API and convert some value_int to
|
@@ -29,25 +31,25 @@ module MtGox
|
|
29
31
|
# @authenticated false
|
30
32
|
# @return [Float] a float BTC value
|
31
33
|
def value_bitcoin(value, key = 'value_int')
|
32
|
-
|
34
|
+
decimalify(value[key], :btc)
|
33
35
|
end
|
34
36
|
|
35
|
-
# Convert a
|
37
|
+
# Convert a BigDecimal value to an int using the MtGox conversion rules.
|
36
38
|
#
|
37
|
-
# param
|
39
|
+
# param decimal [BigDecimal] to convert
|
38
40
|
# param currency [Symbol] currency conversion rule to use amongst [:btc, :usd, :jpy]
|
39
41
|
# return an int
|
40
|
-
def intify(
|
41
|
-
(
|
42
|
+
def intify(decimal, currency)
|
43
|
+
(decimal * INT_MULTIPLIERS[currency]).to_i
|
42
44
|
end
|
43
45
|
|
44
|
-
# Convert an int value to a
|
46
|
+
# Convert an int value to a decimal using the MtGox conversion rules.
|
45
47
|
#
|
46
48
|
# param int [Fixnum] to convert
|
47
49
|
# param currency [Symbol] currency conversion rule to use amongst [:btc, :usd, :jpy]
|
48
|
-
# return a [
|
49
|
-
def
|
50
|
-
(int.
|
50
|
+
# return a [BigDecimal]
|
51
|
+
def decimalify(int, currency)
|
52
|
+
(BigDecimal(int.to_s) / INT_MULTIPLIERS[currency])
|
51
53
|
end
|
52
54
|
end
|
53
55
|
end
|
data/lib/mtgox/version.rb
CHANGED
@@ -1,30 +1,32 @@
|
|
1
1
|
module MtGox
|
2
2
|
class Version
|
3
|
+
class << self
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
# @return [Integer]
|
6
|
+
def major
|
7
|
+
1
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
# @return [Integer]
|
11
|
+
def minor
|
12
|
+
0
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
# @return [Integer]
|
16
|
+
def patch
|
17
|
+
0
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
# @return [String, NilClass]
|
21
|
+
def pre
|
22
|
+
nil
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
# @return [String]
|
26
|
+
def to_s
|
27
|
+
[major, minor, patch, pre].compact.join('.')
|
28
|
+
end
|
28
29
|
|
30
|
+
end
|
29
31
|
end
|
30
32
|
end
|
data/mtgox.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'mtgox/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.add_dependency 'faraday', ['~> 0.8', '< 0.10']
|
8
|
-
spec.add_dependency '
|
8
|
+
spec.add_dependency 'json', ['~> 1.7', '>= 1.7.7']
|
9
9
|
spec.add_development_dependency 'bundler', '~> 1.0'
|
10
10
|
spec.author = "Erik Michaels-Ober"
|
11
11
|
spec.bindir = 'bin'
|
@@ -7,8 +7,10 @@ describe Faraday::Response do
|
|
7
7
|
to_return(status: 200, body: fixture('mysql_error'))
|
8
8
|
end
|
9
9
|
|
10
|
-
it "
|
11
|
-
expect {
|
10
|
+
it "raises MtGox::MysqlError" do
|
11
|
+
expect {
|
12
|
+
MtGox.trades
|
13
|
+
}.to raise_error(MtGox::MysqlError)
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
@@ -18,8 +20,10 @@ describe Faraday::Response do
|
|
18
20
|
to_return(status: 200, body: fixture('unknown_error.json'))
|
19
21
|
end
|
20
22
|
|
21
|
-
it "
|
22
|
-
expect {
|
23
|
+
it "raises MtGox::Error" do
|
24
|
+
expect {
|
25
|
+
MtGox.trades
|
26
|
+
}.to raise_error(MtGox::Error)
|
23
27
|
end
|
24
28
|
|
25
29
|
describe "UnauthorizedError" do
|
@@ -28,8 +32,10 @@ describe Faraday::Response do
|
|
28
32
|
to_return(status: 403, body: fixture('error.json'))
|
29
33
|
end
|
30
34
|
|
31
|
-
it "
|
32
|
-
expect {
|
35
|
+
it "raises MtGox::UnauthorizedError" do
|
36
|
+
expect {
|
37
|
+
MtGox.trades
|
38
|
+
}.to raise_error(MtGox::UnauthorizedError)
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
data/spec/helper.rb
CHANGED
@@ -7,11 +7,18 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
|
7
7
|
]
|
8
8
|
SimpleCov.start
|
9
9
|
|
10
|
-
require 'base64'
|
11
10
|
require 'mtgox'
|
11
|
+
require 'base64'
|
12
|
+
require 'json'
|
12
13
|
require 'rspec'
|
13
14
|
require 'webmock/rspec'
|
14
15
|
|
16
|
+
RSpec.configure do |config|
|
17
|
+
config.expect_with :rspec do |c|
|
18
|
+
c.syntax = :expect
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
15
22
|
WebMock.disable_net_connect!(:allow => 'coveralls.io')
|
16
23
|
|
17
24
|
def a_get(path)
|
data/spec/mtgox/client_spec.rb
CHANGED
@@ -15,11 +15,10 @@ describe MtGox::Client do
|
|
15
15
|
to_return(body: fixture('address.json'))
|
16
16
|
end
|
17
17
|
|
18
|
-
it "
|
18
|
+
it "fetchs a deposit address" do
|
19
19
|
address = @client.address
|
20
|
-
a_post('/api/1/generic/bitcoin/address').
|
21
|
-
|
22
|
-
address.should == '17A1vbzQ39o8cGNnpqx8UvXNrhqwAEP8wY'
|
20
|
+
expect(a_post('/api/1/generic/bitcoin/address')).to have_been_made
|
21
|
+
expect(address).to eq '17A1vbzQ39o8cGNnpqx8UvXNrhqwAEP8wY'
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
@@ -29,11 +28,10 @@ describe MtGox::Client do
|
|
29
28
|
to_return(body: fixture('idkey.json'))
|
30
29
|
end
|
31
30
|
|
32
|
-
it "
|
31
|
+
it "fetches an idkey suitable to WS Api usage" do
|
33
32
|
key = @client.idkey
|
34
|
-
a_post('/api/1/generic/idkey').
|
35
|
-
|
36
|
-
key.should == 'YCKvmyU4QsaHEqM/AvKlqAAAAABRbR5y0vCn1roteQx/Ux/lyLF27X8Em1e4AN/2etPECzIT6dU'
|
33
|
+
expect(a_post('/api/1/generic/idkey')).to have_been_made
|
34
|
+
expect(key).to eq 'YCKvmyU4QsaHEqM/AvKlqAAAAABRbR5y0vCn1roteQx/Ux/lyLF27X8Em1e4AN/2etPECzIT6dU'
|
37
35
|
end
|
38
36
|
end
|
39
37
|
|
@@ -43,29 +41,27 @@ describe MtGox::Client do
|
|
43
41
|
to_return(body: fixture('ticker.json'))
|
44
42
|
end
|
45
43
|
|
46
|
-
it "
|
44
|
+
it "fetches the ticker" do
|
47
45
|
ticker = @client.ticker
|
48
|
-
a_get('/api/1/BTCUSD/ticker').
|
49
|
-
|
50
|
-
ticker.
|
51
|
-
ticker.
|
52
|
-
ticker.
|
53
|
-
ticker.
|
54
|
-
ticker.
|
55
|
-
ticker.
|
56
|
-
ticker.
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
it "should fetch the ticker and keep previous price" do
|
46
|
+
expect(a_get('/api/1/BTCUSD/ticker')).to have_been_made
|
47
|
+
expect(ticker.buy).to eq BigDecimal('5.53587')
|
48
|
+
expect(ticker.sell).to eq BigDecimal('5.56031')
|
49
|
+
expect(ticker.high).to eq BigDecimal('5.70653')
|
50
|
+
expect(ticker.low).to eq BigDecimal('5.4145')
|
51
|
+
expect(ticker.price).to eq BigDecimal('5.5594')
|
52
|
+
expect(ticker.volume).to eq BigDecimal('55829.58960346')
|
53
|
+
expect(ticker.vwap).to eq BigDecimal('5.61048')
|
54
|
+
expect(ticker.avg).to eq BigDecimal('5.56112')
|
55
|
+
end
|
56
|
+
|
57
|
+
it "fetches the ticker and keep previous price" do
|
61
58
|
ticker = @client.ticker
|
62
59
|
ticker = @client.ticker
|
63
|
-
a_get('/api/1/BTCUSD/ticker').
|
64
|
-
|
65
|
-
ticker.
|
66
|
-
ticker.
|
67
|
-
ticker.
|
68
|
-
ticker.unchanged?.should == true
|
60
|
+
expect(a_get('/api/1/BTCUSD/ticker')).to have_been_made.twice
|
61
|
+
expect(ticker.up?).to be_false
|
62
|
+
expect(ticker.down?).to be_false
|
63
|
+
expect(ticker.changed?).to be_false
|
64
|
+
expect(ticker.unchanged?).to be_true
|
69
65
|
end
|
70
66
|
end
|
71
67
|
|
@@ -75,14 +71,13 @@ describe MtGox::Client do
|
|
75
71
|
to_return(status:200, body: fixture('lag.json'))
|
76
72
|
end
|
77
73
|
|
78
|
-
it "
|
74
|
+
it "fetches the lag" do
|
79
75
|
lag = @client.lag
|
80
|
-
a_get('/api/1/generic/order/lag').
|
81
|
-
|
82
|
-
lag.
|
83
|
-
lag.
|
84
|
-
lag.
|
85
|
-
lag.length.should == 3
|
76
|
+
expect(a_get('/api/1/generic/order/lag')).to have_been_made
|
77
|
+
expect(lag.microseconds).to eq 535998
|
78
|
+
expect(lag.seconds).to eq BigDecimal('0.535998')
|
79
|
+
expect(lag.text).to eq "0.535998 seconds"
|
80
|
+
expect(lag.length).to eq 3
|
86
81
|
end
|
87
82
|
end
|
88
83
|
|
@@ -93,71 +88,66 @@ describe MtGox::Client do
|
|
93
88
|
end
|
94
89
|
|
95
90
|
describe '#asks' do
|
96
|
-
it "
|
91
|
+
it "fetches open asks" do
|
97
92
|
asks = @client.asks
|
98
|
-
a_get('/api/1/BTCUSD/depth/fetch').
|
99
|
-
|
100
|
-
asks.first.
|
101
|
-
asks.first.
|
102
|
-
asks.first.amount.should == 0.43936758
|
93
|
+
expect(a_get('/api/1/BTCUSD/depth/fetch')).to have_been_made
|
94
|
+
expect(asks.first.price).to eq 114
|
95
|
+
expect(asks.first.eprice).to eq BigDecimal('114.745848012')
|
96
|
+
expect(asks.first.amount).to eq BigDecimal('0.43936758')
|
103
97
|
end
|
104
98
|
|
105
99
|
it "should be sorted in price-ascending order" do
|
106
100
|
asks = @client.asks
|
107
|
-
asks.sort_by
|
101
|
+
expect(asks.sort_by(&:price)).to eq asks
|
108
102
|
end
|
109
103
|
|
110
104
|
end
|
111
105
|
|
112
106
|
describe "#bids" do
|
113
|
-
it "
|
107
|
+
it "fetches open bids" do
|
114
108
|
bids = @client.bids
|
115
|
-
a_get('/api/1/BTCUSD/depth/fetch').
|
116
|
-
|
117
|
-
bids.first.
|
118
|
-
bids.first.
|
119
|
-
bids.first.amount.should == 124.69802063
|
109
|
+
expect(a_get('/api/1/BTCUSD/depth/fetch')).to have_been_made
|
110
|
+
expect(bids.first.price).to eq BigDecimal('113.0')
|
111
|
+
expect(bids.first.eprice).to eq BigDecimal('112.2655')
|
112
|
+
expect(bids.first.amount).to eq BigDecimal('124.69802063')
|
120
113
|
end
|
121
114
|
|
122
115
|
it "should be sorted in price-descending order" do
|
123
116
|
bids = @client.bids
|
124
|
-
bids.sort_by
|
117
|
+
expect(bids.sort_by(&:price).reverse).to eq bids
|
125
118
|
end
|
126
119
|
end
|
127
120
|
|
128
121
|
describe "#offers" do
|
129
|
-
it "
|
122
|
+
it "fetches both bids and asks, with only one call" do
|
130
123
|
offers = @client.offers
|
131
|
-
a_get('/api/1/BTCUSD/depth/fetch').
|
132
|
-
|
133
|
-
offers[:asks].first.
|
134
|
-
offers[:asks].first.
|
135
|
-
offers[:
|
136
|
-
offers[:bids].first.
|
137
|
-
offers[:bids].first.
|
138
|
-
offers[:bids].first.amount.should == 124.69802063
|
124
|
+
expect(a_get('/api/1/BTCUSD/depth/fetch')).to have_been_made.once
|
125
|
+
expect(offers[:asks].first.price).to eq 114
|
126
|
+
expect(offers[:asks].first.eprice).to eq BigDecimal('114.745848012')
|
127
|
+
expect(offers[:asks].first.amount).to eq BigDecimal('0.43936758')
|
128
|
+
expect(offers[:bids].first.price).to eq BigDecimal('113.0')
|
129
|
+
expect(offers[:bids].first.eprice).to eq BigDecimal('112.2655')
|
130
|
+
expect(offers[:bids].first.amount).to eq BigDecimal('124.69802063')
|
139
131
|
end
|
140
132
|
end
|
141
133
|
|
142
134
|
describe '#min_ask' do
|
143
|
-
it "
|
135
|
+
it "fetches the lowest priced ask" do
|
144
136
|
min_ask = @client.min_ask
|
145
|
-
a_get('/api/1/BTCUSD/depth/fetch').
|
146
|
-
|
147
|
-
min_ask.
|
148
|
-
min_ask.
|
149
|
-
min_ask.amount.should == 0.43936758
|
137
|
+
expect(a_get('/api/1/BTCUSD/depth/fetch')).to have_been_made.once
|
138
|
+
expect(min_ask.price).to eq 114
|
139
|
+
expect(min_ask.eprice).to eq BigDecimal('114.745848012')
|
140
|
+
expect(min_ask.amount).to eq BigDecimal('0.43936758')
|
150
141
|
end
|
151
142
|
end
|
152
143
|
|
153
144
|
describe '#max_bid' do
|
154
|
-
it "
|
145
|
+
it "fetches the highest priced bid" do
|
155
146
|
max_bid = @client.max_bid
|
156
|
-
a_get('/api/1/BTCUSD/depth/fetch').
|
157
|
-
|
158
|
-
max_bid.
|
159
|
-
max_bid.
|
160
|
-
max_bid.amount.should == 124.69802063
|
147
|
+
expect(a_get('/api/1/BTCUSD/depth/fetch')).to have_been_made.once
|
148
|
+
expect(max_bid.price).to eq 113
|
149
|
+
expect(max_bid.eprice).to eq BigDecimal('112.2655')
|
150
|
+
expect(max_bid.amount).to eq BigDecimal('124.69802063')
|
161
151
|
end
|
162
152
|
end
|
163
153
|
|
@@ -169,32 +159,29 @@ describe MtGox::Client do
|
|
169
159
|
to_return(body: fixture('trades.json'))
|
170
160
|
end
|
171
161
|
|
172
|
-
it "
|
162
|
+
it "fetches trades" do
|
173
163
|
trades = @client.trades
|
174
|
-
a_get('/api/1/BTCUSD/trades/fetch').
|
175
|
-
|
176
|
-
trades.last.
|
177
|
-
trades.last.
|
178
|
-
trades.last.
|
179
|
-
trades.last.id.should == 1365780003374123
|
164
|
+
expect(a_get('/api/1/BTCUSD/trades/fetch')).to have_been_made
|
165
|
+
expect(trades.last.date).to eq Time.utc(2013, 4, 12, 15, 20, 3)
|
166
|
+
expect(trades.last.price).to eq BigDecimal('73.19258')
|
167
|
+
expect(trades.last.amount).to eq BigDecimal('0.94043572')
|
168
|
+
expect(trades.last.id).to eq 1365780003374123
|
180
169
|
end
|
181
170
|
end
|
182
171
|
|
183
172
|
describe '#trades :since' do
|
184
173
|
before do
|
185
|
-
trades =
|
174
|
+
trades = JSON.load(fixture('trades.json'))
|
186
175
|
stub_get('/api/1/BTCUSD/trades/fetch?since=1365780002144150').
|
187
|
-
to_return(body:
|
176
|
+
to_return(body: JSON.dump({result: 'success', return: trades['return'].select{|t| t['tid'] >= '1365780002144150'}}))
|
188
177
|
end
|
189
178
|
|
190
|
-
it "
|
179
|
+
it "fetches trades since an id" do
|
191
180
|
trades = @client.trades :since => 1365780002144150
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
trades.first.
|
196
|
-
trades.first.amount.should == 11.76583944
|
197
|
-
trades.first.id.should == 1365780002144150
|
181
|
+
expect(a_get('/api/1/BTCUSD/trades/fetch?since=1365780002144150')).to have_been_made
|
182
|
+
expect(trades.first.price).to eq BigDecimal('72.98274')
|
183
|
+
expect(trades.first.amount).to eq BigDecimal('11.76583944')
|
184
|
+
expect(trades.first.id).to eq 1365780002144150
|
198
185
|
end
|
199
186
|
end
|
200
187
|
|
@@ -205,15 +192,13 @@ describe MtGox::Client do
|
|
205
192
|
to_return(body: fixture('info.json'))
|
206
193
|
end
|
207
194
|
|
208
|
-
it "
|
195
|
+
it "fetches balance" do
|
209
196
|
balance = @client.balance
|
210
|
-
a_post("/api/1/generic/info").
|
211
|
-
|
212
|
-
|
213
|
-
balance.
|
214
|
-
balance.
|
215
|
-
balance.last.currency.should == "EUR"
|
216
|
-
balance.last.amount.should == 23.0
|
197
|
+
expect(a_post("/api/1/generic/info").with(body: test_body, headers: test_headers(@client))).to have_been_made
|
198
|
+
expect(balance.first.currency).to eq "BTC"
|
199
|
+
expect(balance.first.amount).to eq BigDecimal('42.0')
|
200
|
+
expect(balance.last.currency).to eq "EUR"
|
201
|
+
expect(balance.last.amount).to eq BigDecimal('23.0')
|
217
202
|
end
|
218
203
|
end
|
219
204
|
|
@@ -225,37 +210,35 @@ describe MtGox::Client do
|
|
225
210
|
end
|
226
211
|
|
227
212
|
describe "#buys" do
|
228
|
-
it "
|
213
|
+
it "fetches orders" do
|
229
214
|
buys = @client.buys
|
230
|
-
a_post("/api/1/generic/orders").
|
231
|
-
|
232
|
-
|
233
|
-
buys.last.
|
234
|
-
buys.last.
|
215
|
+
expect(a_post("/api/1/generic/orders").with(body: test_body, headers: test_headers(@client))).to have_been_made
|
216
|
+
expect(buys.last.price).to eq 7
|
217
|
+
expect(buys.last.date).to eq Time.utc(2011, 6, 27, 18, 20, 38)
|
218
|
+
expect(buys.last.amount).to eq BigDecimal("0.2")
|
219
|
+
expect(buys.last.status).to eq "open"
|
220
|
+
expect(buys.last.currency).to eq "USD"
|
221
|
+
expect(buys.last.item).to eq "BTC"
|
235
222
|
end
|
236
223
|
end
|
237
224
|
|
238
225
|
describe "#sells" do
|
239
|
-
it "
|
226
|
+
it "fetches sells" do
|
240
227
|
sells = @client.sells
|
241
|
-
a_post("/api/1/generic/orders").
|
242
|
-
|
243
|
-
|
244
|
-
sells.last.price.should == 99.0
|
245
|
-
sells.last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
|
228
|
+
expect(a_post("/api/1/generic/orders").with(body: test_body, headers: test_headers(@client))).to have_been_made
|
229
|
+
expect(sells.last.price).to eq BigDecimal('99.0')
|
230
|
+
expect(sells.last.date).to eq Time.utc(2011, 6, 27, 18, 20, 20)
|
246
231
|
end
|
247
232
|
end
|
248
233
|
|
249
234
|
describe "#orders" do
|
250
|
-
it "
|
235
|
+
it "fetches both buys and sells, with only one call" do
|
251
236
|
orders = @client.orders
|
252
|
-
a_post("/api/1/generic/orders").
|
253
|
-
|
254
|
-
|
255
|
-
orders[:
|
256
|
-
orders[:
|
257
|
-
orders[:sells].last.price.should == 99.0
|
258
|
-
orders[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
|
237
|
+
expect(a_post("/api/1/generic/orders").with(body: test_body, headers: test_headers(@client))).to have_been_made
|
238
|
+
expect(orders[:buys].last.price).to eq BigDecimal('7.0')
|
239
|
+
expect(orders[:buys].last.date).to eq Time.utc(2011, 6, 27, 18, 20, 38)
|
240
|
+
expect(orders[:sells].last.price).to eq BigDecimal('99.0')
|
241
|
+
expect(orders[:sells].last.date).to eq Time.utc(2011, 6, 27, 18, 20, 20)
|
259
242
|
end
|
260
243
|
end
|
261
244
|
end
|
@@ -264,7 +247,6 @@ describe MtGox::Client do
|
|
264
247
|
before do
|
265
248
|
body = test_body({"type" => "bid", "amount_int" => "88000000", "price_int" => "89000"})
|
266
249
|
body_market = test_body({"type" => "bid", "amount_int" => "88000000"})
|
267
|
-
|
268
250
|
stub_post('/api/1/BTCUSD/order/add').
|
269
251
|
with(body: body, headers: test_headers(@client, body)).
|
270
252
|
to_return(body: fixture('buy.json'))
|
@@ -276,19 +258,15 @@ describe MtGox::Client do
|
|
276
258
|
it "should place a bid" do
|
277
259
|
buy = @client.buy!(0.88, 0.89)
|
278
260
|
body = test_body({"type" => "bid", "amount_int" => "88000000", "price_int" => "89000"})
|
279
|
-
a_post("/api/1/BTCUSD/order/add").
|
280
|
-
|
281
|
-
should have_been_made
|
282
|
-
buy.should == "490a214f-9a30-449f-acb8-780f9046502f"
|
261
|
+
expect(a_post("/api/1/BTCUSD/order/add").with(body: body, headers: test_headers(@client, body))).to have_been_made
|
262
|
+
expect(buy).to eq "490a214f-9a30-449f-acb8-780f9046502f"
|
283
263
|
end
|
284
264
|
|
285
265
|
it "should place a market bid" do
|
286
266
|
buy = @client.buy!(0.88, :market)
|
287
267
|
body_market = test_body({"type" => "bid", "amount_int" => "88000000"})
|
288
|
-
a_post("/api/1/BTCUSD/order/add").
|
289
|
-
|
290
|
-
should have_been_made
|
291
|
-
buy.should == "490a214f-9a30-449f-acb8-780f9046502f"
|
268
|
+
expect(a_post("/api/1/BTCUSD/order/add").with(body: body_market, headers: test_headers(@client, body_market))).to have_been_made
|
269
|
+
expect(buy).to eq "490a214f-9a30-449f-acb8-780f9046502f"
|
292
270
|
end
|
293
271
|
end
|
294
272
|
|
@@ -308,19 +286,15 @@ describe MtGox::Client do
|
|
308
286
|
it "should place an ask" do
|
309
287
|
body = test_body({"type" => "ask", "amount_int" => "88000000", "price_int" => "8900000"})
|
310
288
|
sell = @client.sell!(0.88, 89.0)
|
311
|
-
a_post("/api/1/BTCUSD/order/add").
|
312
|
-
|
313
|
-
should have_been_made
|
314
|
-
sell.should == "a20329fe-c0d5-4378-b204-79a7800d41e7"
|
289
|
+
expect(a_post("/api/1/BTCUSD/order/add").with(body: body, headers: test_headers(@client, body))).to have_been_made
|
290
|
+
expect(sell).to eq "a20329fe-c0d5-4378-b204-79a7800d41e7"
|
315
291
|
end
|
316
292
|
|
317
293
|
it "should place a market ask" do
|
318
294
|
body_market = test_body({"type" => "ask", "amount_int" => "88000000"})
|
319
295
|
sell = @client.sell!(0.88, :market)
|
320
|
-
a_post("/api/1/BTCUSD/order/add").
|
321
|
-
|
322
|
-
should have_been_made
|
323
|
-
sell.should == "a20329fe-c0d5-4378-b204-79a7800d41e7"
|
296
|
+
expect(a_post("/api/1/BTCUSD/order/add").with(body: body_market, headers: test_headers(@client, body_market))).to have_been_made
|
297
|
+
expect(sell).to eq "a20329fe-c0d5-4378-b204-79a7800d41e7"
|
324
298
|
end
|
325
299
|
end
|
326
300
|
|
@@ -339,19 +313,15 @@ describe MtGox::Client do
|
|
339
313
|
it "should cancel an order" do
|
340
314
|
cancel = @client.cancel("fda8917a-63d3-4415-b827-758408013690")
|
341
315
|
cancel_body = test_body({"oid" => "fda8917a-63d3-4415-b827-758408013690"})
|
342
|
-
a_post("/api/1/generic/orders").
|
343
|
-
|
344
|
-
|
345
|
-
a_post('/api/1/BTCUSD/order/cancel').
|
346
|
-
with(body: cancel_body, headers: test_headers(@client, cancel_body)).
|
347
|
-
should have_been_made
|
348
|
-
cancel[:buys].length.should == 0
|
316
|
+
expect(a_post("/api/1/generic/orders").with(body: test_body, headers: test_headers(@client))).to have_been_made.once
|
317
|
+
expect(a_post('/api/1/BTCUSD/order/cancel').with(body: cancel_body, headers: test_headers(@client, cancel_body))).to have_been_made
|
318
|
+
expect(cancel[:buys].length).to eq 0
|
349
319
|
end
|
350
320
|
end
|
351
321
|
|
352
322
|
context "with an invalid oid passed" do
|
353
323
|
it "should raise an error" do
|
354
|
-
expect { @client.cancel(1234567890) }.to raise_error(
|
324
|
+
expect { @client.cancel(1234567890) }.to raise_error(MtGox::OrderNotFoundError)
|
355
325
|
end
|
356
326
|
end
|
357
327
|
|
@@ -359,12 +329,10 @@ describe MtGox::Client do
|
|
359
329
|
it "should cancel an order" do
|
360
330
|
cancel = @client.cancel({'oid' => "fda8917a-63d3-4415-b827-758408013690", 'type' => 2})
|
361
331
|
body = test_body({"oid" => "fda8917a-63d3-4415-b827-758408013690"})
|
362
|
-
a_post('/api/1/BTCUSD/order/cancel').
|
363
|
-
|
364
|
-
|
365
|
-
cancel[:
|
366
|
-
cancel[:sells].last.price.should == 99.0
|
367
|
-
cancel[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
|
332
|
+
expect(a_post('/api/1/BTCUSD/order/cancel').with(body: body, headers: test_headers(@client, body))).to have_been_made
|
333
|
+
expect(cancel[:buys].length).to eq 0
|
334
|
+
expect(cancel[:sells].last.price).to eq BigDecimal('99.0')
|
335
|
+
expect(cancel[:sells].last.date).to eq Time.utc(2011, 6, 27, 18, 20, 20)
|
368
336
|
end
|
369
337
|
end
|
370
338
|
end
|
@@ -380,15 +348,14 @@ describe MtGox::Client do
|
|
380
348
|
it "should withdraw funds" do
|
381
349
|
withdraw = @client.withdraw!(1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L")
|
382
350
|
body = test_body({"amount_int" => "100000000", "address" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
|
383
|
-
a_post("/api/1/generic/bitcoin/send_simple").
|
384
|
-
|
385
|
-
should have_been_made
|
386
|
-
withdraw.should == "311295deadbeef390a13c038e2b8ba77feebdaed2c1a59e6e0bdf001656e1314"
|
351
|
+
expect(a_post("/api/1/generic/bitcoin/send_simple").with(body: body, headers: test_headers(@client, body))).to have_been_made
|
352
|
+
expect(withdraw).to eq "311295deadbeef390a13c038e2b8ba77feebdaed2c1a59e6e0bdf001656e1314"
|
387
353
|
end
|
388
354
|
|
389
355
|
it "pays attention to too big withdrawals" do
|
390
|
-
|
391
|
-
|
356
|
+
expect {
|
357
|
+
@client.withdraw!(10000, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L")
|
358
|
+
}.to raise_error(MtGox::FilthyRichError)
|
392
359
|
end
|
393
360
|
end
|
394
361
|
end
|
data/spec/mtgox_spec.rb
CHANGED
@@ -2,20 +2,20 @@ require 'helper'
|
|
2
2
|
|
3
3
|
describe MtGox do
|
4
4
|
describe ".new" do
|
5
|
-
it "
|
6
|
-
MtGox.new.
|
5
|
+
it "returns a MtGox::Client" do
|
6
|
+
expect(MtGox.new).to be_a MtGox::Client
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
describe ".configure" do
|
11
|
-
it "
|
11
|
+
it "sets key and secret" do
|
12
12
|
MtGox.configure do |config|
|
13
13
|
config.key = "key"
|
14
14
|
config.secret = "secret"
|
15
15
|
end
|
16
16
|
|
17
|
-
MtGox.key.
|
18
|
-
MtGox.secret.
|
17
|
+
expect(MtGox.key).to eq "key"
|
18
|
+
expect(MtGox.secret).to eq "secret"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mtgox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
U0xxV3ZRUnNCbHlwSGZoczZKSnVMbHlaUEdoVTNSL3YKU2YzbFZLcEJDV2dS
|
37
37
|
cEdUdnk0NVhWcEIrNTl5MzNQSm1FdVExUFRFT1l2UXlhbzlVS01BQWFBTi83
|
38
38
|
cVdRdGpsMApobHc9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
|
39
|
-
date: 2013-05-
|
39
|
+
date: 2013-05-12 00:00:00.000000000 Z
|
40
40
|
dependencies:
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: faraday
|
@@ -61,13 +61,16 @@ dependencies:
|
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0.10'
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
|
-
name:
|
64
|
+
name: json
|
65
65
|
requirement: !ruby/object:Gem::Requirement
|
66
66
|
none: false
|
67
67
|
requirements:
|
68
68
|
- - ~>
|
69
69
|
- !ruby/object:Gem::Version
|
70
|
-
version: '1.
|
70
|
+
version: '1.7'
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 1.7.7
|
71
74
|
type: :runtime
|
72
75
|
prerelease: false
|
73
76
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -75,7 +78,10 @@ dependencies:
|
|
75
78
|
requirements:
|
76
79
|
- - ~>
|
77
80
|
- !ruby/object:Gem::Version
|
78
|
-
version: '1.
|
81
|
+
version: '1.7'
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 1.7.7
|
79
85
|
- !ruby/object:Gem::Dependency
|
80
86
|
name: bundler
|
81
87
|
requirement: !ruby/object:Gem::Requirement
|
metadata.gz.sig
CHANGED
Binary file
|