Class: ExchangeRateConverter
- Inherits:
-
Object
- Object
- ExchangeRateConverter
- Defined in:
- lib/dollar_to_euro/exchange_rate_converter.rb
Class Method Summary collapse
- .calculate_with_default_rate(amount, parsed_date) ⇒ Object
- .convert(amount, date) ⇒ Object
- .database_is_out_of_date? ⇒ Boolean
- .date_is_holiday_or_weekend?(date) ⇒ Boolean
-
.default_date?(parsed_date) ⇒ Boolean
returns true if date is oldest than the oldest register we have returns true if date is todays date.
- .holiday?(date) ⇒ Boolean
- .previous_rate_available(date) ⇒ Object
- .weekend?(date) ⇒ Boolean
Class Method Details
.calculate_with_default_rate(amount, parsed_date) ⇒ Object
22 23 24 25 |
# 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) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# 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? ⇒ Boolean
52 53 54 55 |
# 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) ⇒ Boolean
37 38 39 |
# 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) ⇒ Boolean
returns true if date is oldest than the oldest register we have returns true if date is todays date
29 30 31 |
# 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) ⇒ Boolean
41 42 43 44 45 46 |
# 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) ⇒ Object
33 34 35 |
# 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) ⇒ Boolean
48 49 50 |
# File 'lib/dollar_to_euro/exchange_rate_converter.rb', line 48 def weekend?(date) [6, 7].include?(Date.parse(date).cwday) end |