Module: Lbank

Defined in:
lib/lbank.rb,
lib/lbank/version.rb

Constant Summary collapse

BASE_CURRENCY =
'LTL'
SOURCE =
'http://lbank.lt/exchange/Results.asp'
VERSION =
"0.0.1"
@@cache =
{}

Class Method Summary collapse

Class Method Details

.convert_currency(amount, from_currency, to_currency, date = nil) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/lbank.rb', line 30

def self.convert_currency(amount, from_currency, to_currency,  date = nil)
  rates     = self.currency_rates(date)

  from_rate = rates[from_currency.to_s]
  to_rate   = rates[to_currency.to_s]

  amount / from_rate * to_rate
end

.currency_rates(date = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lbank.rb', line 11

def self.currency_rates(date = nil)
  date  ||= Date.today

  if @@cache[date].nil?
    url   = "#{SOURCE}?Y=%i&M=%i&D=%i&S=csv" % [ date.year, date.month, date.day ]
    rates = {}

    CSV.parse(open(url)).each do |row|
      rates[row[1]] = row[2].to_i / row[3].to_f
    end

    rates[BASE_CURRENCY] = 1.0

    @@cache[date] = rates
  end

  @@cache[date]
end