Class: Coinstack::Interface

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

Overview

Defines functions the user directly interacts with.

Constant Summary collapse

ROW_LENGTH =
24

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterface

Returns a new instance of Interface.



11
12
13
14
# File 'lib/coinstack.rb', line 11

def initialize
  self.list = List.new
  self.cli = HighLine.new
end

Instance Attribute Details

#cliObject

TODO: Colorscheming, more prettifiers



9
10
11
# File 'lib/coinstack.rb', line 9

def cli
  @cli
end

#listObject

TODO: Colorscheming, more prettifiers



9
10
11
# File 'lib/coinstack.rb', line 9

def list
  @list
end

Instance Method Details

#pretty_print(data) ⇒ Object

Data should be an array of hashes, data gets printed first and so on



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/coinstack.rb', line 68

def pretty_print(data)
  data.each do |datum|
    if datum.empty?
      cli.say('-' * ROW_LENGTH)
    else
      datum.each do |key, value|
        info_length = "#{key} #{value}".length
        buffer = ' ' * [ROW_LENGTH - info_length, 0].max
        row = key.to_s + buffer + '$' + value.to_s

        cli.say(row)
      end
    end
  end
end

#pretty_print_listObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/coinstack.rb', line 28

def pretty_print_list
  total = 0
  data = []
  list.user_pairs.each do |symbol, amount|
    value = (list.pairs[symbol.to_s]['price_usd'].to_f * amount).round(2)
    data.push(symbol => value)
    total += value
  end

  cli.say('Your Portfolio:')
  data.push({}, { TOTAL: total.round(2).to_s }, {})
  pretty_print(data)
end

#prompt_add_assetObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/coinstack.rb', line 42

def prompt_add_asset
  to_add = { amount: nil, symbol: nil }

  loop do
    break if to_add[:symbol] == 'DONE'
    if list.pairs[to_add[:symbol]] && to_add[:amount]
      list.add(to_add)
      to_add = { amount: nil, symbol: nil }
    elsif list.pairs[to_add[:symbol]]
      to_add[:amount] = cli.ask("Type the amount of #{to_add[:symbol]} you have.", Float)
    else
      to_add[:symbol] = cli.ask('Type the symbol of the asset to add, or type "done"').upcase
    end
  end
  start
end

#prompt_remove_assetObject



59
60
61
62
63
# File 'lib/coinstack.rb', line 59

def prompt_remove_asset
  symbol = cli.ask('Which symbol would you like removed?').upcase
  list.remove(symbol)
  start
end

#startObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/coinstack.rb', line 16

def start
  pretty_print_list if list.user_pairs.any?

  loop do
    cli.choose do |menu|
      menu.choice('Add an asset') { prompt_add_asset }
      menu.choice('Remove an asset') { prompt_remove_asset } # TODO
      menu.choice('Exit') { cli.say('Goodbye!') || exit }
    end
  end
end