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.1.0".freeze

Class Method Summary collapse

Class Method Details

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


8
9
10
# File 'lib/ecb/exchange.rb', line 8

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

.currenciesObject


22
23
24
# File 'lib/ecb/exchange.rb', line 22

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

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


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

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