2018-03-08 02:05:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
require "net/http"
|
|
|
|
require "ox"
|
2018-03-08 02:05:19 +01:00
|
|
|
|
|
|
|
module Bank
|
|
|
|
class Feed
|
|
|
|
include Enumerable
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
class << self
|
|
|
|
def current
|
|
|
|
url = URI("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
|
|
|
|
xml = Net::HTTP.get(url)
|
2020-04-02 15:38:07 +02:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
new(xml)
|
|
|
|
end
|
2018-03-08 02:05:19 +01:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
def ninety_days
|
|
|
|
url = URI("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml")
|
|
|
|
xml = Net::HTTP.get(url)
|
2020-04-02 15:38:07 +02:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
new(xml)
|
|
|
|
end
|
2018-03-08 02:05:19 +01:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
def historical
|
|
|
|
url = URI("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml")
|
|
|
|
xml = Net::HTTP.get(url)
|
2020-04-02 15:38:07 +02:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
new(xml)
|
|
|
|
end
|
2018-03-08 02:05:19 +01:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
def saved_data
|
|
|
|
xml = File.read(File.join(__dir__, "eurofxref-hist.xml"))
|
|
|
|
new(xml)
|
|
|
|
end
|
2020-04-02 15:38:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(xml)
|
|
|
|
@document = Ox.load(xml)
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
2024-11-19 20:06:47 +01:00
|
|
|
@document.locate("gesmes:Envelope/Cube/Cube").each do |day|
|
|
|
|
yield(date: Date.parse(day["time"]),
|
2018-10-03 15:48:30 +02:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|