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:
-
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!
Constants
- GHOST_METHOD_PATTERN
- SOURCE_URL
- VERSION
Public Class Methods
@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
@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
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
# File lib/seven_bank_fx_rate/seven_bank_fx_rate.rb, line 50 def data @data || @mutex.synchronize do @data ||= Data.new end end
# 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
# 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