Packages

This library fetches historic EUR rates from the European Central Bank (ECB). It's heavily inspired by the Elixir OpenExchangeRates library.

Current section

Files

Jump to
ecb_rates lib ecb_rates.ex
Raw

lib/ecb_rates.ex

defmodule ECBRates do
@moduledoc """
This is the entrypoint for fetching EUR rates from ECB.
"""
@doc """
Fetches the rates for a given date (as Date struct or as a string in ISO8601 format).
"""
def fetch(%Date{} = date), do: date |> Date.to_iso8601() |> fetch()
def fetch(date) when is_binary(date) do
value = ECBRates.Cache.get()
case Map.get(value, date) do
nil ->
{:error, "No rate for #{date}"}
data ->
{:ok, data}
end
end
@doc """
Fetches the rates for a given date (as Date struct or as a string in ISO8601 format)
and currency (using three letter symbol).
"""
def fetch(date, currency) do
case fetch(date) do
{:error, reason} ->
{:error, reason}
{:ok, data} ->
case Map.get(data, currency) do
nil ->
{:error, "No rate for #{currency} on #{date}"}
rate ->
{:ok, rate}
end
end
end
end