2018-03-08 02:05:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'net/http'
|
|
|
|
require 'ox'
|
|
|
|
|
|
|
|
module Bank
|
|
|
|
class Feed
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
def self.current
|
|
|
|
new('daily')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ninety_days
|
|
|
|
new('hist-90d')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.historical
|
|
|
|
new('hist')
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(scope)
|
|
|
|
@scope = scope
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
|
|
|
document.locate('gesmes:Envelope/Cube/Cube').each do |day|
|
2018-10-03 15:48:30 +02:00
|
|
|
yield(date: Date.parse(day['time']),
|
|
|
|
rates: day.nodes.each_with_object({}) do |currency, rates|
|
|
|
|
rates[currency[:currency]] = Float(currency[:rate])
|
|
|
|
end)
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def document
|
|
|
|
Ox.load(xml)
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml
|
|
|
|
Net::HTTP.get(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
2018-09-11 19:05:14 +02:00
|
|
|
URI("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-#{@scope}.xml")
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|