Class: Capwatch::Console

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

Defined Under Namespace

Classes: Formatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, body, totals) ⇒ Console



8
9
10
11
12
# File 'lib/capwatch/console.rb', line 8

def initialize(name, body, totals)
  @name   = name
  @body   = format_body(body)
  @totals = format_totals(totals)
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/capwatch/console.rb', line 6

def body
  @body
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/capwatch/console.rb', line 6

def name
  @name
end

#totalsObject

Returns the value of attribute totals.



6
7
8
# File 'lib/capwatch/console.rb', line 6

def totals
  @totals
end

Instance Method Details

#draw_tableObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/capwatch/console.rb', line 44

def draw_table
  table  = Terminal::Table.new do |t|
    t.title = name.upcase
    t.style = {
      border_top: false,
      border_bottom: false,
      border_y: "",
      border_i: "",
      padding_left: 1,
      padding_right: 1
    }
    t.headings = [
      "ASSET",
      "PRICE",
      "QUANTITY",
      "DIST %",
      "BTC",
      "ETH",
      "24H %",
      "7D %",
      "USD"
    ]
    body.each do |x|
      t << x
    end
    t.add_separator
    t.add_row totals
  end

  table
end

#format_body(body) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/capwatch/console.rb', line 14

def format_body(body)
  JSON.parse(body).sort_by! { |e| -e["value_btc"].to_f }.map do |coin|
    [
      coin["name"],
      Formatter.format_usd(coin["price_usd"]),
      coin["quantity"],
      Formatter.format_percent(coin["distribution"].to_f * 100),
      Formatter.format_btc(coin["value_btc"]),
      Formatter.format_eth(coin["value_eth"]),
      Formatter.condition_color(Formatter.format_percent(coin["percent_change_24h"])),
      Formatter.condition_color(Formatter.format_percent(coin["percent_change_7d"])),
      Formatter.format_usd(coin["value_usd"])
    ]
  end
end

#format_totals(totals) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/capwatch/console.rb', line 30

def format_totals(totals)
  [
    "",
    "",
    "",
    "",
    Formatter.format_btc(totals[:value_btc]),
    Formatter.format_eth(totals[:value_eth]),
    Formatter.condition_color(Formatter.format_percent(totals[:percent_change_24h])),
    Formatter.condition_color(Formatter.format_percent(totals[:percent_change_7d])),
    Formatter.format_usd(totals[:value_usd]).bold
  ]
end