class ActiveCurrency::AddRates
Store the latest currency rates.
Attributes
bank[RW]
currencies[RW]
Public Class Methods
call(currencies, *options)
click to toggle source
# File lib/active_currency/add_rates.rb, line 21 def self.call(currencies, *options) new(currencies, *options).call end
new(currencies, bank: EuCentralBank.new)
click to toggle source
# File lib/active_currency/add_rates.rb, line 6 def initialize(currencies, bank: EuCentralBank.new) @currencies = currencies.map(&:to_s).map(&:upcase) @bank = bank end
Public Instance Methods
call()
click to toggle source
# File lib/active_currency/add_rates.rb, line 11 def call bank.update_rates rates_hash.each do |(from, to), rate| store.add_rate(from, to, rate) end nil end
Private Instance Methods
get_rate(hash, from, to)
click to toggle source
# File lib/active_currency/add_rates.rb, line 43 def get_rate(hash, from, to) rate = bank.get_rate(from, to) return rate if rate # Inverse rate (not so good) inverse = hash[[to, from]] return 1.fdiv(inverse) if inverse # Rate going through the first currency (desperate) from_main = hash[[from, main_currency]] to_main = hash[[main_currency, to]] return from_main * to_main if from_main && to_main raise "Unknown rate between #{from} and #{to}" end
main_currency()
click to toggle source
# File lib/active_currency/add_rates.rb, line 59 def main_currency currencies.first end
rates_hash()
click to toggle source
# File lib/active_currency/add_rates.rb, line 33 def rates_hash currencies.each_with_object({}) do |from, hash| currencies.each do |to| next if from == to hash[[from, to]] = get_rate(hash, from, to) end end end
store()
click to toggle source
# File lib/active_currency/add_rates.rb, line 29 def store @store ||= ActiveCurrency::RateStore.new end