class Lumione::Bank

Constants

CACHE_DIR
CACHE_FILE
DEFAULT_CACHE_DIR

Public Class Methods

convert_and_print(amount, from_currency, to_currency) click to toggle source
# File lib/lumione/bank.rb, line 13
def self.convert_and_print(amount, from_currency, to_currency)
  new.convert_and_print amount, from_currency, to_currency
end

Public Instance Methods

convert(amount, from_currency, to_currency) click to toggle source
# File lib/lumione/bank.rb, line 33
def convert(amount, from_currency, to_currency)
  @original_money = Money.from_amount(amount, from_currency)
  @converted_money = @original_money.exchange_to(to_currency)
end
convert_and_print(amount, from_currency, to_currency) click to toggle source
# File lib/lumione/bank.rb, line 17
def convert_and_print(amount, from_currency, to_currency)
  prepare_rates

  convert(amount, from_currency, to_currency)

  print_original_and_converted_money
end
prepare_rates() click to toggle source
# File lib/lumione/bank.rb, line 38
def prepare_rates
  create_cache_dir
  update_and_load_rates
end
print_original_and_converted_money() click to toggle source

Private Instance Methods

bank() click to toggle source
# File lib/lumione/bank.rb, line 45
def bank
  Money.default_bank
end
create_cache_dir() click to toggle source
# File lib/lumione/bank.rb, line 49
def create_cache_dir
  cache_dir = File.dirname CACHE_FILE
  FileUtils.mkdir_p cache_dir
end
fetch_rates_and_save_to_cache() click to toggle source
# File lib/lumione/bank.rb, line 62
def fetch_rates_and_save_to_cache
  bank.save_rates(CACHE_FILE)
end
format_conversion(original_money, converted_money) click to toggle source
# File lib/lumione/bank.rb, line 82
def format_conversion(original_money, converted_money)
  formatted_original_money = original_money.format(with_currency: true)
  formatted_converted_money = converted_money.format(with_currency: true)
  "#{formatted_original_money} (#{formatted_converted_money})"
end
how_long_since_rates_were_updated(rates_updated_at) click to toggle source
# File lib/lumione/bank.rb, line 78
def how_long_since_rates_were_updated(rates_updated_at)
  "rates updated #{distance_of_time_in_words_to_now rates_updated_at} ago"
end
load_rates_from_cache() click to toggle source
# File lib/lumione/bank.rb, line 74
def load_rates_from_cache
  bank.update_rates(CACHE_FILE) if File.exists? CACHE_FILE
end
stale_rates?() click to toggle source
# File lib/lumione/bank.rb, line 70
def stale_rates?
  !bank.rates_updated_at || File.mtime(CACHE_FILE) < 1.day.ago
end
up_to_date_rates?() click to toggle source
# File lib/lumione/bank.rb, line 66
def up_to_date_rates?
  !stale_rates?
end
update_and_load_rates() click to toggle source
# File lib/lumione/bank.rb, line 54
def update_and_load_rates
  load_rates_from_cache
  return if up_to_date_rates?

  fetch_rates_and_save_to_cache
  load_rates_from_cache
end