CurrencyConversion
Convert Money Amounts between currencies. This library uses an OTP worker to save current conversion rates.
Installation
The package can be installed by adding currency_conversion to your list of dependencies in mix.exs:
def deps do
[{:currency_conversion, "~> 0.3.1"}]
endConfiguration
source- Configure which Data Source Should Be Used.-
Type:
atom -
Default:
CurrencyConversion.Source.Fixer -
Restrictions: Must implement
CurrencyConversion.Sourcebehaviour -
Given Implementations:
CurrencyConversion.Source.Fixer- Fixer IOCurrencyConversion.Source.Test- Test Source
-
Type:
refresh_interval- Configure how often the data should be refreshed. (in ms)-
Type:
integer -
Default:
86_400_000(Once per Day)
-
Type:
test_rates- Configure rates forCurrencyConversion.Source.Testsource-
Type:
{atom, %{atom: float}} -
Default: see
CurrencyConversion.Source.Test.@default_rates -
Example:
{:EUR, %{CHF: 7}}
-
Type:
config :currency_conversion,
source: CurrencyConversion.Source.Fixer,
source_api_key: "FIXER_ACCESS_KEY",
# defaults to http since free access key only supports http
source_protocol: "https",
refresh_interval: 86_400_000Custom Source
A custom source can be implemented by using the behaviour CurrencyConversion.Source and reconfiguring the source config.
It only has to implement the function load/0, which produces a struct of type %CurrencyConversion.Rates{}.
Test
To prevent HTTP calls in the Tests, configure the Test Source. (See the configuration test_rates for custom test rates.)
config :currency_conversion,
source: CurrencyConversion.Source.Test,
refresh_interval: 86_400_000Usage
Only the function CurrencyConversion.convert/3 is exposed to the user. The library money is used to represent money amounts.
Example
iex> CurrencyConversion.convert(Money.new(7_00, :CHF), :USD)
%Money{amount: 10_50, currency: :USD}