Class: MercadoBitcoin::TradeApi

Inherits:
Object
  • Object
show all
Defined in:
lib/mercado_bitcoin/trade_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#codeObject

Returns the value of attribute code.



7
8
9
# File 'lib/mercado_bitcoin/trade_api.rb', line 7

def code
  @code
end

#keyObject

Returns the value of attribute key.



7
8
9
# File 'lib/mercado_bitcoin/trade_api.rb', line 7

def key
  @key
end

#tonce_correctionObject

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



74
75
76
77
78
79
# File 'lib/mercado_bitcoin/trade_api.rb', line 74

def base_params(method)
  {
    method: method,
    tonce: Time.new.to_i + (ENV['TONCE_CORRECTION'] || tonce_correction).to_i
  }
end

#base_urlObject



62
63
64
# File 'lib/mercado_bitcoin/trade_api.rb', line 62

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

#get_infoObject



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



66
67
68
69
70
71
72
# File 'lib/mercado_bitcoin/trade_api.rb', line 66

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
# File 'lib/mercado_bitcoin/trade_api.rb', line 51

def post(params)
  signature = sign(params)
  JSON.parse(
    RestClient.post(
      base_url,
      params.to_query_string,
      header(signature)
    )
  )
end

#sign(string_or_hash) ⇒ Object



81
82
83
84
85
# File 'lib/mercado_bitcoin/trade_api.rb', line 81

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

#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