frankfurter/lib/snapshot.rb

51 lines
861 B
Ruby
Raw Normal View History

2012-11-20 17:36:12 +01:00
require_relative 'db'
class Snapshot
def self.last
new Currency.last_date
end
def initialize(date)
@date = date
end
2012-11-21 12:31:50 +01:00
def to_base(base)
2012-11-21 13:31:17 +01:00
rebased_rates = rates
unless base == 'EUR'
base_rate = rebased_rates
.update('EUR' => 1.0)
.delete base
rebased_rates.each do |iso_code, rate|
new_rate = rate / base_rate
rebased_rates[iso_code] =
case new_rate
when new_rate > 100
new_rate.round 2
when new_rate > 10
new_rate.round 3
else
new_rate.round 4
end
end
end
2012-11-21 12:31:50 +01:00
{
base: base,
date: @date,
2012-11-21 13:31:17 +01:00
rates: rebased_rates
2012-11-21 12:31:50 +01:00
}
2012-11-20 17:36:12 +01:00
end
private
def rates
Currency
.where(date: @date)
.reduce({}) { |hsh, currency|
hsh.update currency.to_hash
}
end
end