mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-22 02:52:49 +01:00
cfbb4ac4ac
I'm moving my company's server to a private location now that I have sold the domain. While prepping for this, I've done some cleanup and also threw in changes I had lingering on my hard drive. - Run a single database query instead of two - Fold the gem into the app and use Ox instead of REXML - Simplify error handling logic - Relax throttling
54 lines
864 B
Ruby
54 lines
864 B
Ruby
# 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|
|
|
date = Date.parse(day['time'])
|
|
day.locate('Cube').each do |record|
|
|
yield(
|
|
date: date,
|
|
iso_code: record['currency'],
|
|
rate: Float(record['rate'])
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def document
|
|
Ox.load(xml)
|
|
end
|
|
|
|
def xml
|
|
Net::HTTP.get(url)
|
|
end
|
|
|
|
def url
|
|
URI("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-#{@scope}.xml")
|
|
end
|
|
end
|
|
end
|