lib/europe/currency/exchange_rates.rb in europe-0.0.8 vs lib/europe/currency/exchange_rates.rb in europe-0.0.9

- old
+ new

@@ -1,25 +1,33 @@ -require 'open-uri' -require 'nokogiri' +require 'rexml/document' +require 'net/http' +require 'date' # Europe Gem module Europe # currency module Currency # exchange rates module ExchangeRates - EXCHANGE_URL = 'http://www.ecb.europa.eu/stats/' \ - 'eurofxref/eurofxref-daily.xml'.freeze + EXCHANGE_URL = 'https://www.floatrates.com/daily/eur.xml'.freeze + def self.retrieve - extract_rates(Nokogiri::XML(open(EXCHANGE_URL))) + resp = Net::HTTP.get_response(URI.parse(EXCHANGE_URL)) + resp.code.to_i == 200 ? extract_rates(resp.body) : :failed end def self.extract_rates(doc) - rates = { date: Date.parse(doc.css('Cube Cube').first['time']), + xml = REXML::Document.new(doc) + + rates = { date: Date.parse(xml.elements.first.elements[7].text), rates: {} } - doc.css('Cube Cube Cube').each do |rate| - rates[:rates][rate.xpath('@currency').text.to_sym] = - rate.xpath('@rate').text.to_f + + filter_rates(xml, rates) + end + + def self.filter_rates(xml, rates) + xml.elements.each('channel/item') do |item| + rates[:rates][item[13].text.to_sym] = item[17].text.delete(',').to_f end rates end end end