Sha256: 5fb82b2d66cd69e523da77d379f89ed5b7823c086caabc5937d0cdb60bcb94d7
Contents?: true
Size: 1.76 KB
Versions: 2
Compression:
Stored size: 1.76 KB
Contents
require_relative 'dollar' require 'holidays' require 'holidays/core_extensions/date' class Date include Holidays::CoreExtensions::Date end class ExchangeRateConverter class << self 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 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 # returns true if date is oldest than the oldest register we have # returns true if date is todays date def default_date?(parsed_date) parsed_date >= Date.today || parsed_date < Dollar.first.date end def previous_rate_available(date) Dollar.where(:date.lte => date).last.value end def date_is_holiday_or_weekend?(date) weekend?(date) || holiday?(date) end 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 def weekend?(date) [6, 7].include?(Date.parse(date).cwday) end def database_is_out_of_date? # TODO: handle more complex scenarios Dollar.count.zero? end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dollar_to_euro-0.1.1 | lib/dollar_to_euro/exchange_rate_converter.rb |
dollar_to_euro-0.1.0 | lib/dollar_to_euro/exchange_rate_converter.rb |