class ExchangeRateJt::ExchangeRates

Attributes

configuration[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 12
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

at(date, base, counter) click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 24
def at(date, base, counter)
  validate_date(date)
  date = date.strftime('%b_%d_%Y').to_sym.downcase

  base = fetch_rate(date, base)
  counter = fetch_rate(date, counter)

  rate = counter.to_f / base.to_f

  { status: :success, rate: BigDecimal.new(rate, 5) }
end
currency_list() click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 36
def currency_list
  rates_source.currency_list
end
update() click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 16
def update
  rates = rates_source.fetch_rates
  rates.each { |date, values| data_store.persist(date, values) }
  true
rescue => exception
  raise UpdateError, exception.message
end

Private Instance Methods

build_data_store() click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 56
def build_data_store
  DataStoreFactory
    .build(configuration.data_store_type, configuration.data_store)
end
build_rates_source() click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 52
def build_rates_source
  RatesSourceFactory.build(configuration.source)
end
data_store() click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 44
def data_store
  @data_store ||= build_data_store
end
fetch_rate(date, currency) click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 61
def fetch_rate(date, currency)
  return 1 if currency.to_s == configuration.default_currency.to_s
  data_store.fetch(date, currency)
rescue DataStore::DataNotFoundError
  raise RateNotFoundError, 'Exchange rate data not found'
end
rates_source() click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 48
def rates_source
  @rates_source ||= build_rates_source
end
validate_date(date) click to toggle source
# File lib/exchange_rate_jt/exchange_rates.rb, line 68
def validate_date(date)
  raise InvalidDateError, 'Invalid date specified' unless date.is_a?(Date)
  raise FutureDateError, 'Date cannot be in the future' if date > Date.today
end