Class: MercadoBitcoin::TradeApi
- Inherits:
-
Object
- Object
- MercadoBitcoin::TradeApi
- Defined in:
- lib/mercado_bitcoin/trade_api.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#key ⇒ Object
Returns the value of attribute key.
-
#tonce_correction ⇒ Object
Returns the value of attribute tonce_correction.
Instance Method Summary collapse
- #base_params(method) ⇒ Object
- #base_url ⇒ Object
- #cancel_order(pair: 'btc_brl', order_id:) ⇒ Object
- #env_or_var_tonce ⇒ Object
- #get_info ⇒ Object
- #header(signature) ⇒ Object
-
#initialize(key:, code:, tonce_correction: 0) ⇒ TradeApi
constructor
A new instance of TradeApi.
-
#order_list(pair: 'btc_brl', type: nil, status: nil, from_id: nil, end_id: nil, since: nil, _end: nil) ⇒ Object
status: active, canceled, completed since and end: in Unix timestamp: Time.new.to_i.
- #post(params) ⇒ Object
- #sign(string_or_hash) ⇒ Object
- #tonce_error?(result) ⇒ Boolean (also: #get_tonce_correction)
-
#trade(pair: 'btc_brl', type:, volume:, price:) ⇒ Object
type: buy, sell.
Constructor Details
#initialize(key:, code:, tonce_correction: 0) ⇒ TradeApi
Returns a new instance of TradeApi.
9 10 11 12 13 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 9 def initialize(key:, code:, tonce_correction: 0) @key = key @code = code @tonce_correction = tonce_correction end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
7 8 9 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 7 def code @code end |
#key ⇒ Object
Returns the value of attribute key.
7 8 9 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 7 def key @key end |
#tonce_correction ⇒ Object
Returns the value of attribute tonce_correction.
7 8 9 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 7 def tonce_correction @tonce_correction end |
Instance Method Details
#base_params(method) ⇒ Object
89 90 91 92 93 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 89 def base_params(method) { method: method } end |
#base_url ⇒ Object
77 78 79 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 77 def base_url @base_url ||= "https://www.mercadobitcoin.net/tapi/".freeze end |
#cancel_order(pair: 'btc_brl', order_id:) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 30 def cancel_order(pair: 'btc_brl', order_id:) params = base_params('CancelOrder') params[:pair] = pair params[:order_id] = order_id post(params) end |
#env_or_var_tonce ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 95 def env_or_var_tonce if ENV['TONCE_CORRECTION'].to_s =~ /\d+/ ENV['TONCE_CORRECTION'].to_i else tonce_correction end end |
#get_info ⇒ Object
15 16 17 18 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 15 def get_info params = base_params('getInfo') post(params) end |
#header(signature) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 81 def header(signature) { 'Content-Type' => 'application/x-www-form-urlencoded', 'Key' => key, 'Sign' => signature } end |
#order_list(pair: 'btc_brl', type: nil, status: nil, from_id: nil, end_id: nil, since: nil, _end: nil) ⇒ Object
status: active, canceled, completed since and end: in Unix timestamp: Time.new.to_i
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 39 def order_list(pair: 'btc_brl', type: nil, status: nil, from_id: nil, end_id: nil, since: nil, _end: nil) params = base_params('OrderList') params[:pair] = pair params[:type] = type if type params[:status] = status if status params[:from_id] = from_id if from_id params[:end_id] = end_id if end_id params[:since] = since if since params[:end] = _end if _end post(params) end |
#post(params) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 51 def post(params) params[:tonce] = Time.new.to_i + env_or_var_tonce signature = sign(params) result = JSON.parse( RestClient.post( base_url, params.to_query_string, header(signature) ) ) raise TonceDesyncError.new('desync') if tonce_error?(result) result rescue TonceDesyncError @tonce_correction = get_tonce_correction(result) retry end |
#sign(string_or_hash) ⇒ Object
103 104 105 106 107 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 103 def sign(string_or_hash) string_or_hash = string_or_hash.to_query_string if string_or_hash.is_a?(Hash) hmac = OpenSSL::HMAC.new(code, OpenSSL::Digest.new('sha512')) hmac.update(string_or_hash).to_s end |
#tonce_error?(result) ⇒ Boolean Also known as: get_tonce_correction
68 69 70 71 72 73 74 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 68 def tonce_error?(result) if result['error'].to_s =~ /(\d+) e (\d+), tonce recebido (\d+)+/ $1.to_i - $3.to_i + 10 else false end end |
#trade(pair: 'btc_brl', type:, volume:, price:) ⇒ Object
type: buy, sell
21 22 23 24 25 26 27 28 |
# File 'lib/mercado_bitcoin/trade_api.rb', line 21 def trade(pair: 'btc_brl', type:, volume:, price:) params = base_params('Trade') params[:pair] = pair params[:type] = type params[:volume] = volume params[:price] = price post(params) end |