class Capwatch::Console

Attributes

body[RW]
name[RW]
totals[RW]

Public Class Methods

new(name, body, totals) click to toggle source
# File lib/capwatch/console.rb, line 8
def initialize(name, body, totals)
  @name   = name
  @body   = format_body(body)
  @totals = format_totals(totals)
end

Public Instance Methods

draw_table() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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