Class: Coinpare::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/coinpare/command.rb

Constant Summary collapse

SYMBOLS =
{
  down_arrow: "",
  up_arrow: ""
}.freeze
DEFAULT_INTERVAL =

The default interval for auto updating data

5

Instance Method Summary collapse

Instance Method Details

#add_color(str, color) ⇒ Object



50
51
52
# File 'lib/coinpare/command.rb', line 50

def add_color(str, color)
  @options["no-color"] || color == :none ? str : @pastel.decorate(str, color)
end

The exchange, currency & time banner



36
37
38
39
40
# File 'lib/coinpare/command.rb', line 36

def banner(settings)
  "\n#{add_color('Exchange', :yellow)} #{settings['exchange']}  " \
  "#{add_color('Currency', :yellow)} #{settings['base'].upcase}  " \
  "#{add_color('Time', :yellow)} #{timestamp}\n\n"
end

#configObject

Main configuration



17
18
19
20
21
22
23
24
25
26
# File 'lib/coinpare/command.rb', line 17

def config
  @config ||= begin
    config = TTY::Config.new
    config.filename = "coinpare"
    config.extname = ".toml"
    config.append_path Dir.pwd
    config.append_path Dir.home
    config
  end
end

#cursorObject

The cursor movement



111
112
113
114
# File 'lib/coinpare/command.rb', line 111

def cursor
  require "tty-cursor"
  TTY::Cursor
end

#editorObject

Open a file or text in the user’s preferred editor



121
122
123
124
# File 'lib/coinpare/command.rb', line 121

def editor
  require "tty-editor"
  TTY::Editor
end

#executeObject

Execute this command

Raises:

  • (NotImplementedError)


99
100
101
102
103
104
# File 'lib/coinpare/command.rb', line 99

def execute(*)
  raise(
    NotImplementedError,
    "#{self.class}##{__method__} must be implemented"
  )
end

#number_to_currency(value) ⇒ Object



90
91
92
93
94
# File 'lib/coinpare/command.rb', line 90

def number_to_currency(value)
  whole, part = value.to_s.split(".")
  part = "." + part unless part.nil?
  "#{whole.gsub(/(\d)(?=(\d{3})+(?!\d))/, '\\1,')}#{part}"
end

#percent(value) ⇒ Object



60
61
62
# File 'lib/coinpare/command.rb', line 60

def percent(value)
  (value * 100).round(2)
end

#percent_change(before, after) ⇒ Object



64
65
66
# File 'lib/coinpare/command.rb', line 64

def percent_change(before, after)
  (after - before) / before.to_f * 100
end

#pick_arrow(change) ⇒ Object

Provide arrow for marking value growth or decline



44
45
46
47
48
# File 'lib/coinpare/command.rb', line 44

def pick_arrow(change)
  return if change.zero?

  change > 0 ? SYMBOLS[:up_arrow] : SYMBOLS[:down_arrow]
end

#pick_color(change) ⇒ Object



54
55
56
57
58
# File 'lib/coinpare/command.rb', line 54

def pick_color(change)
  return :none if change.zero?

  change > 0 ? :green : :red
end

#precision(value, decimals = 2) ⇒ Object



78
79
80
81
82
83
# File 'lib/coinpare/command.rb', line 78

def precision(value, decimals = 2)
  part = value.to_s.split(".")[1]
  return 0 if part.nil?

  value.between?(0, 1) ? (part.index(/[^0]/) + decimals) : decimals
end

#round_to(value, prec = nil) ⇒ Object



85
86
87
88
# File 'lib/coinpare/command.rb', line 85

def round_to(value, prec = nil)
  prec = precision(value) if prec.nil?
  format("%.#{prec}f", value)
end

#screenObject

Get terminal screen properties



131
132
133
134
# File 'lib/coinpare/command.rb', line 131

def screen
  require "tty-screen"
  TTY::Screen
end

#shorten_currency(value) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/coinpare/command.rb', line 68

def shorten_currency(value)
  if value > 10**9
    "#{(value / 10**9).to_f.round(2)} B"
  elsif value > 10**6
    "#{(value / 10**6).to_f.round(2)} M"
  else
    value
  end
end

#timestampObject

Time for when the data was fetched



30
31
32
# File 'lib/coinpare/command.rb', line 30

def timestamp
  "#{Time.now.strftime('%d %B %Y')} at #{Time.now.strftime('%I:%M:%S %p %Z')}"
end