CurrencyConversion behaviour (currency_conversion v1.0.0) View Source
Module to Convert Currencies.
CurrencyConversion
is a wrapper around the currency conversion. We can define an
implementation as follows:
defmodule MyApp.CurrencyConversion do
use CurrencyConversion, otp_app: :my_app
end
Where the configuration for the Converter must be in your application environment,
usually defined in your config/config.exs
:
config :my_app, MyApp.CurrencyConversion,
source: MyApp.CurrencyConversion.Source.CustomSource
If your application was generated with a supervisor (by passing --sup
to mix new
)
you will have a lib/my_app/application.ex
file containing the application start
callback that defines and starts your supervisor. You just need to edit the start/2
function to start the converter as a supervisor on your application's supervisor:
def start(_type, _args) do
children = [
{MyApp.CurrencyConversion, []}
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
Link to this section Summary
Callbacks
Convert from currency A to B.
Get all currencies
Get current exchange rates
Refresh exchange rates
Link to this section Callbacks
Specs
Convert from currency A to B.
Example
iex> convert(Money.new(7_00, :CHF), :USD)
%Money{amount: 7_03, currency: :USD}
Specs
get_currencies() :: [atom()]
Get all currencies
Examples
iex> get_currencies()
[:EUR, :CHF, :USD, ...]
Specs
get_rates() :: CurrencyConversion.Rates.t()
Get current exchange rates
Examples
iex> get_rates()
%CurrencyConversion.Rates{
base: :EUR,
rates: %{
AUD: 1.4205,
BGN: 1.9558,
...
}
}
Specs
refresh_rates() :: :ok | {:error, term()}
Refresh exchange rates
Examples
iex> refresh_rates()
:ok