Module: ECB::Exchange

Defined in:
lib/ecb/exchange.rb,
lib/ecb/exchange/cache.rb,
lib/ecb/exchange/errors.rb,
lib/ecb/exchange/xml_feed.rb,
lib/ecb/exchange/memory_cache.rb

Defined Under Namespace

Classes: Cache, CurrencyNotFoundError, DateNotFoundError, Error, MemoryCache, ParseError, ResponseError, XMLFeed

Constant Summary collapse

VERSION =
"0.2.1".freeze

Class Method Summary collapse

Class Method Details

.convert(amount, from:, to:, date: Date.today) ⇒ Object



10
11
12
# File 'lib/ecb/exchange.rb', line 10

def self.convert(amount, from:, to:, date: Date.today)
  amount.to_d * rate(from: from, to: to, date: date)
end

.currenciesObject



24
25
26
# File 'lib/ecb/exchange.rb', line 24

def self.currencies
  XMLFeed.rates(Date.today).keys
end

.rate(from:, to:, date: Date.today) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/ecb/exchange.rb', line 14

def self.rate(from:, to:, date: Date.today)
  rates = XMLFeed.rates(date)

  [from, to].each do |currency|
    raise CurrencyNotFoundError.new(currency) unless rates[currency]
  end

  rates[to].to_d * 1.to_d / rates[from].to_d
end