class RiksbankCurrency::Fetcher

Public Class Methods

new(date, base) click to toggle source

@param [Date] date @param [String] base currency

# File lib/riksbank_currency/fetcher.rb, line 8
def initialize(date, base)
  @date = date
  @base = base.to_s
end

Public Instance Methods

fetcher() click to toggle source

Define correct rate fetcher @return [TodayFetcher,DateFetcher]

# File lib/riksbank_currency/fetcher.rb, line 28
def fetcher
  @fetcher ||=
    if @date == Date.today
      TodayFetcher.new
    else
      DateFetcher.new(@date)
    end
end
rate_date() click to toggle source

@return [Date]

# File lib/riksbank_currency/fetcher.rb, line 38
def rate_date
  fetcher.rate_date
end
to_hash() click to toggle source

Convert XML response to Hash and recalculate it by @base currency Example:

{
  "EUR": 9.021,
  "USD": 7.65
}

@return [Hash]

# File lib/riksbank_currency/fetcher.rb, line 20
def to_hash
  rates = fetcher.to_hash
  rates['SEK'] = 1.0
  recombine_by_base(rates)
end

Protected Instance Methods

recombine_by_base(rates) click to toggle source
# File lib/riksbank_currency/fetcher.rb, line 44
def recombine_by_base(rates)
  if @base != 'SEK'
    new_rates = {}

    rates.each do |currency, value|
      new_rates[currency] = rates[@base] / value
    end

    rates = new_rates
  end

  rates[@base] = 1.0

  rates
end