Packages
mercury_client
1.0.0
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
Current section
Files
lib/mercury/case_converter.ex
defmodule Mercury.CaseConverter do
@moduledoc """
Converts the (snake_case, idiomatic-Elixir) keys of maps and keyword lists
you pass to `create`/`update` functions into the camelCase keys the
Mercury API expects, recursively.
Values are left untouched — enum-like values (e.g. payment methods, wire
types) should be given as atoms or strings that already match Mercury's
wire format exactly, e.g. `:domesticWire`, `"businessChecking"`.
"""
@doc "Recursively camelizes the keys of a map, keyword list, or list of either."
@spec camelize_keys(value :: term()) :: term()
def camelize_keys(value) when is_map(value) and not is_struct(value) do
Map.new(value, fn {k, v} -> {camelize(k), camelize_keys(v)} end)
end
def camelize_keys(value) when is_struct(value), do: value
def camelize_keys(value) when is_list(value) do
if Keyword.keyword?(value) do
Map.new(value, fn {k, v} -> {camelize(k), camelize_keys(v)} end)
else
Enum.map(value, &camelize_keys/1)
end
end
def camelize_keys(value), do: value
defp camelize(key) when is_atom(key), do: key |> Atom.to_string() |> camelize()
defp camelize(key) when is_binary(key) do
case String.split(key, "_") do
[single] ->
single
[first | rest] ->
Enum.join([first | Enum.map(rest, &String.capitalize/1)])
end
end
end