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/'.freeze
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'.freeze
RATES_KEY =
'rates'.freeze
TIMESTAMP_KEY =
'timestamp'.freeze

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



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

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



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

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



62
63
64
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 62

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)


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

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



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

def json_response
  @json_response
end

#oer_ratesHash (readonly)

Parsed OpenExchangeRates result as Hash

Returns:

  • (Hash)

    All rates as Hash



79
80
81
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 79

def oer_rates
  @oer_rates
end

#prettyprintBoolean

Get prettyprint option

Returns:

  • (Boolean)


235
236
237
238
239
240
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 235

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

  @prettyprint
end

#rates_expirationTime (readonly)

Rates expiration Time

Returns:

  • (Time)

    expiration time



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

def rates_expiration
  @rates_expiration
end

#show_alternativeBoolean

Get show alternative

Returns:

  • (Boolean)

    if true show alternative



228
229
230
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 228

def show_alternative
  @show_alternative ||= false
end

#symbolsArray

Get symbols

Returns:

  • (Array)

    list of symbols to filter by



245
246
247
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 245

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



89
90
91
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 89

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



216
217
218
219
220
221
222
223
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 216

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.



196
197
198
199
200
201
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 196

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)


128
129
130
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 128

def rates_timestamp
  @rates_timestamp || Time.now
end

#rates_timestamp=(at) ⇒ Time

Set current rates timestamp

Returns:

  • (Time)


121
122
123
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 121

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



206
207
208
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 206

def refresh_rates
  read_from_url
end

#sourceString

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

Returns:

  • (String)

    base currency



169
170
171
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 169

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



157
158
159
160
161
162
163
164
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 157

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



253
254
255
256
257
258
259
260
261
262
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 253

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}"
  if symbols && symbols.is_a?(Array)
    str = "#{str}&symbols=#{symbols.join(',')}"
  end
  str
end

#super_get_rateObject

Alias super method



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

alias super_get_rate get_rate

#update_ratesArray

Update all rates from openexchangerates JSON

Returns:

  • (Array)

    Array of exchange rates



176
177
178
179
180
181
182
183
184
185
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 176

def update_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