Packages
stripity_stripe
2.0.0-alpha.5
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
alias Stripe.Convert
@doc """
Takes the module (e.g. `Stripe.Card`) and the response from Stripe and
returns a struct (e.g. `%Stripe.Card{}`) containing the Stripe response.
"""
@spec stripe_map_to_struct(module, %{String.t => any}) :: struct
def stripe_map_to_struct(module, stripe_response) do
response_mapping = module.response_mapping()
map = Enum.reduce response_mapping, %{}, fn ({key, format}, acc) ->
value = key |> to_string() |> convert_value(format, stripe_response)
Map.put(acc, key, value)
end
struct = struct(module, map)
end
defp convert_value(key, format, map) when is_map(format) do
stripe_map_to_struct(format.module, map)
end
defp convert_value(key, :metadata, map) do
map
|> Map.get(key)
end
defp convert_value(key, format, map) do
map
|> Map.get(key)
|> Convert.with_format(format)
end
def drop_nil_keys(map) do
Enum.reject(map, fn
{_, nil} -> true
_ -> false
end)
|> Enum.into(%{})
end
@spec get_date(map, atom | String.t) :: DateTime.t | nil
def get_date(m, k) do
case Map.get(m, k) do
nil -> nil
ts -> datetime_from_timestamp(ts)
end
end
defp datetime_from_timestamp(ts) when is_binary ts do
ts = case Integer.parse ts do
:error -> 0
{i, _r} -> i
end
datetime_from_timestamp ts
end
defp datetime_from_timestamp(ts) when is_number ts do
DateTime.from_unix!(ts)
end
@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.map(m, fn
{k, v} when is_binary(k) ->
a = String.to_existing_atom(k)
{a, v}
entry ->
entry
end)
|> Enum.into(%{})
end
def string_map_to_atoms([string_key_map]) do
string_map_to_atoms string_key_map
end
def string_map_to_atoms(string_key_map) do
for {key, val} <- string_key_map, into: %{}, do: {String.to_atom(key), val}
end
end