Class: Money::Bank::JsonRates

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

Overview

Money::Bank implementation that gives access to the current exchange rates using jsonrates.com api.

Constant Summary collapse

SERVICE_HOST =

Host of service jsonrates

"jsonrates.com"
SERVICE_PATH =

Relative path of jsonrates api

"/get"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.rates_carefulBoolean

Returns is Rates Careful mode set.

Returns:

  • (Boolean)

    Returns is Rates Careful mode set.



46
47
48
# File 'lib/money/bank/json_rates.rb', line 46

def rates_careful
  @rates_careful
end

.rates_expirationTime (readonly)

Returns the time when the rates expire.

Returns:

  • (Time)

    Returns the time when the rates expire.



43
44
45
# File 'lib/money/bank/json_rates.rb', line 43

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.



40
41
42
# File 'lib/money/bank/json_rates.rb', line 40

def ttl_in_seconds
  @ttl_in_seconds
end

Instance Attribute Details

#api_keyObject

accessor of api_key of jsonrates.com service



36
37
38
# File 'lib/money/bank/json_rates.rb', line 36

def api_key
  @api_key
end

#ratesHash (readonly)

Returns Stores the currently known rates.

Returns:

  • (Hash)

    Stores the currently known rates.



33
34
35
# File 'lib/money/bank/json_rates.rb', line 33

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.



69
70
71
# File 'lib/money/bank/json_rates.rb', line 69

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

Instance Method Details

#add_rate(from, to, rate) ⇒ Numeric

Registers a conversion rate and returns it (uses #set_rate).

Examples:

bank = Money::Bank::JsonRates.new  #=> <Money::Bank::JsonRates...>
bank.add_rate("USD", "CAD", 1.24515)  #=> 1.24515
bank.add_rate("CAD", "USD", 0.803115)  #=> 0.803115

Parameters:

  • from (Currency, String, Symbol)

    Currency to exchange from.

  • to (Currency, String, Symbol)

    Currency to exchange to.

  • rate (Numeric)

    Rate to use when exchanging currencies.

Returns:

  • (Numeric)


143
144
145
# File 'lib/money/bank/json_rates.rb', line 143

def add_rate from, to, rate
  set_rate from, to, rate
end

#expire_ratesBoolean

Flushes all the rates if they are expired.

Returns:

  • (Boolean)


194
195
196
197
198
199
200
201
202
# File 'lib/money/bank/json_rates.rb', line 194

def expire_rates
  if expired?
    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 = Money::Bank::JsonRates.new    #=> <Money::Bank::JsonRates...>
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.



103
104
105
106
107
108
# File 'lib/money/bank/json_rates.rb', line 103

def flush_rate(from, to)
  key = rate_key_for(from, to)
  @mutex.synchronize{
    @rates.delete(key)
  }
end

#flush_ratesHash

Clears all rates stored in @rates

Examples:

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

Returns:

  • (Hash)

    The empty @rates Hash.



83
84
85
86
87
# File 'lib/money/bank/json_rates.rb', line 83

def flush_rates
  @mutex.synchronize{
    @rates = {}
  }
end

#get_rate(from, to) ⇒ Float

Returns the requested rate.

It uses #get_rate_careful or #get_rate_straight respect of @rates_careful value

Examples:

bank = Money::Bank::JsonRates.new  #=> <Money::Bank::JsonRates...>
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.



123
124
125
126
127
128
129
# File 'lib/money/bank/json_rates.rb', line 123

def get_rate(from, to)
  if self.class.rates_careful
    get_rate_careful(from, to)
  else
    get_rate_straight(from, to)
  end
end

#rate_key_for(from, to) ⇒ String

Return the rate hashkey for the given currencies.

Examples:

rate_key_for("USD", "CAD") #=> "USD_TO_CAD"
Money::Bank::JsonRates.rates_careful = true
rate_key_for("USD", "CAD") #=> "USD_TO_CAD_C"

Parameters:

  • from (Currency, String, Symbol)

    The currency to exchange from.

  • to (Currency, String, Symbol)

    The currency to exchange to.

Returns:

  • (String)


182
183
184
185
186
187
188
# File 'lib/money/bank/json_rates.rb', line 182

def rate_key_for(from, to)
  if self.class.rates_careful
    "#{Currency.wrap(from).iso_code}_TO_#{Currency.wrap(to).iso_code}_C".upcase
  else
    super
  end
end

#set_rate(from, to, rate) ⇒ Numeric

Set the rate for the given currencies. Uses Mutex to synchronize data access.

Examples:

@bank = Money::Bank::JsonRates.new  #=> <Money::Bank::JsonRates...>
bank.set_rate("USD", "CAD", 1.24515)  #=> 1.24515
bank.set_rate("CAD", "USD", 0.803115)  #=> 0.803115

Parameters:

  • from (Currency, String, Symbol)

    Currency to exchange from.

  • to (Currency, String, Symbol)

    Currency to exchange to.

  • rate (Numeric)

    Rate to use when exchanging currencies.

  • opts (Hash)

    Options hash to set special parameters

Returns:

  • (Numeric)


162
163
164
165
166
167
168
# File 'lib/money/bank/json_rates.rb', line 162

def set_rate from, to, rate
  if self.class.rates_careful
    set_rate_with_time(from, to, rate)
  else
    super
  end
end