Refactor tasks that fetch rates

This commit is contained in:
Hakan Ensari 2018-06-06 16:30:18 +01:00
parent 2c91173ecf
commit 95d63c143f
5 changed files with 38 additions and 15 deletions

View File

@ -7,6 +7,6 @@ require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new
scheduler.cron '*/15 15,16,17 * * 1-5', timeout: '1m' do
system 'rake rates:update'
system 'rake rates:current'
end
scheduler.join

View File

@ -4,11 +4,15 @@ require 'currency'
require 'bank/feed'
module Bank
def self.fetch_all_rates!
def self.fetch_all!
Currency.dataset.insert_conflict.multi_insert(Feed.historical.to_a)
end
def self.fetch_current_rates!
def self.fetch_ninety_days!
Currency.dataset.insert_conflict.multi_insert(Feed.ninety_days.to_a)
end
def self.fetch_current!
Currency.db.transaction do
Feed.current.each do |hsh|
Currency.find_or_create(hsh)
@ -16,7 +20,7 @@ module Bank
end
end
def self.replace_all_rates!
def self.replace_all!
Currency.dataset.delete
Currency.multi_insert(Feed.historical.to_a)
end

View File

@ -12,5 +12,5 @@ namespace :db do
Sequel::IntegerMigrator.new(db, dir, opts).run
end
task setup: %w[db:migrate rates:reload]
task setup: %w[db:migrate rates:all]
end

View File

@ -1,15 +1,21 @@
# frozen_string_literal: true
namespace :rates do
desc 'Reload all rates'
task reload: :environment do
desc 'Load all'
task all: :environment do
require 'bank'
Bank.fetch_all_rates!
Bank.fetch_all!
end
desc 'Update current rates'
task update: :environment do
desc 'Load last 90 days'
task ninety_days: :environment do
require 'bank'
Bank.fetch_current_rates!
Bank.fetch_ninety_days!
end
desc 'Load current'
task current: :environment do
require 'bank'
Bank.fetch_current!
end
end

View File

@ -21,25 +21,38 @@ describe Bank do
it 'fetches all rates' do
Currency.dataset.delete
Bank.fetch_all_rates!
Bank.fetch_all!
Currency.count.must_be :positive?
end
it 'skips existing records when fetching all rates' do
Currency.where { date < '2012-01-01' }.delete
Bank.fetch_all_rates!
Bank.fetch_all!
Currency.where { date < '2012-01-01' }.count.must_be :positive?
end
it 'fetches rates for last 90 days' do
Currency.dataset.delete
Bank.fetch_ninety_days!
Currency.count.must_be :positive?
end
it 'skips existing records when fetching rates for last 90 days' do
cutoff = Date.today - 60
Currency.where { date < cutoff }.delete
Bank.fetch_ninety_days!
Currency.where { date < cutoff }.count.must_be :positive?
end
it 'fetches current rates' do
Currency.dataset.delete
Bank.fetch_current_rates!
Bank.fetch_current!
Currency.count.must_be :positive?
end
it 'replaces all rates' do
Currency.dataset.delete
Bank.fetch_all_rates!
Bank.fetch_all!
Currency.count.must_be :positive?
end
end