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 = Rufus::Scheduler.new
|
||||||
scheduler.cron '*/15 15,16,17 * * 1-5', timeout: '1m' do
|
scheduler.cron '*/15 15,16,17 * * 1-5', timeout: '1m' do
|
||||||
system 'rake rates:update'
|
system 'rake rates:current'
|
||||||
end
|
end
|
||||||
scheduler.join
|
scheduler.join
|
||||||
|
10
lib/bank.rb
10
lib/bank.rb
@ -4,11 +4,15 @@ require 'currency'
|
|||||||
require 'bank/feed'
|
require 'bank/feed'
|
||||||
|
|
||||||
module Bank
|
module Bank
|
||||||
def self.fetch_all_rates!
|
def self.fetch_all!
|
||||||
Currency.dataset.insert_conflict.multi_insert(Feed.historical.to_a)
|
Currency.dataset.insert_conflict.multi_insert(Feed.historical.to_a)
|
||||||
end
|
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
|
Currency.db.transaction do
|
||||||
Feed.current.each do |hsh|
|
Feed.current.each do |hsh|
|
||||||
Currency.find_or_create(hsh)
|
Currency.find_or_create(hsh)
|
||||||
@ -16,7 +20,7 @@ module Bank
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.replace_all_rates!
|
def self.replace_all!
|
||||||
Currency.dataset.delete
|
Currency.dataset.delete
|
||||||
Currency.multi_insert(Feed.historical.to_a)
|
Currency.multi_insert(Feed.historical.to_a)
|
||||||
end
|
end
|
||||||
|
@ -12,5 +12,5 @@ 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:reload]
|
task setup: %w[db:migrate rates:all]
|
||||||
end
|
end
|
||||||
|
@ -1,15 +1,21 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
namespace :rates do
|
namespace :rates do
|
||||||
desc 'Reload all rates'
|
desc 'Load all'
|
||||||
task reload: :environment do
|
task all: :environment do
|
||||||
require 'bank'
|
require 'bank'
|
||||||
Bank.fetch_all_rates!
|
Bank.fetch_all!
|
||||||
end
|
end
|
||||||
|
|
||||||
desc 'Update current rates'
|
desc 'Load last 90 days'
|
||||||
task update: :environment do
|
task ninety_days: :environment do
|
||||||
require 'bank'
|
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
|
||||||
end
|
end
|
||||||
|
@ -21,25 +21,38 @@ describe Bank do
|
|||||||
|
|
||||||
it 'fetches all rates' do
|
it 'fetches all rates' do
|
||||||
Currency.dataset.delete
|
Currency.dataset.delete
|
||||||
Bank.fetch_all_rates!
|
Bank.fetch_all!
|
||||||
Currency.count.must_be :positive?
|
Currency.count.must_be :positive?
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'skips existing records when fetching all rates' do
|
it 'skips existing records when fetching all rates' do
|
||||||
Currency.where { date < '2012-01-01' }.delete
|
Currency.where { date < '2012-01-01' }.delete
|
||||||
Bank.fetch_all_rates!
|
Bank.fetch_all!
|
||||||
Currency.where { date < '2012-01-01' }.count.must_be :positive?
|
Currency.where { date < '2012-01-01' }.count.must_be :positive?
|
||||||
end
|
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
|
it 'fetches current rates' do
|
||||||
Currency.dataset.delete
|
Currency.dataset.delete
|
||||||
Bank.fetch_current_rates!
|
Bank.fetch_current!
|
||||||
Currency.count.must_be :positive?
|
Currency.count.must_be :positive?
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'replaces all rates' do
|
it 'replaces all rates' do
|
||||||
Currency.dataset.delete
|
Currency.dataset.delete
|
||||||
Bank.fetch_all_rates!
|
Bank.fetch_all!
|
||||||
Currency.count.must_be :positive?
|
Currency.count.must_be :positive?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user