class ExchangeRateConverter
Public Class Methods
calculate_with_default_rate(amount, parsed_date)
click to toggle source
# File lib/dollar_to_euro/exchange_rate_converter.rb, line 22 def calculate_with_default_rate(amount, parsed_date) return Dollar.last.value * amount if parsed_date >= Date.today return Dollar.first.value * amount if parsed_date < Dollar.first.date end
convert(amount, date)
click to toggle source
# File lib/dollar_to_euro/exchange_rate_converter.rb, line 11 def convert(amount, date) parsed_date = Date.parse(date) # if converting at oldest or latest rate return calculate_with_default_rate(amount, parsed_date) if default_date?(parsed_date) # if exchange rate exists for the given date return the exchange register = Dollar.where(date: parsed_date) return register.first.value * amount if register.exists? # if the date is holiday or weekend pick the previous available rate exchange previous_rate_available(parsed_date) * amount if date_is_holiday_or_weekend?(date) end
database_is_out_of_date?()
click to toggle source
# File lib/dollar_to_euro/exchange_rate_converter.rb, line 52 def database_is_out_of_date? # TODO: handle more complex scenarios Dollar.count.zero? end
date_is_holiday_or_weekend?(date)
click to toggle source
# File lib/dollar_to_euro/exchange_rate_converter.rb, line 37 def date_is_holiday_or_weekend?(date) weekend?(date) || holiday?(date) end
default_date?(parsed_date)
click to toggle source
returns true if date is oldest than the oldest register we have returns true if date is todays date
# File lib/dollar_to_euro/exchange_rate_converter.rb, line 29 def default_date?(parsed_date) parsed_date >= Date.today || parsed_date < Dollar.first.date end
holiday?(date)
click to toggle source
# File lib/dollar_to_euro/exchange_rate_converter.rb, line 41 def holiday?(date) Holidays.cache_between(Time.now, 2.years.from_now, :us, :observed) y, m, d = date.split '-' parsed_date = Date.civil(y.to_i, m.to_i, d.to_i) parsed_date.holiday?(:us) end
previous_rate_available(date)
click to toggle source
# File lib/dollar_to_euro/exchange_rate_converter.rb, line 33 def previous_rate_available(date) Dollar.where(:date.lte => date).last.value end
weekend?(date)
click to toggle source
# File lib/dollar_to_euro/exchange_rate_converter.rb, line 48 def weekend?(date) [6, 7].include?(Date.parse(date).cwday) end