class CurrencyExchanger

Public Class Methods

new() click to toggle source
# File lib/mrpin/core/currency_exchanger/currency_exchanger.rb, line 3
def initialize
  @exchangers = []

  @exchanger_local = ExchangerLocal.new

  @exchangers << ExchangerYahoo0.new
  @exchangers << ExchangerYahoo1.new
  @exchangers << ExchangerFixer.new

end

Public Instance Methods

get_rate(from_currency, to_currency) click to toggle source
# File lib/mrpin/core/currency_exchanger/currency_exchanger.rb, line 15
def get_rate(from_currency, to_currency)
  from_currency = from_currency.upcase
  to_currency   = to_currency.upcase

  result = nil

  if from_currency == to_currency
    result = 1.0
  else

    result = @exchanger_local.get_rate(from_currency, to_currency)

    if result.nil?
      result = get_rate_and_cache(from_currency, to_currency)
    end
  end

  if result.nil? || result == 0
    AppInfoBase.instance.on_server_error("can't fetch rate from servers. currency #{from_currency}_#{to_currency}")
    result = 0.0
  end

  result
end

Private Instance Methods

get_rate_and_cache(from_currency, to_currency) click to toggle source
# File lib/mrpin/core/currency_exchanger/currency_exchanger.rb, line 41
def get_rate_and_cache(from_currency, to_currency)
  from_currency = from_currency.upcase
  to_currency   = to_currency.upcase

  result = nil

  @exchangers.each do |exchanger|
    begin
      result = exchanger.get_rate(from_currency, to_currency)

      break if !result.nil? && result > 0
    rescue Exception => e
      AppInfo.instance.on_server_error("Can't fetch currency rate form #{exchanger.class.name}. Error: #{e.message}")
    end
  end

  @exchanger_local.set_rate(from_currency, to_currency, result)

  result
end