mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-22 11:02:30 +01:00
a96e56808e
... along with some minor miscellaneous refactoring This finally completes fixerAPI/fixer#22
31 lines
877 B
Ruby
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
|