Work around ecb rate limiting

This commit is contained in:
Hakan Ensari 2020-04-02 14:38:07 +01:00
parent 706e084eb2
commit cb51bca58b
8 changed files with 45 additions and 24 deletions

View File

@ -5,7 +5,7 @@ require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new scheduler = Rufus::Scheduler.new
scheduler.cron '*/15 15,16,17 * * 1-5' do scheduler.cron '*/30 15,16,17 * * 1-5' do
`rake rates:current` `rake rates:current`
end end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
`rake db:setup` `rake db:prepare`
worker_process_count = (ENV['WORKER_PROCESSES'] || 4).to_i worker_process_count = (ENV['WORKER_PROCESSES'] || 4).to_i

View File

@ -30,6 +30,13 @@ module Bank
Day.multi_insert(data) Day.multi_insert(data)
end end
def seed_with_saved_data!
data = Feed.saved_data.to_a
jsonify!(data)
Day.dataset.delete
Day.multi_insert(data)
end
private private
def jsonify!(data) def jsonify!(data)

File diff suppressed because one or more lines are too long

View File

@ -8,42 +8,42 @@ module Bank
include Enumerable include Enumerable
def self.current def self.current
new('daily') url = URI('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')
xml = Net::HTTP.get(url)
new(xml)
end end
def self.ninety_days def self.ninety_days
new('hist-90d') url = URI('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml')
xml = Net::HTTP.get(url)
new(xml)
end end
def self.historical def self.historical
new('hist') url = URI('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml')
xml = Net::HTTP.get(url)
new(xml)
end end
def initialize(scope) def self.saved_data
@scope = scope xml = File.read(File.join(__dir__, 'eurofxref-hist.xml'))
new(xml)
end
def initialize(xml)
@document = Ox.load(xml)
end end
def each def each
document.locate('gesmes:Envelope/Cube/Cube').each do |day| @document.locate('gesmes:Envelope/Cube/Cube').each do |day|
yield(date: Date.parse(day['time']), yield(date: Date.parse(day['time']),
rates: day.nodes.each_with_object({}) do |currency, rates| rates: day.nodes.each_with_object({}) do |currency, rates|
rates[currency[:currency]] = Float(currency[:rate]) rates[currency[:currency]] = Float(currency[:rate])
end) end)
end end
end end
private
def document
Ox.load(xml)
end
def xml
Net::HTTP.get(url)
end
def url
URI("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-#{@scope}.xml")
end
end end
end end

View File

@ -12,5 +12,13 @@ namespace :db do
Sequel::IntegerMigrator.new(db, dir, opts).run Sequel::IntegerMigrator.new(db, dir, opts).run
end end
task setup: %w[db:migrate rates:all] task prepare: %w[db:migrate rates:all]
namespace :test do
task :prepare do
ENV['APP_ENV'] ||= 'test'
Rake::Task['db:migrate'].invoke
Rake::Task['rates:seed_with_saved_data'].invoke
end
end
end end

View File

@ -18,4 +18,9 @@ namespace :rates do
require 'bank' require 'bank'
Bank.fetch_current! Bank.fetch_current!
end end
task :seed_with_saved_data do
require 'bank'
Bank.seed_with_saved_data!
end
end end

View File

@ -15,4 +15,4 @@ end
RuboCop::RakeTask.new RuboCop::RakeTask.new
task default: %w[db:setup test rubocop] task default: %w[db:test:prepare test rubocop]