class Ubea::CurrencyConverter
Public Class Methods
convert(amount, from, to)
click to toggle source
# File lib/ubea/currency_converter.rb, line 8 def self.convert(amount, from, to) amount * rate(from, to) end
rate(from, to)
click to toggle source
# File lib/ubea/currency_converter.rb, line 12 def self.rate(from, to) pair = [from, to].join if @rates[pair].nil? || @rates[pair].updated_at < Time.now - 10 * 60 rate_1 = YahooExchange.get_rate(from, to) rate_2 = begin RateExchange.get_rate(from, to) rescue OpenURI::HTTPError FreeCurrencyConverterAPI.get_rate(from, to) end spread = (1.0 - rate_1 / rate_2).abs * 100 if spread > 1 raise "#{spread.to_f}% spread for #{pair}: #{rate_1} vs #{rate_2}!" end @rates[pair] = OpenStruct.new( rate: rate_1, updated_at: Time.now ).freeze end @rates[pair].rate end