class ExchangeRateJt::RatesSource::ECB

Constants

CURRENCY_LIST
REPOSITORY_URL

Public Instance Methods

currency_list() click to toggle source
# File lib/exchange_rate_jt/rates_source/ecb.rb, line 18
def currency_list
  CURRENCY_LIST
end
fetch_rates() click to toggle source
# File lib/exchange_rate_jt/rates_source/ecb.rb, line 13
def fetch_rates
  doc = fetch_source
  parse(doc)
end

Private Instance Methods

fetch_source() click to toggle source
# File lib/exchange_rate_jt/rates_source/ecb.rb, line 24
def fetch_source
  Nokogiri::XML(open(REPOSITORY_URL, read_timeout: 5000))
rescue Timeout::Error
  handle_fetch_error('the request timed out')
rescue OpenURI::HTTPError => error
  handle_fetch_error(error)
end
format_date(time_as_string) click to toggle source
# File lib/exchange_rate_jt/rates_source/ecb.rb, line 47
def format_date(time_as_string)
  DateTime.parse(time_as_string).strftime('%b_%d_%Y').downcase.to_sym
end
handle_fetch_error(error) click to toggle source
# File lib/exchange_rate_jt/rates_source/ecb.rb, line 51
def handle_fetch_error(error)
  response = error.io
  code = response.status[0]
  raise CouldNotFetchDataError, "There was an problem fetching the data from the source - #{code}"
end
parse(doc) click to toggle source
# File lib/exchange_rate_jt/rates_source/ecb.rb, line 32
def parse(doc)
  daily_updates = doc.xpath('gesmes:Envelope/xmlns:Cube/xmlns:Cube')
  daily_updates.each_with_object({}) do |daily_update, results_hash|
    strtime = daily_update.attribute('time').value
    date = format_date(strtime)
    results_hash[date] = {}
    daily_update.children.each do |rate|
      curr = rate.attribute('currency').value
      results_hash[date][curr] = rate.attribute('rate').value
    end
  end
rescue
  raise ParseError, 'There was an error parsing the document'
end