module NoNonsenseCurrencyConverter

Constants

BASE_URL

BASE_URL = ‘www.google.com/’

CONVERTER_URL
VERSION

Public Class Methods

get_converted_currency_value(from_currency, to_currency, amount, force=false) click to toggle source
# File lib/no_nonsense_currency_converter.rb, line 12
def self.get_converted_currency_value(from_currency, to_currency, amount, force=false)
  from_currency = from_currency.upcase
  to_currency   = to_currency.upcase
  return amount if from_currency == to_currency
  rate =
      if (@@rate_hash[from_currency].try(:[], to_currency).present?) && !force
        @@rate_hash[from_currency][to_currency]
      else
        conversion_rate = get_raw_data(from_currency, to_currency)
        @@rate_hash[from_currency].present? ? @@rate_hash[from_currency].merge!({ to_currency => conversion_rate }) : @@rate_hash[from_currency] = { to_currency => conversion_rate }
        conversion_rate
      end
  (rate * amount.to_f).to_f.round(2)
end

Private Class Methods

get_final_parsed_amount(data) click to toggle source
# File lib/no_nonsense_currency_converter.rb, line 35
def self.get_final_parsed_amount(data)
  # data.read.scan(/<span class=bld>(\d+\.?\d*) [A-Z]{3}<\/span>/).flatten.first
  JSON.parse(data)['rate']['']
end
get_raw_data(from, to) click to toggle source
# File lib/no_nonsense_currency_converter.rb, line 28
def self.get_raw_data(from, to)
  # open("#{BASE_URL}#{CONVERTER_URL}?a=#{amount}&from=#{from.upcase}&to=#{to.upcase}")
  uri  = URI("#{BASE_URL}#{CONVERTER_URL}?symbols=#{to}&base=#{from}")
  data = Net::HTTP.get(uri)
  JSON.parse(data)['rates'][to]
end