Packages
stripity_stripe
3.3.2
3.3.2
3.3.1
3.2.0
3.1.1
3.1.0
3.0.0
2.17.3
2.17.2
2.17.1
2.17.0
2.16.0
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.1
2.12.0
2.11.0
2.10.0
2.9.0
2.8.0
2.7.2
2.7.1
2.7.0
2.6.0
2.5.0
2.4.0
2.3.0
2.2.3
2.2.2
2.2.1
2.2.0
2.1.0
2.0.1
2.0.0
2.0.0-alpha.11
2.0.0-alpha.10
2.0.0-alpha.9
2.0.0-alpha.8
2.0.0-alpha.7
2.0.0-alpha.6
2.0.0-alpha.5
2.0.0-alpha.4
2.0.0-alpha.3
2.0.0-alpha.2
2.0.0-alpha.1
1.6.2
1.6.1
1.6.0
1.4.0
1.3.0
1.2.0
1.1.0
0.5.0
0.4.0
0.3.0
0.2.0
A Stripe client for Elixir.
Current section
Files
Jump to
Current section
Files
lib/stripe/util.ex
defmodule Stripe.Util do
@moduledoc false
@doc """
Performs a root-level conversion of map keys from strings to atoms.
This function performs the transformation safely using `String.to_existing_atom/1`, but this has a possibility to raise if
there is not a corresponding atom.
It is recommended that you pre-filter maps for known values before
calling this function.
## Examples
iex> map = %{
...> "a"=> %{
...> "b" => %{
...> "c" => 1
...> }
...> }
...> }
iex> Stripe.Util.map_keys_to_atoms(map)
%{
a: %{
"b" => %{
"c" => 1
}
}
}
"""
def map_keys_to_atoms(m) do
Enum.into(m, %{}, fn
{k, v} when is_binary(k) ->
a = String.to_existing_atom(k)
{a, v}
entry ->
entry
end)
end
def atomize_keys(%{} = map) do
Enum.into(map, %{}, fn {k, v} -> {atomize_key(k), atomize_keys(v)} end)
end
def atomize_keys([head | rest]), do: [atomize_keys(head) | atomize_keys(rest)]
# Default
def atomize_keys(not_a_map), do: not_a_map
def atomize_key(k) when is_binary(k), do: String.to_existing_atom(k)
def atomize_key(k), do: k
@spec object_name_to_module(String.t()) :: module
def object_name_to_module("test_helpers.test_clock"), do: Stripe.TestHelpers.TestClock
def object_name_to_module(object_name) do
module_parts =
object_name
|> String.split(".")
|> Enum.map(fn part ->
part
|> String.split("_")
|> Enum.map_join("", &String.capitalize/1)
end)
Module.safe_concat([Stripe | module_parts])
end
@spec module_to_string(module) :: String.t()
def module_to_string(module) do
module |> Atom.to_string() |> String.trim_leading("Elixir.")
end
def multipart_key(:file), do: :file
def multipart_key(key) when is_atom(key), do: Atom.to_string(key)
def multipart_key(key), do: key
def normalize_id(%{id: id}) when id !== nil, do: id
def normalize_id(id) when is_binary(id), do: id
defmacro log_deprecation(msg \\ "") do
# credo:disable-for-next-line Credo.Check.Warning.MixEnv
if Mix.env() in [:test, :dev] do
{fun, arity} = __CALLER__.function
mod = __CALLER__.module
quote bind_quoted: [mod: mod, fun: fun, arity: arity, msg: msg] do
require Logger
Logger.warning("[DEPRECATION] The function #{mod}.#{fun}/#{arity} is deprecated. #{msg}")
end
end
end
end