class Ubea::CurrencyConverter::FreeCurrencyConverterAPI

Constants

URL

Public Class Methods

get_json(url, from, to) click to toggle source
# File lib/ubea/currency_converter.rb, line 77
def self.get_json(url, from, to)
  Retryable.retryable(tries: 3, sleep: 1) do
    json = open(url).read
    JSON.parse(json)["#{from}_#{to}"]["val"].to_s
  end
end
get_rate(from, to) click to toggle source
# File lib/ubea/currency_converter.rb, line 61
def self.get_rate(from, to)
  url = URL % [from, to]
  string = get_json(url, from, to)
  rate = BigDecimal.new(string)

  if rate <= 1E-3
    Log.warn "Cannot retrieve exchange rate for #{from}#{to}, not enough precision, using the opposite pair"

    url = URL % [to, from]
    string = get_json(url, to, from)
    rate = BigDecimal.new(1) / BigDecimal.new(string)
  end

  rate
end