Packages

A complete, production-grade Elixir client for the Mercury Banking API (accounts, transactions, recipients, invoices, payments, treasury, webhooks, and more), with typed errors, retry with backoff, and lazy auto-paginating streams.

Current section

Files

Jump to
mercury_client lib mercury categories.ex
Raw

lib/mercury/categories.ex

defmodule Mercury.Category do
@moduledoc "An expense category."
@type t :: %__MODULE__{
id: String.t(),
name: String.t(),
visible_for_reimbursements: boolean(),
visible_for_card_spend: boolean(),
visible_for_other: boolean()
}
defstruct [:id, :name, :visible_for_reimbursements, :visible_for_card_spend, :visible_for_other]
@doc false
def from_json(map) when is_map(map) do
%__MODULE__{
id: map["id"],
name: map["name"],
visible_for_reimbursements: map["visibleForReimbursements"],
visible_for_card_spend: map["visibleForCardSpend"],
visible_for_other: map["visibleForOther"]
}
end
end
defmodule Mercury.Categories do
@moduledoc "The Mercury Categories API — expense categories."
alias Mercury.{Category, Client, HTTP, Page, Pagination, Support}
@doc "Creates a new expense category. `POST /categories`"
@spec create(client :: Client.t(), attrs :: map() | keyword()) ::
{:ok, Category.t()} | {:error, Exception.t()}
def create(%Client{} = client, attrs) do
with {:ok, body} <- HTTP.post(client, "categories", attrs) do
{:ok, Category.from_json(body)}
end
end
@spec create!(client :: Client.t(), attrs :: map() | keyword()) :: Category.t()
def create!(client, attrs), do: Support.bang!(create(client, attrs))
@doc "Returns one page of categories. `GET /categories`"
@spec list(client :: Client.t(), opts :: keyword()) ::
{:ok, Page.t(Category.t())} | {:error, Exception.t()}
def list(%Client{} = client, opts \\ []) do
with {:ok, body} <- HTTP.get(client, "categories", opts) do
{:ok, Page.from_json(body, "categories", &Category.from_json/1)}
end
end
@spec list!(client :: Client.t(), opts :: keyword()) :: Page.t(Category.t())
def list!(client, opts \\ []), do: Support.bang!(list(client, opts))
@doc "A lazy, auto-paginating stream of every category."
@spec stream(client :: Client.t(), opts :: keyword()) :: Enumerable.t()
def stream(%Client{} = client, opts \\ []) do
opts = Keyword.new(opts)
Pagination.stream(fn cursor -> list(client, Keyword.put(opts, :start_after, cursor)) end)
end
@doc "Renames a category. `PUT /categories/{id}`"
@spec update(client :: Client.t(), category_id :: String.t(), attrs :: map() | keyword()) ::
{:ok, Category.t()} | {:error, Exception.t()}
def update(%Client{} = client, category_id, attrs) do
with {:ok, body} <- HTTP.put(client, "categories/#{category_id}", attrs) do
{:ok, Category.from_json(body)}
end
end
@spec update!(client :: Client.t(), category_id :: String.t(), attrs :: map() | keyword()) ::
Category.t()
def update!(client, category_id, attrs), do: Support.bang!(update(client, category_id, attrs))
@doc "Deletes a category. `DELETE /categories/{id}`"
@spec delete(client :: Client.t(), category_id :: String.t()) :: :ok | {:error, Exception.t()}
def delete(%Client{} = client, category_id) do
case HTTP.delete(client, "categories/#{category_id}") do
{:ok, _body} -> :ok
{:error, _error} = error -> error
end
end
@spec delete!(client :: Client.t(), category_id :: String.t()) :: :ok
def delete!(client, category_id), do: Support.bang!(delete(client, category_id))
end