mtgox 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,7 +32,7 @@ author, which you can do using the following script:
32
32
  config.pass = YOUR_MTGOX_PASSWORD
33
33
  end
34
34
 
35
- MtGox.send 1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"
35
+ MtGox.withdraw 1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"
36
36
 
37
37
  Continuous Integration
38
38
  ----------------------
@@ -56,7 +56,7 @@ Usage Examples
56
56
  puts MtGox.trades
57
57
 
58
58
  # Certain methods require authentication
59
- MtGox.configure do |config| [TODO]
59
+ MtGox.configure do |config|
60
60
  config.name = YOUR_MTGOX_USERNAME
61
61
  config.pass = YOUR_MTGOX_PASSWORD
62
62
  end
@@ -64,17 +64,17 @@ Usage Examples
64
64
  # Get your current balance
65
65
  puts MtGox.balance
66
66
 
67
- # Place an order to buy 1 BTC for 20 USD (returns a list of your open orders)
68
- puts MtGox.buy! 1.0, 20.0
67
+ # Place a limit order to buy one bitcoin for $0.011
68
+ MtGox.buy! 1.0, 0.011
69
69
 
70
- # Place an order to sell 1 BTC for 20 USD (returns a list of your open orders)
71
- puts MtGox.sell! 1.0, 20.0
70
+ # Place a limit order to sell one bitcoin for $100
71
+ MtGox.sell! 1.0, 100.0
72
72
 
73
73
  # Cancel order #1234567890
74
- puts MtGox.cancel 1234567890
74
+ MtGox.cancel 1234567890
75
75
 
76
- # Send 1 BTC to the author of this gem
77
- puts MtGox.send 1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L" [TODO]
76
+ # Withdraw 1 BTC from your account
77
+ MtGox.withdraw! 1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"
78
78
 
79
79
  Contributing
80
80
  ------------
data/lib/mtgox.rb CHANGED
@@ -6,22 +6,22 @@ module MtGox
6
6
  def configure
7
7
  yield self
8
8
  end
9
- end
10
9
 
11
- # Alias for MtGox::Client.new
12
- #
13
- # @return [MtGox::Client]
14
- def self.new
15
- MtGox::Client.new
16
- end
10
+ # Alias for MtGox::Client.new
11
+ #
12
+ # @return [MtGox::Client]
13
+ def new
14
+ MtGox::Client.new
15
+ end
17
16
 
18
- # Delegate to MtGox::Client
19
- def self.method_missing(method, *args, &block)
20
- return super unless new.respond_to?(method)
21
- new.send(method, *args, &block)
22
- end
17
+ # Delegate to MtGox::Client
18
+ def method_missing(method, *args, &block)
19
+ return super unless new.respond_to?(method)
20
+ new.send(method, *args, &block)
21
+ end
23
22
 
24
- def self.respond_to?(method, include_private=false)
25
- new.respond_to?(method, include_private) || super(method, include_private)
23
+ def respond_to?(method, include_private=false)
24
+ new.respond_to?(method, include_private) || super(method, include_private)
25
+ end
26
26
  end
27
27
  end
data/lib/mtgox/client.rb CHANGED
@@ -21,6 +21,18 @@ module MtGox
21
21
  get('/code/data/ticker.php')['ticker']
22
22
  end
23
23
 
24
+ # Fetch both bids and asks in one call, for network efficiency
25
+ #
26
+ # @authenticated false
27
+ # @return [Hashie::Rash] a hash with keys :asks and :bids, which contain arrays as described in #asks and #bids.
28
+ # @example
29
+ # offers = MtGox.offers
30
+ # offers.asks[0, 3] #=> [[19.3898, 3.9], [19.4, 48.264], [19.409, 1]]
31
+ # offers.bids[0, 3] #=> [[19.3898, 77.42], [19.3, 3.02], [19.29, 82.378]]
32
+ def offers
33
+ get('/code/data/getDepth.php')
34
+ end
35
+
24
36
  # Fetch open asks
25
37
  #
26
38
  # @authenticated false
@@ -28,7 +40,7 @@ module MtGox
28
40
  # @example
29
41
  # MtGox.asks[0, 3] #=> [[19.3898, 3.9], [19.4, 48.264], [19.409, 1]]
30
42
  def asks
31
- get('/code/data/getDepth.php')['asks']
43
+ offers['asks']
32
44
  end
33
45
 
34
46
  # Fetch open bids
@@ -38,19 +50,7 @@ module MtGox
38
50
  # @example
39
51
  # MtGox.bids[0, 3] #=> [[19.3898, 77.42], [19.3, 3.02], [19.29, 82.378]]
40
52
  def bids
41
- get('/code/data/getDepth.php')['bids']
42
- end
43
-
44
- # Fetch both bids and asks in one call, for network efficiency
45
- #
46
- # @authenticated false
47
- # @return [Hashie::Rash] a hash with keys :asks and :bids, which contain arrays as described in #asks and #bids.
48
- # @example
49
- # offers = MtGox.offers
50
- # offers.asks[0, 3] #=> [[19.3898, 3.9], [19.4, 48.264], [19.409, 1]]
51
- # offers.bids[0, 3] #=> [[19.3898, 77.42], [19.3, 3.02], [19.29, 82.378]]
52
- def offers
53
- get('/code/data/getDepth.php')
53
+ offers['bids']
54
54
  end
55
55
 
56
56
  # Fetch recent trades
@@ -75,6 +75,16 @@ module MtGox
75
75
  post('/code/getFunds.php', pass_params)
76
76
  end
77
77
 
78
+ # Fetch your open orders, both buys and sells, for network efficiency
79
+ #
80
+ # @authenticated true
81
+ # @return [<Hashie::Rash>] with keys `buy` and `sell`, which contain arrays as described in {MtGox::Client#buys} and {MtGox::Client#sells}
82
+ # @example
83
+ # MtGox.orders[0, 3] #=> [<#Hashie::Rash amount=0.73 dark="0" date="1307949196" oid="929284" price=2 status=:active type=2>, <#Hashie::Rash amount=0.36 dark="0" date="1307949201" oid="929288" price=4 status=:active type=2>, <#Hashie::Rash amount=0.24 dark="0" date="1307949212" oid="929292" price=6 status=:active type=2>]
84
+ def orders
85
+ post('/code/getOrders.php', pass_params)['orders']
86
+ end
87
+
78
88
  # Fetch your open buys
79
89
  #
80
90
  # @authenticated true
@@ -99,47 +109,34 @@ module MtGox
99
109
  end
100
110
  end
101
111
 
102
- # Fetch your open orders, both buys and sells, for network efficiency
103
- #
104
- # @authenticated true
105
- # @return [<Hashie::Rash>] with keys `buy` and `sell`, which contain arrays as described in {MtGox::Client#buys} and {MtGox::Client#sells}
106
- # @example
107
- # MtGox.orders[0, 3] #=> [<#Hashie::Rash amount=0.73 dark="0" date="1307949196" oid="929284" price=2 status=:active type=2>, <#Hashie::Rash amount=0.36 dark="0" date="1307949201" oid="929288" price=4 status=:active type=2>, <#Hashie::Rash amount=0.24 dark="0" date="1307949212" oid="929292" price=6 status=:active type=2>]
108
- def orders
109
- hash = post('/code/getOrders.php', pass_params)['orders']
110
- end
111
-
112
- # Place an order to buy
112
+ # Place a limit order to buy BTC
113
113
  #
114
- # @todo Return something useful
115
114
  # @authenticated true
116
- # @param quantity [Numeric] the number of bitcoins to purchase
115
+ # @param amount [Numeric] the number of bitcoins to purchase
117
116
  # @param price [Numeric] the bid price in US dollars
118
117
  # @return [Array<Hashie::Rash>]
119
118
  # @example
120
- # # Buy one BTC for $25
121
- # MtGox.buy! 1.0, 25.0
122
- def buy!(quantity, price)
123
- post('/code/buyBTC.php', pass_params.merge({:amount => quantity, :price => price}))
119
+ # # Buy one bitcoin for $0.011
120
+ # MtGox.buy! 1.0, 0.011
121
+ def buy!(amount, price)
122
+ post('/code/buyBTC.php', pass_params.merge({:amount => amount, :price => price}))
124
123
  end
125
124
 
126
- # Place an order to sell
125
+ # Place a limit order to sell BTC
127
126
  #
128
- # @todo Return something useful
129
127
  # @authenticated true
130
- # @param quantity [Numeric] the number of bitcoins to sell
128
+ # @param amount [Numeric] the number of bitcoins to sell
131
129
  # @param price [Numeric] the ask price in US dollars
132
130
  # @return [Array<Hashie::Rash>]
133
131
  # @example
134
- # # Sell 0.7 BTC for $26
135
- # MtGox.sell! 0.7, 26.0
136
- def sell!(quantity, price)
137
- post('/code/sellBTC.php', pass_params.merge({:amount => quantity, :price => price}))
132
+ # # Sell one bitcoin for $100
133
+ # MtGox.sell! 1.0, 100.0
134
+ def sell!(amount, price)
135
+ post('/code/sellBTC.php', pass_params.merge({:amount => amount, :price => price}))
138
136
  end
139
137
 
140
138
  # Cancel an open order
141
139
  #
142
- # @todo Return something useful
143
140
  # @authenticated true
144
141
  # @overload cancel(oid)
145
142
  # @param oid [String] an order ID
@@ -157,12 +154,12 @@ module MtGox
157
154
  # MtGox.cancel {"oid" => "1234567890", "type" => 2}
158
155
  def cancel(args)
159
156
  if args.is_a?(Hash)
160
- order = args.select{|k, v| ['oid', 'type'].include?(k)}
157
+ order = args.delete_if{|k, v| !['oid', 'type'].include?(k.to_s)}
161
158
  post('/code/cancelOrder.php', pass_params.merge(order))
162
159
  else
163
160
  order = orders.select{|o| o['oid'] == args.to_s}.first
164
161
  if order
165
- order.select!{|k, v| ['oid', 'type'].include?(k)}
162
+ order = order.delete_if{|k, v| !['oid', 'type'].include?(k.to_s)}
166
163
  post('/code/cancelOrder.php', pass_params.merge(order))
167
164
  else
168
165
  raise Faraday::Error::ResourceNotFound, {:status => 404, :headers => {}, :body => "Order not found."}
@@ -170,11 +167,23 @@ module MtGox
170
167
  end
171
168
  end
172
169
 
170
+ # Transfer bitcoins from your Mt. Gox account into another account
171
+ #
172
+ # @authenticated true
173
+ # @param amount [Numeric] the number of bitcoins to withdraw
174
+ # @param btca [String] the bitcoin address to send to
175
+ # @return [Array<Hashie::Rash>]
176
+ # @example
177
+ # # Withdraw 1 BTC from your account
178
+ # MtGox.withdraw! 1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"
179
+ def withdraw!(amount, btca)
180
+ post('/code/withdraw.php', pass_params.merge({:group1 => "BTC", :amount => amount, :btca => btca}))
181
+ end
182
+
173
183
  private
174
184
 
175
185
  def pass_params
176
186
  {:name => MtGox.name, :pass => MtGox.pass}
177
187
  end
178
-
179
188
  end
180
189
  end
@@ -1,4 +1,5 @@
1
1
  require 'faraday_middleware'
2
+ require 'mtgox/version'
2
3
 
3
4
  module MtGox
4
5
  module Connection
@@ -6,6 +7,10 @@ module MtGox
6
7
 
7
8
  def connection
8
9
  options = {
10
+ :headers => {
11
+ :accept => 'application/json',
12
+ :user_agent => "mtgox gem #{MtGox::VERSION}",
13
+ },
9
14
  :ssl => {:verify => false},
10
15
  :url => 'https://mtgox.com',
11
16
  }
data/lib/mtgox/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MtGox
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
File without changes
@@ -0,0 +1 @@
1
+ {"error":"Not logged in."}
@@ -0,0 +1 @@
1
+ {"usds":64.59,"btcs":9,"status":"Your funds are on their way..."}
data/spec/helper.rb CHANGED
@@ -22,7 +22,6 @@ def stub_post(path)
22
22
  stub_request(:post, 'https://mtgox.com' + path)
23
23
  end
24
24
 
25
-
26
25
  def fixture_path
27
26
  File.expand_path('../fixtures', __FILE__)
28
27
  end
@@ -3,7 +3,10 @@ require 'helper'
3
3
  describe MtGox::Client do
4
4
  before do
5
5
  @client = MtGox::Client.new
6
- MtGox.configure {|c| c.name="my_name"; c.pass="my_password"}
6
+ MtGox.configure do |config|
7
+ config.name = "my_name"
8
+ config.pass = "my_password"
9
+ end
7
10
  end
8
11
 
9
12
  describe '#ticker' do
@@ -54,7 +57,7 @@ describe MtGox::Client do
54
57
  end
55
58
 
56
59
  describe "#offers" do
57
- it "should fetch both bids and asks, making only 1 network request" do
60
+ it "should fetch both bids and asks, with only one call" do
58
61
  offers = @client.offers
59
62
  a_get('/code/data/getDepth.php').should have_been_made.once
60
63
  offers.asks.last.should == [45, 593.28]
@@ -88,12 +91,15 @@ describe MtGox::Client do
88
91
  describe '#balance' do
89
92
  before do
90
93
  stub_post('/code/getFunds.php').
91
- to_return(:status => 200, :body => fixture('funds.json'))
94
+ with(:body => {"name" => "my_name", "pass" => "my_password"}).
95
+ to_return(:status => 200, :body => fixture('balance.json'))
92
96
  end
93
97
 
94
98
  it "should fetch balance" do
95
99
  balance = @client.balance
96
- a_post("/code/getFunds.php").should have_been_made
100
+ a_post("/code/getFunds.php").
101
+ with(:body => {"name" => "my_name", "pass" => "my_password"}).
102
+ should have_been_made
97
103
  balance.usds.should == 3.7
98
104
  balance.btcs.should == 22.0
99
105
  end
@@ -102,13 +108,16 @@ describe MtGox::Client do
102
108
  describe "order methods" do
103
109
  before :each do
104
110
  stub_post('/code/getOrders.php').
111
+ with(:body => {"name" => "my_name", "pass" => "my_password"}).
105
112
  to_return(:status => 200, :body => fixture('orders.json'))
106
113
  end
107
114
 
108
115
  describe "#buys" do
109
116
  it "should fetch orders" do
110
117
  buys = @client.buys
111
- a_post("/code/getOrders.php").should have_been_made
118
+ a_post("/code/getOrders.php").
119
+ with(:body => {"name" => "my_name", "pass" => "my_password"}).
120
+ should have_been_made
112
121
  buys.last.price.should == 14
113
122
  end
114
123
  end
@@ -116,15 +125,28 @@ describe MtGox::Client do
116
125
  describe "#sells" do
117
126
  it "should fetch sells" do
118
127
  sells = @client.sells
119
- a_post("/code/getOrders.php").should have_been_made
128
+ a_post("/code/getOrders.php").
129
+ with(:body => {"name" => "my_name", "pass" => "my_password"}).
130
+ should have_been_made
120
131
  sells.last.price.should == 29.3
121
132
  end
122
133
  end
134
+
135
+ describe "#orders" do
136
+ it "should fetch both buys and sells, with only one call" do
137
+ orders = @client.orders
138
+ a_post("/code/getOrders.php").
139
+ with(:body => {"name" => "my_name", "pass" => "my_password"}).
140
+ should have_been_made
141
+ orders.last.price.should == 29.3
142
+ end
143
+ end
123
144
  end
124
145
 
125
146
  describe "#buy!" do
126
147
  before do
127
148
  stub_post('/code/buyBTC.php').
149
+ with(:body => {"name" => "my_name", "pass" => "my_password", "amount" => "0.88", "price" => "0.89"}).
128
150
  to_return(:status => 200, :body => fixture('buy.json'))
129
151
  end
130
152
 
@@ -139,6 +161,7 @@ describe MtGox::Client do
139
161
  describe "#sell!" do
140
162
  before do
141
163
  stub_post('/code/sellBTC.php').
164
+ with(:body => {"name" => "my_name", "pass" => "my_password", "amount" => "0.88", "price" => "89.0"}).
142
165
  to_return(:status => 200, :body => fixture('sell.json'))
143
166
  end
144
167
 
@@ -153,15 +176,19 @@ describe MtGox::Client do
153
176
  describe "#cancel" do
154
177
  before do
155
178
  stub_post('/code/getOrders.php').
179
+ with(:body => {"name" => "my_name", "pass" => "my_password"}).
156
180
  to_return(:status => 200, :body => fixture('orders.json'))
157
181
  stub_post('/code/cancelOrder.php').
182
+ with(:body => {"name" => "my_name", "pass" => "my_password", "oid" => "929284", "type" => "2"}).
158
183
  to_return(:status => 200, :body => fixture('cancel.json'))
159
184
  end
160
185
 
161
186
  context "with a valid oid passed" do
162
187
  it "should cancel an order" do
163
188
  @client.cancel(929284)
164
- a_post("/code/getOrders.php").should have_been_made.once
189
+ a_post("/code/getOrders.php").
190
+ with(:body => {"name" => "my_name", "pass" => "my_password"}).
191
+ should have_been_made.once
165
192
  a_post('/code/cancelOrder.php').
166
193
  with(:body => {"name" => "my_name", "pass" => "my_password", "oid" => "929284", "type" => "2"}).
167
194
  should have_been_made
@@ -186,4 +213,18 @@ describe MtGox::Client do
186
213
  end
187
214
  end
188
215
 
216
+ describe "#withdraw!" do
217
+ before do
218
+ stub_post('/code/withdraw.php').
219
+ with(:body => {"name" => "my_name", "pass" => "my_password", "group1" => "BTC", "amount" => "1.0", "btca" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"}).
220
+ to_return(:status => 200, :body => fixture('withdraw.json'))
221
+ end
222
+
223
+ it "should withdraw funds" do
224
+ @client.withdraw!(1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L")
225
+ a_post("/code/withdraw.php").
226
+ with(:body => {"name" => "my_name", "pass" => "my_password", "group1" => "BTC", "amount" => "1.0", "btca" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"}).
227
+ should have_been_made
228
+ end
229
+ end
189
230
  end
data/spec/mtgox_spec.rb CHANGED
@@ -6,17 +6,17 @@ describe MtGox do
6
6
  MtGox.new.should be_a MtGox::Client
7
7
  end
8
8
  end
9
-
9
+
10
10
  describe ".configure" do
11
11
  it "should set 'name' and 'pass'" do
12
- MtGox.configure do |c|
13
- c.name="username"
14
- c.pass="password"
12
+ MtGox.configure do |config|
13
+ config.name = "username"
14
+ config.pass = "password"
15
15
  end
16
-
16
+
17
17
  MtGox.name.should == "username"
18
18
  MtGox.pass.should == "password"
19
19
  end
20
20
  end
21
-
21
+
22
22
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mtgox
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Erik Michaels-Ober
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-18 00:00:00 -07:00
13
+ date: 2011-06-26 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -170,14 +170,16 @@ files:
170
170
  - lib/mtgox/request.rb
171
171
  - lib/mtgox/version.rb
172
172
  - mtgox.gemspec
173
+ - spec/fixtures/balance.json
173
174
  - spec/fixtures/buy.json
174
175
  - spec/fixtures/cancel.json
175
176
  - spec/fixtures/depth.json
176
- - spec/fixtures/funds.json
177
+ - spec/fixtures/error.json
177
178
  - spec/fixtures/orders.json
178
179
  - spec/fixtures/sell.json
179
180
  - spec/fixtures/ticker.json
180
181
  - spec/fixtures/trades.json
182
+ - spec/fixtures/withdraw.json
181
183
  - spec/helper.rb
182
184
  - spec/mtgox/client_spec.rb
183
185
  - spec/mtgox_spec.rb
@@ -210,14 +212,16 @@ signing_key:
210
212
  specification_version: 3
211
213
  summary: Ruby wrapper for the Mt. Gox Trade API
212
214
  test_files:
215
+ - spec/fixtures/balance.json
213
216
  - spec/fixtures/buy.json
214
217
  - spec/fixtures/cancel.json
215
218
  - spec/fixtures/depth.json
216
- - spec/fixtures/funds.json
219
+ - spec/fixtures/error.json
217
220
  - spec/fixtures/orders.json
218
221
  - spec/fixtures/sell.json
219
222
  - spec/fixtures/ticker.json
220
223
  - spec/fixtures/trades.json
224
+ - spec/fixtures/withdraw.json
221
225
  - spec/helper.rb
222
226
  - spec/mtgox/client_spec.rb
223
227
  - spec/mtgox_spec.rb