Class: Money::Bank::GoogleCurrency

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

Constant Summary collapse

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGoogleCurrency

Returns a new instance of GoogleCurrency.


52
53
54
55
# File 'lib/money/bank/google_currency.rb', line 52

def initialize(*)
  super
  @store.extend Money::RatesStore::RateRemovalSupport
end

Class Attribute Details

.rates_expirationTime (readonly)

Returns the time when the rates expire.

Returns:

  • (Time)

    Returns the time when the rates expire.


32
33
34
# File 'lib/money/bank/google_currency.rb', line 32

def rates_expiration
  @rates_expiration
end

.ttl_in_secondsInteger

Returns the Time To Live (TTL) in seconds.

Returns:

  • (Integer)

    Returns the Time To Live (TTL) in seconds.


29
30
31
# File 'lib/money/bank/google_currency.rb', line 29

def ttl_in_seconds
  @ttl_in_seconds
end

Instance Attribute Details

#ratesHash (readonly)

Returns Stores the currently known rates.

Returns:

  • (Hash)

    Stores the currently known rates.


24
25
26
# File 'lib/money/bank/google_currency.rb', line 24

def rates
  @rates
end

Class Method Details

.refresh_rates_expiration!Time

Set the rates expiration TTL seconds from the current time.

Returns:

  • (Time)

    The next expiration.


47
48
49
# File 'lib/money/bank/google_currency.rb', line 47

def refresh_rates_expiration!
  @rates_expiration = Time.now + ttl_in_seconds
end

Instance Method Details

#expire_ratesBoolean

Flushes all the rates if they are expired.

Returns:

  • (Boolean)

110
111
112
113
114
115
116
117
118
# File 'lib/money/bank/google_currency.rb', line 110

def expire_rates
  if self.class.ttl_in_seconds && self.class.rates_expiration <= Time.now
    flush_rates
    self.class.refresh_rates_expiration!
    true
  else
    false
  end
end

#flush_rate(from, to) ⇒ Float

Clears the specified rate stored in @rates.

Examples:

@bank = GoogleCurrency.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:

  • (Float)

    The flushed rate.


84
85
86
# File 'lib/money/bank/google_currency.rb', line 84

def flush_rate(from, to)
  store.remove_rate(from, to)
end

#flush_ratesHash

Clears all rates stored in @rates

Examples:

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

Returns:

  • (Hash)

    The empty @rates Hash.


66
67
68
# File 'lib/money/bank/google_currency.rb', line 66

def flush_rates
  store.clear_rates
end

#get_rate(from, to) ⇒ Float

Returns the requested rate.

It also flushes all the rates when and if they are expired.

Examples:

@bank = GoogleCurrency.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.


101
102
103
104
# File 'lib/money/bank/google_currency.rb', line 101

def get_rate(from, to)
  expire_rates
  store.get_rate(from, to) || store.add_rate(from, to, fetch_rate(from, to))
end