Class: MtGox::Client
- Inherits:
-
Object
- Object
- MtGox::Client
- Includes:
- Connection, Request
- Defined in:
- lib/mtgox/client.rb
Constant Summary collapse
- ORDER_TYPES =
{:sell => 1, :buy => 2}
Instance Method Summary collapse
-
#asks ⇒ Array<Array<Numeric>>
Fetch open asks.
-
#balance ⇒ Hashie::Rash
Fetch your balance.
-
#bids ⇒ Array<Array<Numeric>>
Fetch open bids.
-
#buy!(amount, price) ⇒ Array<Hashie::Rash>
Place a limit order to buy BTC.
-
#buys ⇒ Array<Hashie::Rash>
Fetch your open buys.
-
#cancel(args) ⇒ Object
Cancel an open order.
-
#offers ⇒ Hashie::Rash
Fetch both bids and asks in one call, for network efficiency.
-
#orders ⇒ <Hashie::Rash>
Fetch your open orders, both buys and sells, for network efficiency.
-
#sell!(amount, price) ⇒ Array<Hashie::Rash>
Place a limit order to sell BTC.
-
#sells ⇒ Array<Hashie::Rash>
Fetch your open sells.
-
#ticker ⇒ Hashie::Rash
Fetch the latest ticker data.
-
#trades ⇒ Array<Hashie::Rash>
Fetch recent trades.
-
#withdraw!(amount, btca) ⇒ Array<Hashie::Rash>
Transfer bitcoins from your Mt.
Methods included from Request
Instance Method Details
#asks ⇒ Array<Array<Numeric>>
Fetch open asks
45 46 47 |
# File 'lib/mtgox/client.rb', line 45 def asks offers['asks'] end |
#balance ⇒ Hashie::Rash
Fetch your balance
79 80 81 |
# File 'lib/mtgox/client.rb', line 79 def balance post('/code/getFunds.php', pass_params) end |
#bids ⇒ Array<Array<Numeric>>
Fetch open bids
55 56 57 |
# File 'lib/mtgox/client.rb', line 55 def bids offers['bids'] end |
#buy!(amount, price) ⇒ Array<Hashie::Rash>
Place a limit order to buy BTC
126 127 128 |
# File 'lib/mtgox/client.rb', line 126 def buy!(amount, price) parse_orders(post('/code/buyBTC.php', pass_params.merge({:amount => amount, :price => price}))['orders']) end |
#buys ⇒ Array<Hashie::Rash>
Fetch your open buys
99 100 101 102 103 |
# File 'lib/mtgox/client.rb', line 99 def buys orders.select do |o| o['type'] == ORDER_TYPES[:buy] end end |
#cancel(oid) ⇒ Object #cancel(order) ⇒ Object
Cancel an open order
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/mtgox/client.rb', line 160 def cancel(args) if args.is_a?(Hash) order = args.delete_if{|k, v| !['oid', 'type'].include?(k.to_s)} post('/code/cancelOrder.php', pass_params.merge(order)) else order = orders.select{|o| o['oid'] == args.to_s}.first if order order = order.delete_if{|k, v| !['oid', 'type'].include?(k.to_s)} post('/code/cancelOrder.php', pass_params.merge(order)) else raise Faraday::Error::ResourceNotFound, {:status => 404, :headers => {}, :body => "Order not found."} end end end |
#offers ⇒ Hashie::Rash
Fetch both bids and asks in one call, for network efficiency
32 33 34 35 36 37 |
# File 'lib/mtgox/client.rb', line 32 def offers offers = get('/code/data/getDepth.php') offers['asks'] = offers['asks'].sort_by{|a| a[0]} offers['bids'] = offers['bids'].sort_by{|b| b[0]}.reverse offers end |
#orders ⇒ <Hashie::Rash>
Fetch your open orders, both buys and sells, for network efficiency
89 90 91 |
# File 'lib/mtgox/client.rb', line 89 def orders parse_orders(post('/code/getOrders.php', pass_params)['orders']) end |
#sell!(amount, price) ⇒ Array<Hashie::Rash>
Place a limit order to sell BTC
139 140 141 |
# File 'lib/mtgox/client.rb', line 139 def sell!(amount, price) parse_orders(post('/code/sellBTC.php', pass_params.merge({:amount => amount, :price => price}))['orders']) end |
#sells ⇒ Array<Hashie::Rash>
Fetch your open sells
111 112 113 114 115 |
# File 'lib/mtgox/client.rb', line 111 def sells orders.select do |o| o['type'] == ORDER_TYPES[:sell] end end |
#ticker ⇒ Hashie::Rash
Fetch the latest ticker data
20 21 22 |
# File 'lib/mtgox/client.rb', line 20 def ticker get('/code/data/ticker.php')['ticker'] end |
#trades ⇒ Array<Hashie::Rash>
Fetch recent trades
65 66 67 68 69 70 71 |
# File 'lib/mtgox/client.rb', line 65 def trades get('/code/data/getTrades.php').each do |trade| trade['amount'] = trade['amount'].to_f trade['date'] = Time.at(trade['date']) trade['price'] = trade['price'].to_f end end |
#withdraw!(amount, btca) ⇒ Array<Hashie::Rash>
Transfer bitcoins from your Mt. Gox account into another account
184 185 186 |
# File 'lib/mtgox/client.rb', line 184 def withdraw!(amount, btca) post('/code/withdraw.php', pass_params.merge({:group1 => "BTC", :amount => amount, :btca => btca})) end |