mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-10-29 23:32:35 +01:00
Refactor tasks that fetch rates
This commit is contained in:
parent
2c91173ecf
commit
95d63c143f
@ -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
|
||||
|
10
lib/bank.rb
10
lib/bank.rb
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user