frankfurter/lib/bank/feed.rb

52 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "net/http"
require "ox"
module Bank
class Feed
include Enumerable
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
new(xml)
end
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
new(xml)
end
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
new(xml)
end
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)
end
def each
@document.locate("gesmes:Envelope/Cube/Cube").each do |day|
yield(date: Date.parse(day["time"]),
rates: day.nodes.each_with_object({}) do |currency, rates|
rates[currency[:currency]] = Float(currency[:rate])
end)
end
end
end
end