Class: Money::Bank::OpenexchangeratesBank

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

Overview

CurrencylayerBank base class

Constant Summary collapse

CL_URL =

CurrencylayerBank url

'http://openexchangerates.org/api/latest.json'.freeze
CL_SECURE_URL =

CurrencylayerBank secure url

CL_URL.sub('http:', 'https:')
CL_SOURCE =

Default base currency

'USD'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_keyString

API must have a valid access_key

Parameters:

  • value (String)

    API access key

Returns:

  • (String)

    chosen API access key



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

def access_key
  @access_key
end

#cacheString, ...

Cache accessor, can be a String or a Proc

Parameters:

  • value (String, Pathname, Proc)

    cache system

Returns:

  • (String, Pathname, Proc)

    chosen cache system



58
59
60
# File 'lib/money/bank/openexchangerates_bank.rb', line 58

def cache
  @cache
end

#ratesObject (readonly)

Parsed CurrencylayerBank result as Hash



61
62
63
# File 'lib/money/bank/openexchangerates_bank.rb', line 61

def rates
  @rates
end

#rates_mem_timestampTime (readonly)

Get the timestamp of rates in memory

Returns:

  • (Time)

    time object or nil



65
66
67
# File 'lib/money/bank/openexchangerates_bank.rb', line 65

def rates_mem_timestamp
  @rates_mem_timestamp
end

#secure_connectionBoolean

Use https to fetch rates from CurrencylayerBank CurrencylayerBank only allows http as connection for the free plan users.

Parameters:

  • value (Boolean)

    true for secure connection

Returns:

  • (Boolean)

    chosen secure connection



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

def secure_connection
  @secure_connection
end

#ttl_in_secondsInteger

Get the seconds after than the current rates are automatically expired by default, they never expire.

Returns:

  • (Integer)

    chosen time to live in seconds



123
124
125
# File 'lib/money/bank/openexchangerates_bank.rb', line 123

def ttl_in_seconds
  @ttl_in_seconds ||= 0
end

Instance Method Details

#add_rate(from_currency, to_currency, rate) ⇒ Numeric

Override Money ‘add_rate` method for caching

Parameters:

  • from_currency (String)

    Currency ISO code. ex. ‘USD’

  • to_currency (String)

    Currency ISO code. ex. ‘CAD’

  • rate (Numeric)

    Rate to use when exchanging currencies.

Returns:

  • (Numeric)

    rate.



147
148
149
# File 'lib/money/bank/openexchangerates_bank.rb', line 147

def add_rate(from_currency, to_currency, rate)
  super
end

#alternativesString?

Get alternative system. Default is nil

Returns:

  • (String, nil)

    chosen alternative system



116
117
118
# File 'lib/money/bank/openexchangerates_bank.rb', line 116

def alternatives
  @alternatives || nil
end

#alternatives=(system) ⇒ String?

Set alternative system, to request alternative rates

Examples:

alternatives = :all
alternatives = :only
alternatives = nil

Parameters:

  • value (String, Symbol)

    alternatives

Returns:

  • (String, nil)

    chosen alternative system



105
106
107
108
109
110
111
112
# File 'lib/money/bank/openexchangerates_bank.rb', line 105

def alternatives=(system)
  @alternatives = case system.to_sym
                  when :all
                    'show_alternative'
                  when :only
                    'only_alternative'
                  end
end

#expire_rates!Boolean

Fetch new rates if cached rates are expired or stale

Returns:

  • (Boolean)

    true if rates are expired and updated from remote



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/money/bank/openexchangerates_bank.rb', line 167

def expire_rates!
  if expired?
    update_rates(true)
    true
  elsif stale?
    update_rates
    true
  else
    false
  end
end

#expired?Boolean

Check if rates are expired

Returns:

  • (Boolean)

    true if rates are expired



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

def expired?
  Time.now > 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’

  • opts (Hash) (defaults to: {})

    Options hash to set special parameters.

Returns:

  • (Numeric)

    rate.



159
160
161
162
163
# File 'lib/money/bank/openexchangerates_bank.rb', line 159

def get_rate(from_currency, to_currency, opts = {})
  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_expirationTime

Get rates expiration time based on ttl

Returns:

  • (Time)

    rates expiration time



207
208
209
# File 'lib/money/bank/openexchangerates_bank.rb', line 207

def rates_expiration
  rates_timestamp + ttl_in_seconds
end

#rates_timestampTime

Get the timestamp of rates

Returns:

  • (Time)

    time object or nil



213
214
215
216
# File 'lib/money/bank/openexchangerates_bank.rb', line 213

def rates_timestamp
  raw = raw_rates_careful
  raw.key?('timestamp') ? Time.at(raw['timestamp']) : Time.at(0)
end

#sourceString

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

Returns:

  • (String)

    base currency



92
93
94
# File 'lib/money/bank/openexchangerates_bank.rb', line 92

def source
  @source ||= CL_SOURCE
end

#source=(value) ⇒ String

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

Examples:

source = 'USD'

Parameters:

  • value (String)

    Currency code, ISO 3166-1 alpha-3

Returns:

  • (String)

    chosen base currency



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

def source=(value)
  @source = Money::Currency.find(value.to_s).try(:iso_code) || CL_SOURCE
end

#source_urlString

Source url of CurrencylayerBank defined with access_key and secure_connection

Returns:

  • (String)

    the remote API url

Raises:



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

def source_url
  raise NoAccessKey if access_key.nil? || access_key.empty?
  cl_url = CL_URL
  cl_url = CL_SECURE_URL if secure_connection
  params = "#{cl_url}?base=#{source}&app_id=#{access_key}&prettyprint=0"
  params += "&#{alternatives}=1" unless alternatives.nil?
  params
end

#stale?Boolean

Check if rates are stale Stale is true if rates are updated straight by another thread. The actual thread has always old rates in memory store.

Returns:

  • (Boolean)

    true if rates are stale



189
190
191
# File 'lib/money/bank/openexchangerates_bank.rb', line 189

def stale?
  rates_timestamp != rates_mem_timestamp
end

#super_get_rateObject

Alias super method



152
# File 'lib/money/bank/openexchangerates_bank.rb', line 152

alias super_get_rate get_rate

#update_rates(straight = false) ⇒ Array

Update all rates from CurrencylayerBank JSON

Returns:

  • (Array)

    array of exchange rates



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/money/bank/openexchangerates_bank.rb', line 129

def update_rates(straight = false)
  store.reset!
  rates = exchange_rates(straight).each do |exchange_rate|
    currency = exchange_rate.first
    rate = exchange_rate.last
    next unless Money::Currency.find(currency)
    add_rate(source, currency, rate)
    add_rate(currency, source, 1.0 / rate)
  end
  @rates_mem_timestamp = rates_timestamp
  rates
end