Class: Devilicious::CurrencyConverter::YahooExchange

Inherits:
Object
  • Object
show all
Defined in:
lib/devilicious/currency_converter.rb

Constant Summary collapse

URL =
"http://download.finance.yahoo.com/d/quotes.csv?s=%s%s=X&f=sl1d1&e=.csv".freeze

Class Method Summary collapse

Class Method Details

.get_csv(url) ⇒ Object



73
74
75
76
77
78
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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