class DailyExchangeRatesBank

Class for aiding in exchanging money between different currencies.

Constants

SERIALIZER_DATE_SEPARATOR

Public Class Methods

new(store = Money::RatesStore::StoreWithDateSupport.new, &block) click to toggle source
Calls superclass method
# File lib/daily_exchange_rates_bank.rb, line 11
def initialize(store = Money::RatesStore::StoreWithDateSupport.new, &block)
  super(store, &block)
end

Public Instance Methods

exchange(cents, from_currency, to_currency, date = nil) click to toggle source
# File lib/daily_exchange_rates_bank.rb, line 36
def exchange(cents, from_currency, to_currency, date = nil)
  exchange_with(Money.new(cents, from_currency), to_currency, date)
end
exchange_with(from, to_currency, date = nil) click to toggle source
# File lib/daily_exchange_rates_bank.rb, line 40
def exchange_with(from, to_currency, date = nil)
  to_currency = ::Money::Currency.wrap(to_currency)
  return from if from.currency == to_currency

  rate = get_rate(from.currency, to_currency, date)

  rate ||= rate_from_exchange_rates_api(from.currency, to_currency, date)

  fractional = calculate_fractional(from, to_currency, rate)
  Money.new(fractional, to_currency)
end
get_rate(from, to, date = nil) click to toggle source
# File lib/daily_exchange_rates_bank.rb, line 15
def get_rate(from, to, date = nil)
  store.get_rate(::Money::Currency.wrap(from).iso_code,
                 ::Money::Currency.wrap(to).iso_code,
                 date)
end
rates() click to toggle source
# File lib/daily_exchange_rates_bank.rb, line 28
def rates
  store.each_rate.each_with_object({}) do |(from, to, rate, date), hash|
    key = [from, to].join(SERIALIZER_SEPARATOR)
    key = [key, date.to_s].join(SERIALIZER_DATE_SEPARATOR) if date
    hash[key] = rate
  end
end
set_rate(from, to, rate, date = nil) click to toggle source
# File lib/daily_exchange_rates_bank.rb, line 21
def set_rate(from, to, rate, date = nil)
  store.add_rate(::Money::Currency.wrap(from).iso_code,
                 ::Money::Currency.wrap(to).iso_code,
                 rate,
                 date)
end

Private Instance Methods

calculate_fractional(from, to_currency, rate) click to toggle source
# File lib/daily_exchange_rates_bank.rb, line 66
def calculate_fractional(from, to_currency, rate)
  BigDecimal(rate.to_s) * BigDecimal(from.fractional.to_s) / (
    BigDecimal(from.currency.subunit_to_unit.to_s) /
    BigDecimal(to_currency.subunit_to_unit.to_s)
  )
end
rate_from_exchange_rates_api(from_currency, to_currency, date) click to toggle source
# File lib/daily_exchange_rates_bank.rb, line 54
def rate_from_exchange_rates_api(from_currency, to_currency, date)
  api_client = DailyExchangeRatesBank::ExchangeRatesApiClient.new
  rates = api_client.exchange_rates(from: from_currency.iso_code,
                                    to: [to_currency.iso_code],
                                    date: (date || Date.today))
  rate = rates[to_currency.iso_code]

  set_rate(from_currency, to_currency, rate, date)

  rate
end