Class: Money::Bank::CurrencylayerBank

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

Overview

CurrencylayerBank base class

Constant Summary collapse

CL_URL =

CurrencylayerBank url

'http://apilayer.net/api/live'.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/currencylayer_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/currencylayer_bank.rb', line 58

def cache
  @cache
end

#ratesObject (readonly)

Parsed CurrencylayerBank result as Hash


61
62
63
# File 'lib/money/bank/currencylayer_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/currencylayer_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/currencylayer_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


99
100
101
# File 'lib/money/bank/currencylayer_bank.rb', line 99

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.


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

def add_rate(from_currency, to_currency, rate)
  super
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


143
144
145
146
147
148
149
150
151
152
153
# File 'lib/money/bank/currencylayer_bank.rb', line 143

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


157
158
159
# File 'lib/money/bank/currencylayer_bank.rb', line 157

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.


135
136
137
138
139
# File 'lib/money/bank/currencylayer_bank.rb', line 135

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


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

def rates_expiration
  rates_timestamp + ttl_in_seconds
end

#rates_timestampTime

Get the timestamp of rates

Returns:

  • (Time)

    time object or nil


187
188
189
190
# File 'lib/money/bank/currencylayer_bank.rb', line 187

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/currencylayer_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/currencylayer_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:


172
173
174
175
176
177
# File 'lib/money/bank/currencylayer_bank.rb', line 172

def source_url
  raise NoAccessKey if access_key.nil? || access_key.empty?
  cl_url = CL_URL
  cl_url = CL_SECURE_URL if secure_connection
  "#{cl_url}?source=#{source}&access_key=#{access_key}"
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


165
166
167
# File 'lib/money/bank/currencylayer_bank.rb', line 165

def stale?
  rates_timestamp != rates_mem_timestamp
end

#super_get_rateObject

Alias super method


128
# File 'lib/money/bank/currencylayer_bank.rb', line 128

alias super_get_rate get_rate

#update_rates(straight = false) ⇒ Array

Update all rates from CurrencylayerBank JSON

Returns:

  • (Array)

    array of exchange rates


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/money/bank/currencylayer_bank.rb', line 105

def update_rates(straight = false)
  store.reset!
  rates = exchange_rates(straight).each do |exchange_rate|
    currency = exchange_rate.first[3..-1]
    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