Class: Yahoofx::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoofx/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(rate_class = Yahoofx::Pair) ⇒ Converter

Returns a new instance of Converter.



3
4
5
# File 'lib/yahoofx/converter.rb', line 3

def initialize(rate_class = Yahoofx::Pair)
  @rate_class = rate_class
end

Instance Method Details

#answer(p) ⇒ Object

Either returns a currency rate or converts an amount from one currency to another. Example to convert currency: “119.99 EUR in USD” Example to show currency rate: “EURUSD”



10
11
12
13
14
15
16
# File 'lib/yahoofx/converter.rb', line 10

def answer(p)
  if p.length==6
    return @rate_class.new(p[0..2], p[3..5]).bid
  else
    convert p
  end
end

#convert(input_string) ⇒ Object



18
19
20
21
# File 'lib/yahoofx/converter.rb', line 18

def convert(input_string)
  parsed = parse(input_string)
  @rate_class.new(parsed[:from_currency], parsed[:to_currency]).bid*parsed[:from_amount]
end

#parse(input_string) ⇒ Object

Example parameter: “100 EUR in USD”



24
25
26
27
28
29
# File 'lib/yahoofx/converter.rb', line 24

def parse(input_string)
  /([\d\.]+)[\p{Space}]*(.*)in(.*)/ =~ input_string
  { :from_amount => $1.to_f,
    :from_currency => $2.strip,
    :to_currency => $3.strip }
end