module SevenBankFxRate

Easier access to the foreign exchange rate data of Seven Bank international money transfer service in Japan, with JPY1.00 as the base currency and unit.

Example of usage:

Method patterns:

SevenBankFxRate.for :xx, :yyy
SevenBankFxRate.find 'xx', 'yyy'
SevenBankFxRate.country_xx_currency_yyy

where xx is country code and yyy is currency code; both symbol and string can be used for either code with case-insensitive formats: e.g.

SevenBankFxRate.for :cn, :cny
SevenBankFxRate.for 'cn', 'usd'
SevenBankFxRate.for :cn, :CNY
SevenBankFxRate.for :CN, :usd
SevenBankFxRate.find :cn, :cny
SevenBankFxRate.find 'cn', 'usd'
SevenBankFxRate.country_cn_currency_cny
SevenBankFxRate.country_CN_currency_USD

Country codes reference:

https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

Currency codes reference:

https://en.wikipedia.org/wiki/ISO_4217

Constants

GHOST_METHOD_PATTERN
SOURCE_URL
VERSION

Public Class Methods

all() click to toggle source

@return exchange rate data for all available countries

as an array of SevenBankFxRate::Elements::Country

# File lib/seven_bank_fx_rate/seven_bank_fx_rate.rb, line 34
def all
  data.countries
end
find(country_code, currency_code)
Alias for: for
for(country_code, currency_code) click to toggle source

@return exchange rate for given country code and currency code or nil

if either country or currency code is invalid or when there is no exchange rate data available for the country and currency combination

# File lib/seven_bank_fx_rate/seven_bank_fx_rate.rb, line 17
def for(country_code, currency_code)
  country = all.find do |c|
    c.country_code == country_code.to_s.upcase
  end
  return unless country

  currency = country.currencies.find do |c|
    c.currency_code == currency_code.to_s.upcase
  end
  currency&.fx_rate
end
Also aliased as: find
update!() click to toggle source

Upon accessing of any of the public methods, the exchange rate data are fetched once and then cached for the lifetime of the module, because the remote xml source is updated only 3 times a day based on the website.

However, this method still allows fetching the latest data when needed.

# File lib/seven_bank_fx_rate/seven_bank_fx_rate.rb, line 43
def update!
  @data = Data.new
  true
end

Private Class Methods

data() click to toggle source
# File lib/seven_bank_fx_rate/seven_bank_fx_rate.rb, line 50
def data
  @data || @mutex.synchronize do
    @data ||= Data.new
  end
end
method_missing(method_name, *arguments, &block) click to toggle source
Calls superclass method
# File lib/seven_bank_fx_rate/seven_bank_fx_rate.rb, line 58
def method_missing(method_name, *arguments, &block)
  if GHOST_METHOD_PATTERN =~ method_name.to_s
    _, country_code, _, currency_code = method_name.to_s.split('_')
    send :for, country_code, currency_code
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/seven_bank_fx_rate/seven_bank_fx_rate.rb, line 67
def respond_to_missing?(method_name, include_private = false)
  !!(GHOST_METHOD_PATTERN =~ method_name.to_s) || super
end