Class: Coinpare::Commands::Markets

Inherits:
Coinpare::Command show all
Defined in:
lib/coinpare/commands/markets.rb

Constant Summary

Constants inherited from Coinpare::Command

Coinpare::Command::DEFAULT_INTERVAL, Coinpare::Command::SYMBOLS

Instance Method Summary collapse

Methods inherited from Coinpare::Command

#add_color, #config, #cursor, #editor, #number_to_currency, #percent, #percent_change, #pick_arrow, #pick_color, #precision, #round_to, #screen, #shorten_currency, #timestamp

Constructor Details

#initialize(name, options) ⇒ Markets

Returns a new instance of Markets.



15
16
17
18
19
20
21
22
# File 'lib/coinpare/commands/markets.rb', line 15

def initialize(name, options)
  @name = name
  @options = options
  @pastel = Pastel.new
  @timers = Timers::Group.new
  @spinner = TTY::Spinner.new(":spinner Fetching data...",
                              format: :dots, clear: true)
end

Instance Method Details



82
83
84
85
86
# File 'lib/coinpare/commands/markets.rb', line 82

def banner
  "\n#{add_color('Coin', :yellow)} #{@name.upcase}  " \
  "#{add_color('Base Currency', :yellow)} #{@options['base'].upcase}  " \
  "#{add_color('Time', :yellow)} #{timestamp}\n\n"
end

#clear_output(output, lines) ⇒ Object



57
58
59
60
61
# File 'lib/coinpare/commands/markets.rb', line 57

def clear_output(output, lines)
  output.print cursor.clear_screen_down if @options["watch"]
  yield if block_given?
  output.print cursor.up(lines) if @options["watch"]
end

#display_markets(output, pager) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/coinpare/commands/markets.rb', line 45

def display_markets(output, pager)
  to_symbol = fetch_symbol
  response = Fetcher.fetch_top_exchanges_by_pair(
               @name.upcase, @options["base"].upcase, @options)
  return unless response
  table = setup_table(response["Data"]["Exchanges"], to_symbol)

  lines = banner.lines.size + 1 + table.rows_size + 3
  @spinner.stop
  clear_output(output, lines) { print_results(table, output, pager) }
end

#execute(input: $stdin, output: $stdout) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/coinpare/commands/markets.rb', line 24

def execute(input: $stdin, output: $stdout)
  pager = TTY::Pager.new(output: output)
  @spinner.auto_spin

  if @options["watch"]
    output.print cursor.hide
    interval = @options["watch"].to_f > 0 ? @options["watch"].to_f : DEFAULT_INTERVAL
    @timers.now_and_every(interval) { display_markets(output, pager) }
    loop { @timers.wait }
  else
    display_markets(output, pager)
  end
ensure
  @spinner.stop
  if @options["watch"]
    @timers.cancel
    output.print cursor.clear_screen_down
    output.print cursor.show
  end
end

#fetch_symbolObject



75
76
77
78
79
80
# File 'lib/coinpare/commands/markets.rb', line 75

def fetch_symbol
  prices = Fetcher.fetch_prices(@name.upcase, @options["base"].upcase, @options)
  return unless prices

  prices["DISPLAY"][@name.upcase][@options["base"].upcase]["TOSYMBOL"]
end


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/coinpare/commands/markets.rb', line 63

def print_results(table, output, pager)
  output.puts banner
  lines = banner.lines.size + 1 + (table.rows_size + 3)
  rendered = table.render(:unicode, padding: [0, 1], alignment: :right)
  if lines >= screen.height
    pager.page rendered
  else
    output.print rendered
  end
  output.puts
end

#setup_table(data, to_symbol) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/coinpare/commands/markets.rb', line 88

def setup_table(data, to_symbol)
  table = TTY::Table.new(header: [
    { value: "Market", alignment: :left },
    "Price",
    "Chg. 24H",
    "Chg.% 24H",
    "Open 24H",
    "High 24H",
    "Low 24H",
    "Direct Vol. 24H"
  ])

  data.each do |market|
    change24h = market["CHANGE24HOUR"]
    market_details = [
      { value: add_color(market["MARKET"], :yellow), alignment: :left },
      add_color("#{to_symbol} #{number_to_currency(round_to(market['PRICE']))}", pick_color(change24h)),
      add_color("#{pick_arrow(change24h)} #{to_symbol} #{number_to_currency(round_to(change24h))}", pick_color(change24h)),
      add_color("#{pick_arrow(change24h)} #{round_to(market['CHANGEPCT24HOUR'] * 100)}%", pick_color(change24h)),
      "#{to_symbol} #{number_to_currency(round_to(market['OPEN24HOUR']))}",
      "#{to_symbol} #{number_to_currency(round_to(market['HIGH24HOUR']))}",
      "#{to_symbol} #{number_to_currency(round_to(market['LOW24HOUR']))}",
      "#{to_symbol} #{number_to_currency(round_to(market['VOLUME24HOURTO']))}"
    ]
    table << market_details
  end

  table
end