Convert Money Amounts between currencies. This library uses an OTP worker to save current conversion rates.
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"}]
end
source
- Configure which Data Source Should Be Used.- Type:
atom
- Default:
CurrencyConversion.Source.Fixer
- Restrictions: Must implement
CurrencyConversion.Source
behaviour - 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.Test
source- 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_000
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{}
.
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_000
Only the function CurrencyConversion.convert/3
is exposed to the user. The library money is used to represent money amounts.
iex> CurrencyConversion.convert(Money.new(7_00, :CHF), :USD)
%Money{amount: 10_50, currency: :USD}