Sha256: 7d72fe8d34acf61660533fb2895be5668938d393a61952b7146d5624e29b1855
Contents?: true
Size: 1.34 KB
Versions: 1
Compression:
Stored size: 1.34 KB
Contents
#!/usr/bin/env ruby # CLI app for currency conversion # Uses Ruby Money for accuracy # Uses Fixer.io for exchange data require 'uri' require 'net/http' require 'json' require 'money' require 'monetize' def conversion # User input begin base_currency = ARGV[0].upcase initial_amount = ARGV[1] convert_to = ARGV[2].upcase rescue puts 'Input Error' exit(1) end # Fetches API info begin uri = URI("http://api.fixer.io/latest?base=#{base_currency}&symbols=#{convert_to}") response = Net::HTTP.get(uri) parsed = JSON.parse(response) convert_factor = (parsed['rates'][convert_to]) rescue puts puts 'No exchange rate found' puts exit(1) end # Fixes locale exception bug I18n.enforce_available_locales = false # Creates money exchange rate and new money object begin Money.add_rate(base_currency, convert_to, convert_factor) rescue Money::Currency::UnknownCurrency puts 'Invalid Currency' exit(1) end initial = Monetize.parse("#{base_currency} #{initial_amount}") final_convert = initial.exchange_to(convert_to) # Output formatting puts '=================' puts "#{base_currency} to #{convert_to}" puts "Rate: #{convert_factor}" puts "#{base_currency}: #{initial.format}" puts "#{convert_to}: #{final_convert.format}" puts '=================' end conversion
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jake-scripts-2.0.4 | exe/currency |