Class: Money::Bank::GoogleCurrencyRailsCache

Inherits:
VariableExchange
  • Object
show all
Defined in:
lib/money/bank/google_currency_rails_cache.rb

Constant Summary collapse

SERVICE_HOST =
"www.google.com"
SERVICE_PATH =
"/finance/converter"

Instance Method Summary collapse

Instance Method Details

#cached_ratesArray

Lists all rates currency cached.

Returns:

  • (Array)

    An array of rates.



71
72
73
# File 'lib/money/bank/google_currency_rails_cache.rb', line 71

def cached_rates
  Rails.cache.read("exchange_rate/all_rates")
end

#flush_rate(from, to) ⇒ Boolean

Clears the specified rate stored in @rates.

Examples:

@bank = GoogleCurrencyRailsCache.new    #=> <Money::Bank::GoogleCurrency...>
@bank.get_rate(:USD, :EUR)    #=> 0.776337241
@bank.flush_rate(:USD, :EUR)  #=> 0.776337241

Parameters:

  • from (String, Symbol, Currency)

    Currency to convert from (used for key into @rates).

  • to (String, Symbol, Currency)

    Currency to convert to (used for key into @rates).

Returns:

  • (Boolean)

    Indicating successful removal.



43
44
45
46
47
# File 'lib/money/bank/google_currency_rails_cache.rb', line 43

def flush_rate(from, to)
  rate_key = rate_key_for(from, to)
  Rails.cache.delete("exchange_rate/#{rate_key}")
  remove_from_stored_rates(rate_key)
end

#flush_ratesObject

Clears all rates stored in @rates

Examples:

@bank = GoogleCurrencyRailsCache.new  #=> <Money::Bank::GoogleCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241
@bank.flush_rates           #=> nil


19
20
21
22
23
24
25
26
27
# File 'lib/money/bank/google_currency_rails_cache.rb', line 19

def flush_rates
  rates = Rails.cache.read("exchange_rate/all_rates")
  unless rates.nil?
    rates.each do |rate_key|
      Rails.cache.delete "exchange_rate/#{rate_key}"
    end
  end
  Rails.cache.delete("exchange_rate/all_rates")
end

#get_rate(from, to) ⇒ Float

Returns the requested rate.

Examples:

@bank = GoogleCurrencyRailsCache.new  #=> <Money::Bank::GoogleCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241

Parameters:

  • from (String, Symbol, Currency)

    Currency to convert from

  • to (String, Symbol, Currency)

    Currency to convert to

Returns:

  • (Float)

    The requested rate.



60
61
62
63
64
# File 'lib/money/bank/google_currency_rails_cache.rb', line 60

def get_rate(from, to)
    Rails.cache.fetch("exchange_rate/#{rate_key_for(from, to)}", :expires_in => 12.hours ) {
      fetch_rate(from, to)
    }
end