Class: OmniExchange::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/omni_exchange/provider.rb

Direct Known Subclasses

OpenExchangeRates, Xe

Class Method Summary collapse

Class Method Details

.allObject

a method that gives access to the @providers hash and the providers registered in it



33
34
35
# File 'lib/omni_exchange/provider.rb', line 33

def self.all
  @providers
end

.get_currency_unit(base_currency) ⇒ Float

Some currencies, such as the US dollar, have subunits (ie. cents). Therefore, to make sure that currencies are

exchanged accurately, a currency's subunit needs to be taken into account, and that's what this method does.
Subunit data is easily found through use of the RubyMoney gem.

Parameters:

  • base_currency (String)

    the ISO Currency Code of the currency that you’re exchanging from. A check is done on this currency to see if it has subunits (such as the US dollar having cents). ie. “USD”, “JPY”

Returns:

  • (Float)

    the amount an exchange rate should be multiplied by to account for a currency’s potential subunits



50
51
52
# File 'lib/omni_exchange/provider.rb', line 50

def self.get_currency_unit(base_currency)
  1.0 / Money::Currency.wrap(base_currency).subunit_to_unit
end

.get_exchange_rate(base_currency:, target_currency:) ⇒ Object

Each provider class should inherit from Provider and have a .get_exchange_rates method. If a provider class

doesn't have a .get_exchange_rates method, the method below will be called and an error will be raised.


39
40
41
# File 'lib/omni_exchange/provider.rb', line 39

def self.get_exchange_rate(base_currency:, target_currency:) # rubocop:disable Lint/UnusedMethodArgument
  raise 'method not implemented...'
end

.load_provider(provider_name) ⇒ Class

This method is called in the .exchange_currency method. It returns the provider that is requested if the provider

is registered in the @providers hash. Once loaded, class methods (such as .get_exchange_rate) can be called on
the provider. However, if the provider is unregistered, a LoadError is raised.

Parameters:

  • provider_name (Symbol)

    the name of the exchange rate API provider that is to be loaded. ie. :xe

Returns:

  • (Class)

    the provider is returned if it has been registered properly in the @providers hash. Otherwise, a LoadError exception is raised

Raises:

  • (LoadError.new)


25
26
27
28
29
30
# File 'lib/omni_exchange/provider.rb', line 25

def self.load_provider(provider_name)
  provider = @providers[provider_name]
  return provider if provider

  raise(LoadError.new, "#{provider_name} did not load properly as a provider")
end

.register_provider(provider_name, provider_module) ⇒ Object

This method registers providers by adding a provider’s name as a key and a provider class as a value to

@providers. This happens automatically on load at the top of the lib/omni_exchange.rb file when each
file in the lib/omni_exchange/providers folder becomes required.

Parameters:

  • provider_name (Symbol)

    the name of the exchange rate API data provider. ie. :xe, :open_exchange_rates

  • provider_module (Class)

    the class of the provider. ie. OmniExchange::Xe, OmniExchange::OpenExchangeRates



14
15
16
# File 'lib/omni_exchange/provider.rb', line 14

def self.register_provider(provider_name, provider_module)
  @providers[provider_name] = provider_module
end