Class: Chg::CLI
- Inherits:
-
Object
- Object
- Chg::CLI
- Defined in:
- lib/chg/cli.rb
Overview
CLI interface
Constant Summary collapse
- MissingKey =
ArgumentError.new('Missing ALPHAVANTAGE_KEY env. var')
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #change(from, to, amount) ⇒ Object
-
#initialize(key) ⇒ CLI
constructor
A new instance of CLI.
- #rate(from, to) ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(key) ⇒ CLI
Returns a new instance of CLI.
11 12 13 |
# File 'lib/chg/cli.rb', line 11 def initialize(key) @key = key end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/chg/cli.rb', line 9 def key @key end |
Instance Method Details
#change(from, to, amount) ⇒ Object
19 20 21 22 |
# File 'lib/chg/cli.rb', line 19 def change(from, to, amount) changed = amount * rate(from, to) "#{amount} #{from.upcase} = #{changed} #{to.upcase}" end |
#rate(from, to) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/chg/cli.rb', line 24 def rate(from, to) raise MissingKey unless key xchg = Alphavantage::Exchange.new(from: from, to: to, key: key).now xchg.exchange_rate.to_f end |
#usage ⇒ Object
15 16 17 |
# File 'lib/chg/cli.rb', line 15 def usage "Usage: #{program} <from> <to> [amount = 1]" end |