Class: MtGox::Client
- Inherits:
-
Object
- Object
- MtGox::Client
- Includes:
- Connection, Models, Request
- Defined in:
- lib/mtgox/client.rb
Constant Summary collapse
- ORDER_TYPES =
{:sell => 1, :buy => 2}
- @@ticker =
{}
Instance Method Summary collapse
-
#address ⇒ String
Fetch a deposit address.
-
#asks ⇒ Array<MtGox::Ask>
Fetch open asks.
-
#balance ⇒ Array<MtGox::Balance>
Fetch your current balance.
-
#bids ⇒ Array<MtGox::Bid>
Fetch open bids.
-
#buy!(amount, price) ⇒ Hash
Place a limit order to buy BTC.
-
#buys ⇒ Array<MtGox::Buy>
Fetch your open buys.
-
#cancel(args) ⇒ Object
Cancel an open order.
-
#max_bid ⇒ MtGox::MinBid
Fetch the highest priced bid.
-
#min_ask ⇒ MtGox::MinAsk
Fetch the lowest priced ask.
-
#offers ⇒ Hash
Fetch both bids and asks in one call, for network efficiency.
-
#orders ⇒ Hash
Fetch your open orders, both buys and sells, for network efficiency.
-
#sell!(amount, price) ⇒ Hash
Place a limit order to sell BTC.
-
#sells ⇒ Array<MtGox::Sell>
Fetch your open sells.
-
#ticker(currency = nil) ⇒ MtGox::Ticker
Fetch the latest ticker data.
-
#trades ⇒ Array<MtGox::Trade>
Fetch recent trades.
-
#withdraw!(amount, btca) ⇒ Array<MtGox::Balance>
Transfer bitcoins from your Mt.
Methods included from Request
Instance Method Details
#address ⇒ String
Fetch a deposit address
20 21 22 |
# File 'lib/mtgox/client.rb', line 20 def address post('/api/0/btcAddress.php')['addr'] end |
#asks ⇒ Array<MtGox::Ask>
Fetch open asks
72 73 74 |
# File 'lib/mtgox/client.rb', line 72 def asks offers[:asks] end |
#balance ⇒ Array<MtGox::Balance>
Fetch your current balance
126 127 128 129 130 131 132 |
# File 'lib/mtgox/client.rb', line 126 def balance parse_balance(post('/api/0/getFunds.php', {})) #info = post('/code/info.php', pass_params) #info['Wallets'].values.map { |v| v['Balance'] }.map do |balance_info| # Balance.new(balance_info['currency'], balance_info['value']) #end end |
#bids ⇒ Array<MtGox::Bid>
Fetch open bids
82 83 84 |
# File 'lib/mtgox/client.rb', line 82 def bids offers[:bids] end |
#buy!(amount, price) ⇒ Hash
Place a limit order to buy BTC
173 174 175 |
# File 'lib/mtgox/client.rb', line 173 def buy!(amount, price) parse_orders(post('/api/0/buyBTC.php', {:amount => amount, :price => price})['orders']) end |
#buys ⇒ Array<MtGox::Buy>
Fetch your open buys
150 151 152 |
# File 'lib/mtgox/client.rb', line 150 def buys orders[:buys] end |
#cancel(oid) ⇒ Hash #cancel(order) ⇒ Hash
Cancel an open order
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/mtgox/client.rb', line 207 def cancel(args) if args.is_a?(Hash) order = args.delete_if { |k, v| !['oid', 'type'].include?(k.to_s) } parse_orders(post('/api/0/cancelOrder.php', order)['orders']) else orders = post('/api/0/getOrders.php', {})['orders'] order = orders.find { |order| order['oid'] == args.to_s } if order order = order.delete_if { |k, v| !['oid', 'type'].include?(k.to_s) } parse_orders(post('/api/0/cancelOrder.php', order)['orders']) else raise Faraday::Error::ResourceNotFound, {:status => 404, :headers => {}, :body => 'Order not found.'} end end end |
#max_bid ⇒ MtGox::MinBid
Fetch the highest priced bid
103 104 105 106 |
# File 'lib/mtgox/client.rb', line 103 def max_bid MaxBid.instance.set_attributes bids.first.attributes MaxBid.instance end |
#min_ask ⇒ MtGox::MinAsk
Fetch the lowest priced ask
92 93 94 95 |
# File 'lib/mtgox/client.rb', line 92 def min_ask MinAsk.instance.set_attributes asks.first.attributes MinAsk.instance end |
#offers ⇒ Hash
Fetch both bids and asks in one call, for network efficiency
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mtgox/client.rb', line 51 def offers offers = get('/api/0/data/getDepth.php') asks = offers['asks'].sort_by do |ask| ask[0].to_f end.map! do |ask| Ask.new(*ask) end bids = offers['bids'].sort_by do |bid| -bid[0].to_f end.map! do |bid| Bid.new(*bid) end {:asks => asks, :bids => bids} end |
#orders ⇒ Hash
Fetch your open orders, both buys and sells, for network efficiency
140 141 142 |
# File 'lib/mtgox/client.rb', line 140 def orders parse_orders(post('/api/0/getOrders.php', {})['orders']) end |
#sell!(amount, price) ⇒ Hash
Place a limit order to sell BTC
186 187 188 |
# File 'lib/mtgox/client.rb', line 186 def sell!(amount, price) parse_orders(post('/api/0/sellBTC.php', {:amount => amount, :price => price})['orders']) end |
#sells ⇒ Array<MtGox::Sell>
Fetch your open sells
160 161 162 |
# File 'lib/mtgox/client.rb', line 160 def sells orders[:sells] end |
#ticker(currency = nil) ⇒ MtGox::Ticker
Fetch the latest ticker data
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mtgox/client.rb', line 32 def ticker(currency=nil) if currency ticker = get("/api/1/BTC#{currency}/public/ticker")['return'] @@ticker[currency] ||= MultiTicker.new :currency => currency @@ticker[currency].set_attributes ticker @@ticker[currency] else ticker = get('/api/0/data/ticker.php')['ticker'] Ticker.instance.set_attributes ticker Ticker.instance end end |
#trades ⇒ Array<MtGox::Trade>
Fetch recent trades
114 115 116 117 118 |
# File 'lib/mtgox/client.rb', line 114 def trades get('/api/0/data/getTrades.php').sort_by { |trade| trade['date'] }.map do |trade| Trade.new(trade) end end |
#withdraw!(amount, btca) ⇒ Array<MtGox::Balance>
Transfer bitcoins from your Mt. Gox account into another account
232 233 234 |
# File 'lib/mtgox/client.rb', line 232 def withdraw!(amount, btca) parse_balance(post('/api/0/withdraw.php', {:group1 => 'BTC', :amount => amount, :btca => btca})) end |