mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-22 19:12:32 +01:00
cf373f3efb
This way, we will have no issue fitting the entire dataset on the free tier of Heroku
37 lines
766 B
Ruby
37 lines
766 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'day'
|
|
require 'forwardable'
|
|
|
|
class Currency < Sequel::Model(Day.currencies)
|
|
class << self
|
|
extend Forwardable
|
|
|
|
def_delegators :dataset, :latest, :between
|
|
end
|
|
|
|
dataset_module do
|
|
def only(*iso_codes)
|
|
where(iso_code: iso_codes)
|
|
end
|
|
|
|
def between(interval)
|
|
case interval.last - interval.first
|
|
when 0..90 then super
|
|
when 91..365 then super.sample('week')
|
|
else super.sample('month')
|
|
end
|
|
end
|
|
|
|
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)
|
|
end
|
|
end
|
|
end
|