Class: Devilicious::CurrencyConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/devilicious/currency_converter.rb

Defined Under Namespace

Classes: RateExchange, YahooExchange

Class Method Summary collapse

Class Method Details

.convert(amount, from, to) ⇒ Object



7
8
9
# File 'lib/devilicious/currency_converter.rb', line 7

def self.convert(amount, from, to)
  amount * rate(from, to)
end

.rate(from, to) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/devilicious/currency_converter.rb', line 11

def self.rate from, to
  pair = [from, to].join

  if @rates[pair].nil? || @rates[pair].updated_at < Time.now - 10*60

    Log.debug "Refreshing #{pair} rate..."

    rate = begin
      RateExchange.get_rate(from, to)
    rescue => exception
      Log.warn "Could not retrieve exchange rate from RateExchange: #{exception.inspect}"

      YahooExchange.get_rate(from, to)
    end

    @rates[pair] = OpenStruct.new(
      rate: rate,
      updated_at: Time.now
    ).freeze

  end

  @rates[pair].rate
end