frankfurter/lib/currency.rb
Hakan Ensari a96e56808e Implement time series
... along with some minor miscellaneous refactoring

This finally completes fixerAPI/fixer#22
2018-07-05 21:30:17 +01:00

31 lines
877 B
Ruby

# frozen_string_literal: true
class Currency < Sequel::Model
dataset_module do
def latest(date = Date.today)
where(date: select(:date).where(Sequel.lit('date <= ?', date))
.order(Sequel.desc(:date))
.limit(1))
end
def between(date_interval)
query = where(date: date_interval).order(:date)
length = date_interval.last - date_interval.first
if length > 365
query.sampled('month')
elsif length > 90
query.sampled('week')
else
query
end
end
def sampled(precision)
sampled_date = Sequel.lit("date_trunc('#{precision}', date)")
select(:iso_code).select_append { avg(rate).as(rate) }
.select_append(sampled_date.as(:date))
.group(:iso_code, sampled_date)
end
end
end