Class: Chg::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/chg/cli.rb

Overview

CLI interface

Constant Summary collapse

MissingKey =
ArgumentError.new('Missing ALPHAVANTAGE_KEY env. var')

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#keyObject (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

Raises:



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

#usageObject



15
16
17
# File 'lib/chg/cli.rb', line 15

def usage
  "Usage: #{program} <from> <to> [amount = 1]"
end