Sha256: 54e2ad7c981c5d9edbf05ba6940ee6a0f3a92733a8a973316443a68489baa68a

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

#!/usr/bin/env ruby

require 'optparse'
require 'methadone'
require 'dollar_to_euro'
require 'date'

class App
  include Methadone::Main
  include Methadone::CLILogging

  main do
    # If user wants to update the database this action will be processed alone
    if options[:update]
      url = "#{DollarToEuro::ECB_URL}&start=04-01-1999&end=#{Date.today.strftime("%d-%m-%Y")}&type=csv"
      UpdateDatabase.perform_async(url)
    else
      raise 'Amount required.' if options[:amount].nil?
      options[:date] ||= Date.today.to_s
      y, m, d = options[:date].split '-'
      raise 'Invalid date.' unless Date.valid_date? y.to_i, m.to_i, d.to_i
      # if database is out of date tell the user to update it
      raise 'Please update the database first.' if ExchangeRateConverter.database_is_out_of_date?
      puts ExchangeRateConverter.convert(options[:amount].to_f, options[:date])
    end
  end

  description 'Convert dollars to euros.'

  on('-a AMOUNT', '--amount', 'Amount in dollars that you want to convert to euros.')
  on('-d DATE', '--date', "Date for having the value of the euro at that specific day, format YYYY-MM-DD. Default '#{Date.today.to_s}'.")
  on('-u', '--update', 'Updates the database to have the latest values. No other options are considered.')

  version DollarToEuro::VERSION, compact: true

  go!
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dollar_to_euro-0.1.1 bin/dollar_to_euro
dollar_to_euro-0.1.0 bin/dollar_to_euro