Class: Money::Bank::TransferwiseBank

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

Overview

TransferwiseBank base class

Constant Summary collapse

TW_SERVICE_HOST =

TransferwiseBank url components

'api.transferwise.com'.freeze
TW_SERVICE_PATH =
'/v1/rates'.freeze
TW_SANDBOX_SERVICE_HOST =
'api.sandbox.transferwise.tech'.freeze
TW_SERVICE_SSL_VERSION =

Default SSL Version

:TLSv1_2
TW_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



49
50
51
# File 'lib/money/bank/transferwise_bank.rb', line 49

def access_key
  @access_key
end

#cacheString, ...

Cache accessor, can be a String or a Proc



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

def cache
  @cache
end

#raise_on_failureBoolean

Option to raise an error on failure to connect to the API or parse the response. By default, this is true, but the ability to disable it is useful when developing without an active internet connection.



150
151
152
153
# File 'lib/money/bank/transferwise_bank.rb', line 150

def raise_on_failure
  @raise_on_failure = true if @raise_on_failure.nil?
  @raise_on_failure
end

#ratesObject (readonly)

Parsed TransferwiseBank result as Hash



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

def rates
  @rates
end

#rates_mem_timestampTime (readonly)

Get the timestamp of rates in memory



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

def rates_mem_timestamp
  @rates_mem_timestamp
end

#service_ssl_versionSymbol

Set the SSL Version used for requests to the API. By default, :TLSv1_2 is used.



134
135
136
# File 'lib/money/bank/transferwise_bank.rb', line 134

def service_ssl_version
  @service_ssl_version ||= TW_SERVICE_SSL_VERSION
end

#ttl_in_secondsInteger

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



127
128
129
# File 'lib/money/bank/transferwise_bank.rb', line 127

def ttl_in_seconds
  @ttl_in_seconds ||= 0
end

#use_sandboxBoolean

Option to use the Sandbox version of the TransferWise API. By default, this is false, and the live API is used.



141
142
143
144
# File 'lib/money/bank/transferwise_bank.rb', line 141

def use_sandbox
  @use_sandbox = false if @use_sandbox.nil?
  @use_sandbox
end

Instance Method Details

#add_rate(from_currency, to_currency, rate) ⇒ Numeric

Override Money ‘add_rate` method for caching



178
179
180
# File 'lib/money/bank/transferwise_bank.rb', line 178

def add_rate(from_currency, to_currency, rate)
  super
end

#expire_rates!Boolean

Fetch new rates if cached rates are expired or stale



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/money/bank/transferwise_bank.rb', line 198

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



212
213
214
# File 'lib/money/bank/transferwise_bank.rb', line 212

def expired?
  Time.now > rates_expiration
end

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

Override Money ‘get_rate` method for caching



190
191
192
193
194
# File 'lib/money/bank/transferwise_bank.rb', line 190

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



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

def rates_expiration
  rates_timestamp + ttl_in_seconds
end

#rates_timestampTime

Get the timestamp of rates from first listed rate



251
252
253
254
# File 'lib/money/bank/transferwise_bank.rb', line 251

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

#service_hostString

Service host of TransferwiseBank API based on value of ‘use_sandbox’ option.



227
228
229
# File 'lib/money/bank/transferwise_bank.rb', line 227

def service_host
  use_sandbox ? TW_SANDBOX_SERVICE_HOST : TW_SERVICE_HOST
end

#sourceString

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



120
121
122
# File 'lib/money/bank/transferwise_bank.rb', line 120

def source
  @source ||= TW_SOURCE
end

#source=(value) ⇒ String

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

Examples:

source = 'USD'


114
115
116
# File 'lib/money/bank/transferwise_bank.rb', line 114

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

#source_urlString

Source url of TransferwiseBank

Raises:



233
234
235
236
237
238
239
240
241
# File 'lib/money/bank/transferwise_bank.rb', line 233

def source_url
  raise NoAccessKey if access_key.nil? || access_key.empty?
  url_componenets = {
    host: service_host,
    path: TW_SERVICE_PATH,
    query: "source=#{source}"
  }
  URI::HTTPS.build(url_componenets)
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.



220
221
222
# File 'lib/money/bank/transferwise_bank.rb', line 220

def stale?
  rates_timestamp != rates_mem_timestamp
end

#super_get_rateObject

Alias super method



183
# File 'lib/money/bank/transferwise_bank.rb', line 183

alias super_get_rate get_rate

#update_rates(straight = false) ⇒ Array

Update all rates from TrasferwiseBank JSON



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/money/bank/transferwise_bank.rb', line 157

def update_rates(straight = false)
  new_rates = exchange_rates(straight)
  return if new_rates.first.empty?
  store.reset!
  rates = new_rates.each do |exchange_rate|
    currency = exchange_rate['target']
    rate = exchange_rate['rate']
    next unless Money::Currency.find(currency)
    add_rate(source, currency, rate)
    add_rate(currency, source, 1.0 / rate)
  end
  add_rate(source, source, 1.0)
  @rates_mem_timestamp = rates_timestamp
  rates
end