Class: Money::Bank::OpenExchangeRatesBank

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

Overview

OpenExchangeRatesBank base class

Constant Summary collapse

VERSION =
::OpenExchangeRatesBank::VERSION
BASE_URL =
'https://openexchangerates.org/api/'
OER_URL =

OpenExchangeRates urls

URI.join(BASE_URL, 'latest.json')
OER_HISTORICAL_URL =
URI.join(BASE_URL, 'historical/')
OE_SOURCE =

Default base currency “base”: “USD”

'USD'
RATES_KEY =
'rates'
TIMESTAMP_KEY =
'timestamp'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_idString

As of the end of August 2012 all requests to the Open Exchange Rates API must have a valid app_id see docs.openexchangerates.org/docs/authentication

Examples:

oxr.app_id = 'YOUR_APP_APP_ID'

Parameters:

  • token (String)

    to access OXR API

Returns:

  • (String)

    token to access OXR API



55
56
57
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 55

def app_id
  @app_id
end

#cacheString, Proc

Cache accessor

Examples:

oxr.cache = 'path/to/file/cache.json'

Parameters:

  • for (String, Proc)

    a String a filepath

Returns:

  • (String, Proc)

    for a String a filepath



64
65
66
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 64

def cache
  @cache
end

#dateString

Examples:

oxr.date = '2015-01-01'

Parameters:

  • The (String)

    requested date in YYYY-MM-DD format

Returns:

  • (String)

    The requested date in YYYY-MM-DD format



74
75
76
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 74

def date
  @date
end

#force_refresh_rate_on_expireObject

Force refresh rates cache and store on the fly when ttl is expired This will slow down request on get_rate, so use at your on risk, if you don’t want to setup crontab/worker/scheduler for your application

Parameters:

  • (Boolean)


81
82
83
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 81

def force_refresh_rate_on_expire
  @force_refresh_rate_on_expire
end

#json_responseString (readonly)

Unparsed OpenExchangeRates response as String

Returns:

  • (String)

    OpenExchangeRates json response



96
97
98
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 96

def json_response
  @json_response
end

#oer_ratesHash (readonly)

Parsed OpenExchangeRates result as Hash

Returns:

  • (Hash)

    All rates as Hash



91
92
93
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 91

def oer_rates
  @oer_rates
end

#prettyprintBoolean

Get prettyprint option

Returns:

  • (Boolean)


250
251
252
253
254
255
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 250

def prettyprint
  return true unless defined? @prettyprint
  return true if @prettyprint.nil?

  @prettyprint
end

#rates_expirationTime (readonly)

Rates expiration Time

Returns:

  • (Time)

    expiration time



86
87
88
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 86

def rates_expiration
  @rates_expiration
end

#show_alternativeBoolean

Get show alternative

Returns:

  • (Boolean)

    if true show alternative



243
244
245
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 243

def show_alternative
  @show_alternative ||= false
end

#symbolsArray

Get symbols

Returns:

  • (Array)

    list of symbols to filter by



260
261
262
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 260

def symbols
  @symbols ||= nil
end

#ttl_in_secondsInteger

Seconds after than the current rates are automatically expired

Returns:

  • (Integer)

    Setted time to live in seconds



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

def ttl_in_seconds
  @ttl_in_seconds
end

Instance Method Details

#expire_ratesNilClass, Time

Expire rates when expired

Returns:

  • (NilClass, Time)

    nil if not expired or new expiration time



231
232
233
234
235
236
237
238
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 231

def expire_rates
  return unless ttl_in_seconds
  return if rates_expiration > Time.now

  refresh_rates if force_refresh_rate_on_expire
  update_rates
  refresh_rates_expiration
end

#get_rate(from_currency, to_currency, opts = {}) ⇒ Numeric

Override Money ‘get_rate` method for caching

Parameters:

  • from_currency (String)

    Currency ISO code. ex. ‘USD’

  • to_currency (String)

    Currency ISO code. ex. ‘CAD’

Returns:

  • (Numeric)

    rate.



211
212
213
214
215
216
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 211

def get_rate(from_currency, to_currency, opts = {})
  super if opts[:call_super]
  expire_rates
  rate = get_rate_or_calc_inverse(from_currency, to_currency, opts)
  rate || calc_pair_rate_using_base(from_currency, to_currency, opts)
end

#rates_timestampTime

Current rates timestamp

Returns:

  • (Time)


140
141
142
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 140

def rates_timestamp
  @rates_timestamp || Time.now
end

#rates_timestamp=(at) ⇒ Time

Set current rates timestamp

Returns:

  • (Time)


133
134
135
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 133

def rates_timestamp=(at)
  @rates_timestamp = Time.at(at)
end

#refresh_ratesArray Also known as: save_rates

Fetch from url and save cache

Returns:

  • (Array)

    Array of exchange rates



221
222
223
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 221

def refresh_rates
  read_from_url
end

#sourceString

Get the base currency for all rates. By default, USD is used.

Returns:

  • (String)

    base currency



181
182
183
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 181

def source
  @source ||= OE_SOURCE
end

#source=(value) ⇒ String

Set the base currency for all rates. By default, USD is used. OpenExchangeRates only allows USD as base currency for the free plan users.

Examples:

oxr.source = 'USD'

Parameters:

  • value (String)

    Currency code, ISO 3166-1 alpha-3

Returns:

  • (String)

    chosen base currency



169
170
171
172
173
174
175
176
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 169

def source=(value)
  scurrency = Money::Currency.find(value.to_s)
  @source = if scurrency
              scurrency.iso_code
            else
              OE_SOURCE
            end
end

#source_urlString

Source url of openexchangerates defined with app_id

Returns:

  • (String)

    URL



268
269
270
271
272
273
274
275
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 268

def source_url
  str = "#{oer_url}?app_id=#{app_id}"
  str = "#{str}&base=#{source}" unless source == OE_SOURCE
  str = "#{str}&show_alternative=#{show_alternative}"
  str = "#{str}&prettyprint=#{prettyprint}"
  str = "#{str}&symbols=#{symbols.join(',')}" if symbols&.is_a?(Array)
  str
end

#super_get_rateObject

Alias super method



203
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 203

alias super_get_rate get_rate

#update_ratesArray

Update all rates from openexchangerates JSON

Returns:

  • (Array)

    Array of exchange rates



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 188

def update_rates
  store.transaction do
    clear_rates!
    exchange_rates.each do |exchange_rate|
      rate = exchange_rate.last
      currency = exchange_rate.first
      next unless Money::Currency.find(currency)

      set_rate(source, currency, rate)
      set_rate(currency, source, 1.0 / rate)
    end
  end
end