open_exchange_rates v0.3.0 OpenExchangeRates

This module contains all the helper methods for converting currencies

Summary

Functions

Returns a list of all available currencies

Returns the age of the cache in seconds

Get the conversion rate for a between two currencies”

Will convert a price from once currency to another

bang method of convert/3 Will either return the result or raise when there was an error

examples

Converts a price and returns a properly formatted string for the given currency

Bang version of convert_and_format/3 Will either return the result or raise when there was an error

Will convert cents from once currency to another

bang method of convert_cents/3 Will either return the result or raise when there was an error

example

Converts cents and returns a properly formatted string for the given currency

Bang version of convert_cents_and_format/3 Will either return the result or raise when there was an error

Functions

available_currencies()

Specs

available_currencies :: [String.t]

Returns a list of all available currencies.

example

iex> OpenExchangeRates.available_currencies |> Enum.take(10)
["AWG", "NAD", "INR", "LAK", "BOB", "MOP", "QAR", "SDG", "TMT", "BRL"]
cache_age()

Specs

cache_age :: Integer.t

Returns the age of the cache in seconds

example

OpenExchangeRates.cache_age
25341
conversion_rate(from, to)

Specs

conversion_rate(String.t | Atom.t, String.t | Atom.t) ::
  {:ok, Float.t} |
  {:error, String.t}

Get the conversion rate for a between two currencies”

Example

iex> OpenExchangeRates.conversion_rate(:EUR, :GBP)
{:ok, 0.8481186252771619}
convert(value, from, to)

Specs

convert(Integer.t, String.t | Atom.t, String.t | Atom.t) ::
  {:ok, Float.t} |
  {:error, String.t}
convert(Float.t, String.t | Atom.t, String.t | Atom.t) ::
  {:ok, Float.t} |
  {:error, String.t}

Will convert a price from once currency to another

example

iex> OpenExchangeRates.convert(100.00, :EUR, :GBP)
{:ok, 84.81186252771619}
convert!(value, from, to)

Specs

convert!(Integer.t, String.t | Atom.t, String.t | Atom.t) ::
  {:ok, Float.t} |
  {:error, String.t}
convert!(Float.t, String.t | Atom.t, String.t | Atom.t) ::
  {:ok, Float.t} |
  {:error, String.t}

bang method of convert/3 Will either return the result or raise when there was an error

examples

iex> OpenExchangeRates.convert!(100.00, :EUR, :GBP)
84.81186252771619

iex> OpenExchangeRates.convert!(100, :EUR, :GBP)
84.81186252771619
convert_and_format(value, from, to)

Specs

convert_and_format(Integer.t | Float.t, Atom.t | String.t, Atom.t | String.t) :: String.t

Converts a price and returns a properly formatted string for the given currency.

Examples

iex> OpenExchangeRates.convert_and_format(1234, :EUR, :AUD)
{:ok, "A$1,795.10"}
convert_and_format!(value, from, to)

Specs

convert_and_format!(Integer.t | Float.t, Atom.t | String.t, Atom.t | String.t) :: String.t

Bang version of convert_and_format/3 Will either return the result or raise when there was an error

#example

iex> OpenExchangeRates.convert_and_format!(1234567, :EUR, :USD)
"$1,368,699.56"
convert_cents(value, from, to)

Specs

convert_cents(Integer.t, String.t | Atom.t, String.t | Atom.t) ::
  {:ok, Integer.t} |
  {:error, String.t}

Will convert cents from once currency to another

example

iex> OpenExchangeRates.convert_cents(100, :GBP, :AUD)
{:ok, 172}
convert_cents!(value, from, to)

Specs

convert_cents!(Integer.t, String.t | Atom.t, String.t | Atom.t) ::
  {:ok, Integer.t} |
  {:error, String.t}

bang method of convert_cents/3 Will either return the result or raise when there was an error

example

iex> OpenExchangeRates.convert_cents!(100, :GBP, :AUD)
172
convert_cents_and_format(value, from, to)

Specs

convert_cents_and_format(Integer.t, Atom.t | String.t, Atom.t | String.t) :: String.t

Converts cents and returns a properly formatted string for the given currency.

Examples

iex> OpenExchangeRates.convert_cents_and_format(1234567, :EUR, :CAD)
{:ok, "C$18,026.07"}

iex> OpenExchangeRates.convert_cents_and_format(1234567, :EUR, :USD)
{:ok, "$13,687"}

iex> OpenExchangeRates.convert_cents_and_format(1234567, :USD, :EUR)
{:ok, "€11.135,79"}

iex> OpenExchangeRates.convert_cents_and_format(1234567, :EUR, :NOK)
{:ok, "116.495,78NOK"}
convert_cents_and_format!(value, from, to)

Specs

convert_cents_and_format!(Integer.t, Atom.t | String.t, Atom.t | String.t) :: String.t

Bang version of convert_cents_and_format/3 Will either return the result or raise when there was an error

#example

iex> OpenExchangeRates.convert_cents_and_format!(1234567, :EUR, :USD)
"$13,687"