lib/capwatch/console.rb in capwatch-0.1.13 vs lib/capwatch/console.rb in capwatch-0.2.0
- old
+ new
@@ -1,91 +1,113 @@
+# frozen_string_literal: true
+
module Capwatch
class Console
- def self.format_table(hash)
- hash[:table].each do |x|
- x[1] = format_usd(x[1])
- x[3] = format_usd(x[3])
- x[4] = format_btc(x[4])
- x[5] = format_eth(x[5])
- x[6] = format_percent(x[6])
- x[7] = format_percent(x[7])
- x[8] = format_percent(x[8])
- end
- hash[:footer][3] = format_usd(hash[:footer][3])
- hash[:footer][4] = format_btc(hash[:footer][4])
- hash[:footer][5] = format_eth(hash[:footer][5])
- hash[:footer][7] = format_percent(hash[:footer][7])
- hash[:footer][8] = format_percent(hash[:footer][8])
- hash
+
+ attr_accessor :name, :body, :totals
+
+ def initialize(name, body, totals)
+ @name = name
+ @body = format_body(body)
+ @totals = format_totals(totals)
end
- def self.colorize_table(hash)
- hash[:table].each do |x|
- x[7] = condition_color(x[7])
- x[8] = condition_color(x[8])
+ 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
- hash[:footer][7] = condition_color(hash[:footer][7])
- hash[:footer][8] = condition_color(hash[:footer][8])
- hash
end
- def self.draw_table(hash)
- hash = colorize_table(format_table(hash))
- table = Terminal::Table.new do |t|
- t.title = hash[:title].upcase
+ 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
+
+ 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: '',
+ border_y: "",
+ border_i: "",
padding_left: 1,
padding_right: 1
}
t.headings = [
- 'ASSET',
- 'PRICE',
- 'QUANTITY',
- 'VALUE (USD)',
- 'VALUE (BTC)',
- 'VALUE (ETH)',
- 'DIST %',
- '24H %',
- '7D %'
+ "ASSET",
+ "PRICE",
+ "QUANTITY",
+ "DIST %",
+ "BTC",
+ "ETH",
+ "24H %",
+ "7D %",
+ "USD"
]
- hash[:table].each do |x|
+ body.each do |x|
t << x
end
t.add_separator
- t.add_row hash[:footer]
+ t.add_row totals
end
table
end
- def self.format_usd(n)
- '$' + n.round(2).to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
- end
- def self.format_btc(value)
- format('฿%.2f', value)
- end
+ class Formatter
- def self.format_eth(value)
- format('Ξ%.2f', value)
- end
+ class << self
- def self.format_percent(value)
- format('%.2f%', value)
- end
+ def format_usd(n)
+ "$" + n.to_f.round(2).to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
+ end
- def self.condition_color(value)
- percent_value = value.to_f
- if percent_value > 1
- value.green
- elsif percent_value < 0
- value.red
- else
- value.green
+ def format_btc(value)
+ format("฿%.2f", value)
+ end
+
+ def format_eth(value)
+ format("Ξ%.2f", value)
+ end
+
+ def format_percent(value)
+ format("%.2f%", value.to_f)
+ end
+
+ def condition_color(value)
+ percent_value = value.to_f
+ if percent_value > 1
+ value.green
+ elsif percent_value < 0
+ value.red
+ else
+ value.green
+ end
+ end
+
end
- end
+
+ end # class Formatter
+
end
end