Class: OmniExchange::Provider
- Inherits:
-
Object
- Object
- OmniExchange::Provider
- Defined in:
- lib/omni_exchange/provider.rb
Direct Known Subclasses
Class Method Summary collapse
-
.all ⇒ Object
a method that gives access to the @providers hash and the providers registered in it.
-
.get_currency_unit(base_currency) ⇒ Float
Some currencies, such as the US dollar, have subunits (ie. cents).
-
.get_exchange_rate(base_currency:, target_currency:) ⇒ Object
Each provider class should inherit from Provider and have a .get_exchange_rates method.
-
.load_provider(provider_name) ⇒ Class
This method is called in the .exchange_currency method.
-
.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.
Class Method Details
.all ⇒ Object
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.
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.
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.
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 |