class Devilicious::CurrencyConverter::YahooExchange

Constants

URL

Public Class Methods

get_csv(url) click to toggle source
# File lib/devilicious/currency_converter.rb, line 73
def self.get_csv(url)
  retryable(tries: 3, sleep: 1) do
    csv = open(url).read
    csv.split(",")
  end
end
get_rate(from, to) click to toggle source
# File lib/devilicious/currency_converter.rb, line 57
def self.get_rate(from, to)
  url = URL % [from, to]
  csv = get_csv(url)
  rate = BigDecimal.new(csv[1].to_s)

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

    url = URL % [to, from]
    csv = get_csv(url)
    rate = BigDecimal.new(1) / BigDecimal.new(csv[1].to_s)
  end

  rate
end