Class: Matheus::ConvertCurrency
- Includes:
- ActiveSupport::NumberHelper
- Defined in:
- lib/matheus/convert_currency.rb
Overview
Usage:
$ convert-currency 100 usd eur
$ convert-currency 100 usd eur 2024-03-06
$ convert-currency usd eur # defaults to 1
Instance Method Summary collapse
Methods inherited from Command
Methods included from StringFormat
Methods included from Result::Methods
Instance Method Details
#call(args) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/matheus/convert_currency.rb', line 13 def call(args) amount, source, target, date = parse_args(args) api_url = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@#{date}/v1/currencies/#{source}.min.json" data = JSON.parse(Net::HTTP.get(URI(api_url))) rate = data.dig(source.downcase, target.downcase) if rate converted = amount * rate puts "#{number_to_currency(amount, unit: "")} #{source.upcase} = #{number_to_currency(converted, unit: "")} #{target.upcase}" else Failure("Conversion rate from #{source.upcase} to #{target.upcase} not found") end rescue => e Failure(e.) end |