Module: SevenBankFxRate

Defined in:
lib/seven_bank_fx_rate.rb,
lib/seven_bank_fx_rate/agent.rb,
lib/seven_bank_fx_rate/parser.rb,
lib/seven_bank_fx_rate/version.rb,
lib/seven_bank_fx_rate/elements/meta.rb,
lib/seven_bank_fx_rate/elements/country.rb,
lib/seven_bank_fx_rate/elements/currency.rb,
lib/seven_bank_fx_rate/seven_bank_fx_rate.rb

Overview

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:

  • Get meta information: SevenBankFxRate.create_date SevenBankFxRate.apply_date SevenBankFxRate.apply_time SevenBankFxRate.data_count

  • Get exchange rate with specific country code and currency code:

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
  • Get an array of exchange rate for all available countries and currencies: SevenBankFxRate.all

  • Forcibly update latest data: SevenBankFxRate.update!

Defined Under Namespace

Modules: Elements Classes: Agent, Data, Parser

Constant Summary collapse

SOURCE_URL =
'https://www.sevenbank.co.jp/t/html/file/CurrentFXList.xml'
VERSION =
'1.1.1'

Class Method Summary collapse

Class Method Details

.allObject

as an array of SevenBankFxRate::Elements::Country

Returns:

  • exchange rate data for all available countries


34
35
36
# File 'lib/seven_bank_fx_rate/seven_bank_fx_rate.rb', line 34

def all
  data.countries
end

.for(country_code, currency_code) ⇒ Object Also known as: find

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

Returns:

  • exchange rate for given country code and currency code or nil


17
18
19
20
21
22
23
24
25
26
27
# 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

.update!Object

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.


43
44
45
46
# File 'lib/seven_bank_fx_rate/seven_bank_fx_rate.rb', line 43

def update!
  @data = Data.new
  true
end