2018-03-08 02:05:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-03 15:48:30 +02:00
|
|
|
require 'day'
|
|
|
|
require 'forwardable'
|
|
|
|
|
|
|
|
class Currency < Sequel::Model(Day.currencies)
|
|
|
|
class << self
|
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
def_delegators :dataset, :latest, :between
|
|
|
|
end
|
|
|
|
|
2018-03-08 02:05:19 +01:00
|
|
|
dataset_module do
|
2018-10-03 15:48:30 +02:00
|
|
|
def only(*iso_codes)
|
|
|
|
where(iso_code: iso_codes)
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
2018-07-05 21:19:37 +02:00
|
|
|
|
2018-10-03 15:48:30 +02:00
|
|
|
def between(interval)
|
|
|
|
case interval.last - interval.first
|
|
|
|
when 0..90 then super
|
2020-04-01 12:21:32 +02:00
|
|
|
when 91.. then super.sample('week')
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-03 15:48:30 +02:00
|
|
|
def sample(precision)
|
|
|
|
sampler = Sequel.function(:date_trunc, precision, :date)
|
|
|
|
|
|
|
|
select(:iso_code)
|
|
|
|
.select_append { avg(rate).as(rate) }
|
|
|
|
.select_append(sampler.as(:date))
|
|
|
|
.group(:iso_code, sampler)
|
|
|
|
.order(:date)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
end
|