class SKVReport::ExchangeRates

Fetches relevant exchange rate for given date and given currency

Attributes

base_currency[R]
rates[R]

Public Class Methods

new(rates = {}) click to toggle source
# File lib/skv_report/exchange_rates.rb, line 14
def initialize(rates = {})
  @rates = rates['rates'] || rates['dates_with_rates']
  @base_currency = rates['base']
end

Public Instance Methods

rate_for(from_currency, date) click to toggle source
# 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

Private Instance Methods

raise_no_rates_for_date_error(date) click to toggle source
# File lib/skv_report/exchange_rates.rb, line 32
def raise_no_rates_for_date_error(date)
  raise NoRatesForDateError, "date: #{date}\navailable_dates: #{rates.keys}"
end