Class: MercadoBitcoin::Console

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

Defined Under Namespace

Modules: Commands Classes: CommandParse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Console

Returns a new instance of Console.



4
5
6
# File 'lib/mercado_bitcoin/console.rb', line 4

def initialize(opts = {})
  @options = default_options.merge(opts)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/mercado_bitcoin/console.rb', line 2

def options
  @options
end

Instance Method Details

#cancel_order(*args) ⇒ Object

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mercado_bitcoin/console.rb', line 92

def cancel_order(*args)
  raise ArgumentError.new("faltando ORDER_ID") if args.count < 1
  ret = args.map do |id|
    trade_api.cancel_order(coin_pair: options[:coin_pair], order_id: id)
  end
  if ret.size > 1
    ret
  else
    ret[0]
  end
end

#default_optionsObject



16
17
18
19
20
21
22
23
24
# File 'lib/mercado_bitcoin/console.rb', line 16

def default_options
  @options ||= {
    key: ENV['MB_API_KEY'],
    code: ENV['MB_SECRET_KEY'],
    coin_pair: ENV['MB_COIN_PAIR'] || MercadoBitcoin::TradeApi::BTC,
    pretty_print: true,
    status_list: '2'
  }
end

#exec(command, opts) ⇒ Object



26
27
28
# File 'lib/mercado_bitcoin/console.rb', line 26

def exec(command, opts)
  print send(command, *opts)
end

#get_account_info(*args) ⇒ Object



41
42
43
# File 'lib/mercado_bitcoin/console.rb', line 41

def (*args)
  trade_api.
end

#get_order(*args) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mercado_bitcoin/console.rb', line 45

def get_order(*args)
  raise ArgumentError.new("faltando ORDER_ID") if args.count < 1
  ret = args.map do |id|
    trade_api.get_order(order_id: id)
  end
  if ret.size > 1
    ret
  else
    ret[0]
  end
end

#get_withdrawal(*args) ⇒ Object

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mercado_bitcoin/console.rb', line 104

def get_withdrawal(*args)
  raise ArgumentError.new("faltando withdrawal_id") if args.count < 1
  ret = args.map do |id|
    trade_api.get_withdrawal(coin_pair: params[:coin_pair], withdrawal_id: id)
  end
  if ret.size > 1
    ret
  else
    ret[0]
  end
end

#list_orderbook(*args) ⇒ Object



69
70
71
72
73
74
# File 'lib/mercado_bitcoin/console.rb', line 69

def list_orderbook(*args)
  trade_api.list_orderbook(
    coin_pair: options[:coin_pair],
    full: options[:full]
  )
end

#list_orders(*args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mercado_bitcoin/console.rb', line 57

def list_orders(*args)
  trade_api.list_orders(
    coin_pair: options[:coin_pair],
    order_type: options[:order_type],
    status_list: options[:status_list],
    has_fills: options[:has_fills],
    from_id: options[:from_id],
    to_id: options[:to_id],
    from_timestamp: options[:from_timestamp],
    to_timestamp: options[:to_timestamp])
end

#list_system_messages(*args) ⇒ Object



37
38
39
# File 'lib/mercado_bitcoin/console.rb', line 37

def list_system_messages(*args)
  trade_api.list_system_messages
end

#option_parserObject



12
13
14
# File 'lib/mercado_bitcoin/console.rb', line 12

def option_parser
  @option_parser ||= MercadoBitcoin::Console::CommandParse.new
end

#place_buy_order(*args) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/mercado_bitcoin/console.rb', line 76

def place_buy_order(*args)
  trade_api.place_buy_order(
    coin_pair: options[:coin_pair],
    quantity: options[:quantity],
    limit_price: options[:limit_price]
  )
end

#place_sell_order(*args) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/mercado_bitcoin/console.rb', line 84

def place_sell_order(*args)
  trade_api.place_sell_order(
    coin_pair: options[:coin_pair],
    quantity: options[:quantity],
    limit_price: options[:limit_price]
  )
end

#run!Object



8
9
10
# File 'lib/mercado_bitcoin/console.rb', line 8

def run!
  option_parser.parse(self)
end

#ticker(*args) ⇒ Object



30
31
32
33
34
35
# File 'lib/mercado_bitcoin/console.rb', line 30

def ticker(*args)
  MercadoBitcoin::Ticker
    .new(options[:coin_pair] == MercadoBitcoin::TradeApi::BTC ? :bitcoin : :litecoin)
    .tap { |t| t.fetch }
    .parsed
end

#withdrawal_coin(*args) ⇒ Object



116
117
118
119
120
# File 'lib/mercado_bitcoin/console.rb', line 116

def withdrawal_coin(*args)
  prms = params.dup
  prms = params.merge(args.first) if args.count == 1 and args.first.is_a?(Hash)
  trade_api.withdrawal_coin(**prms)
end