class RiksbankCurrency::DateFetcher

Get historical rates by date.

@see swea.riksbank.se/sweaWS/docs/api/call/getInterestAndExchangeRates.htm

IMPORTANT! The date should be less than current date. To get current rates - check TodayFetcher

Public Class Methods

new(date) click to toggle source
# File lib/riksbank_currency/fetcher/date_fetcher.rb, line 9
def initialize(date)
  @date = date
end

Public Instance Methods

rate_date() click to toggle source
# File lib/riksbank_currency/fetcher/date_fetcher.rb, line 13
def rate_date
  @rate_date ||= BusinessDay.get_latest(@date)
end
response() click to toggle source
# File lib/riksbank_currency/fetcher/date_fetcher.rb, line 36
def response
  @response ||= Request.call(xml_template, 'getInterestAndExchangeRates')
end
to_hash() click to toggle source
# File lib/riksbank_currency/fetcher/date_fetcher.rb, line 17
def to_hash
  rates = {}

  response.xpath("//series").each do |series|
    currency = Helper.currency_from_seriesid(series.at_xpath('seriesid').content)

    if (rate = series.at_xpath('resultrows/value').content).length > 0
      unit = BigDecimal(series.at_xpath('unit').content)
      rate = BigDecimal(rate)

      rates[currency] = rate / unit
    else
      next
    end
  end

  rates
end

Protected Instance Methods

group(currency) click to toggle source
# File lib/riksbank_currency/fetcher/date_fetcher.rb, line 48
    def group(currency)
      <<-XML
        <searchGroupSeries>
          <groupid>130</groupid>
          <seriesid>#{Helper.currency_to_seriesid(currency)}</seriesid>
        </searchGroupSeries>
      XML
    end
groups() click to toggle source
# File lib/riksbank_currency/fetcher/date_fetcher.rb, line 42
def groups
  RiksbankCurrency.currencies.map do |currency|
    group(currency)
  end.join('')
end
xml_template() click to toggle source
# File lib/riksbank_currency/fetcher/date_fetcher.rb, line 57
    def xml_template
      formatted_date = Helper.format_date(rate_date)
      <<-XML
      <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://swea.riksbank.se/xsd">
        <soap:Header/>
        <soap:Body>
          <xsd:getInterestAndExchangeRates>
            <searchRequestParameters>
              <aggregateMethod>D</aggregateMethod>
              <datefrom>#{formatted_date}</datefrom>
              <dateto>#{formatted_date}</dateto>
              <languageid>en</languageid>
              <min>false</min>
              <avg>true</avg>
              <max>true</max>
              <ultimo>false</ultimo>
              #{groups}
            </searchRequestParameters>
          </xsd:getInterestAndExchangeRates>   
        </soap:Body>
      </soap:Envelope>
      XML
    end