class RiksbankCurrency::Rates

Public Class Methods

new(**options) click to toggle source
# File lib/riksbank_currency/rates.rb, line 5
def initialize(**options)
  @date = options[:date] || Date.today
  @base = options[:base] || 'SEK'

  # it is possible to pass cached rates to initializer to avoid extra http calls
  unless options[:rates].nil?
    @rates     = options[:rates]
    @rate_date = @date
  end
end

Public Instance Methods

currencies() click to toggle source

@return [Array]

# File lib/riksbank_currency/rates.rb, line 47
def currencies
  rates.keys
end
fetcher() click to toggle source

@return [RiksbankCurreny::Fetcher]

# File lib/riksbank_currency/rates.rb, line 17
def fetcher
  @fetcher ||= Fetcher.new(@date, @base)
end
rate(from, to = @base) click to toggle source
# File lib/riksbank_currency/rates.rb, line 21
def rate(from, to = @base)
  return nil if rates[from].nil? || rates[to].nil?
  rates[from] / rates[to]
end
rate_date() click to toggle source

Sometimes date passed to the initializer is different from last available exchange date. It could be because of holidays, weekends or bank closing hours.

For example, if we want to get rates for the `1st of January 2018` then rate date will be `27th of December 2017`, because `1st of January` is a holiday. @return [Date]

# File lib/riksbank_currency/rates.rb, line 42
def rate_date
  @rate_date ||= fetcher.rate_date
end
rates() click to toggle source

Hash with all available currencies {

"EUR": 9.021,
"USD": 7.211

} @return [Hash]

# File lib/riksbank_currency/rates.rb, line 32
def rates
  @rates ||= fetcher.to_hash
end