Class: SKVReport::ExchangeRates

Inherits:
Object
  • Object
show all
Defined in:
lib/skv_report/exchange_rates.rb

Overview

Fetches relevant exchange rate for given date and given currency

Defined Under Namespace

Classes: NoRateForCurrencyError, NoRatesForDateError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rates = {}) ⇒ ExchangeRates

Returns a new instance of ExchangeRates.



14
15
16
17
# File 'lib/skv_report/exchange_rates.rb', line 14

def initialize(rates = {})
  @rates = rates['rates'] || rates['dates_with_rates']
  @base_currency = rates['base']
end

Instance Attribute Details

#base_currencyObject (readonly)

Returns the value of attribute base_currency.



12
13
14
# File 'lib/skv_report/exchange_rates.rb', line 12

def base_currency
  @base_currency
end

#ratesObject (readonly)

Returns the value of attribute rates.



12
13
14
# File 'lib/skv_report/exchange_rates.rb', line 12

def rates
  @rates
end

Instance Method Details

#rate_for(from_currency, date) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/skv_report/exchange_rates.rb', line 19

def rate_for(from_currency, date)
  date_rates = rates[date]
  raise_no_rates_for_date_error(date) if date_rates.nil?

  rate = rates.dig(date, from_currency.upcase)
  return rate unless rate.nil?

  raise NoRateForCurrencyError,
        "currency: #{from_currency}\nrates: #{rates}"
end