Class: SKVReport::ExchangeRates
- Inherits:
-
Object
- Object
- SKVReport::ExchangeRates
- 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
-
#base_currency ⇒ Object
readonly
Returns the value of attribute base_currency.
-
#rates ⇒ Object
readonly
Returns the value of attribute rates.
Instance Method Summary collapse
-
#initialize(rates = {}) ⇒ ExchangeRates
constructor
A new instance of ExchangeRates.
- #rate_for(from_currency, date) ⇒ Object
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_currency ⇒ Object (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 |
#rates ⇒ Object (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 |