class Hodlmoon::Table

Public Class Methods

build(info, currency) click to toggle source
# File lib/hodlmoon/table.rb, line 5
def self.build(info, currency)
  new(info, currency).build
end
new(info, currency) click to toggle source
# File lib/hodlmoon/table.rb, line 9
def initialize(info, currency)
  @info = info
  @currency = currency
end

Public Instance Methods

build() click to toggle source
# File lib/hodlmoon/table.rb, line 14
def build
  Terminal::Table.new do |t|
    t.title =  '☾   ☾   ☾   Hodlmoon   ☽   ☽   ☽'
    t.headings = humanised_headings
    t.rows = rows
    t.style = { all_separators: true, border_x: '=', border_i: 'O', alignment: :center }
  end
end

Private Instance Methods

colorise(key, value) click to toggle source
# File lib/hodlmoon/table.rb, line 58
def colorise(key, value)
  return unless value

  case key
  when /price_.*/ then colorise_yellow(value)
  when /percent_change_.*/ then colorise_red_or_green(value)
  end
end
colorise_green(string) click to toggle source
# File lib/hodlmoon/table.rb, line 79
def colorise_green(string)
  string.prepend("\e[32m+").concat("\e[0m")
end
colorise_red(string) click to toggle source
# File lib/hodlmoon/table.rb, line 75
def colorise_red(string)
  string.prepend("\e[31m").concat("\e[0m")
end
colorise_red_or_green(string) click to toggle source
# File lib/hodlmoon/table.rb, line 71
def colorise_red_or_green(string)
  string.include?('-') ? colorise_red(string) : colorise_green(string)
end
colorise_yellow(string) click to toggle source
# File lib/hodlmoon/table.rb, line 67
def colorise_yellow(string)
  string.prepend("\e[33m").concat("\e[0m")
end
colorised_info() click to toggle source
# File lib/hodlmoon/table.rb, line 46
def colorised_info
  filtered_info.map do |coin|
    coin.each_pair(&method(:colorise))
  end
end
filtered_info() click to toggle source
# File lib/hodlmoon/table.rb, line 52
def filtered_info
  @info.map do |coin|
    headings.each_with_object({}) { |k, h| h[k] = coin[k]; }
  end
end
headings() click to toggle source
# File lib/hodlmoon/table.rb, line 25
def headings
  ['rank',
   'name',
   'symbol',
   "price_#{@currency}",
   "market_cap_#{@currency}",
   'percent_change_1h',
   'percent_change_24h',
   'percent_change_7d']
end
humanised_headings() click to toggle source
# File lib/hodlmoon/table.rb, line 36
def humanised_headings
  headings.map do |heading|
    heading.tr('_', ' ').capitalize
  end
end
rows() click to toggle source
# File lib/hodlmoon/table.rb, line 42
def rows
  colorised_info.map(&:values)
end