Packages
pigeon
1.6.0
2.0.1
2.0.0
2.0.0-rc.3
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.0
1.3.2
1.3.1
1.3.0
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.1.0-rc.1
1.1.0-rc.0
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.8.0
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.0
0.2.0
0.1.0
iOS (APNS), Android (FCM), and Amazon Android (ADM) push notifications for Elixir.
Current section
Files
Jump to
Current section
Files
lib/pigeon/adm/config.ex
defmodule Pigeon.ADM.Config do
@moduledoc """
Validates configuration settings that initialize ADM.Worker instances.
"""
defstruct name: nil, client_id: nil, client_secret: nil
@type t :: %__MODULE__{
client_id: String.t() | nil,
client_secret: String.t() | nil,
name: atom | nil
}
def default_name, do: :adm_default
@doc ~S"""
Returns a new `ADM.Config` with given `opts`.
## Examples
iex> Pigeon.ADM.Config.new(
...> name: :test,
...> client_id: "amzn.client.id",
...> client_secret: "1234secret"
...> )
%Pigeon.ADM.Config{name: :test, client_id: "amzn.client.id",
client_secret: "1234secret"}
"""
@spec new(Keyword.t() | atom) :: t
def new(opts) when is_list(opts) do
%__MODULE__{
name: opts[:name],
client_id: opts[:client_id],
client_secret: opts[:client_secret]
}
end
def new(name) when is_atom(name) do
Application.get_env(:pigeon, :adm)[name]
|> Enum.to_list()
|> Keyword.put(:name, name)
|> new()
end
@doc ~S"""
Returns whether a given config has valid credentials.
## Examples
iex> :adm_default |> new() |> valid?()
true
"""
def valid?(config) do
valid_item?(config.client_id) and valid_item?(config.client_secret)
end
defp valid_item?(item), do: is_binary(item) and String.length(item) > 0
@spec validate!(any) :: :ok | no_return
def validate!(config) do
if valid?(config) do
:ok
else
raise Pigeon.ConfigError,
reason: "attempted to start without valid client id and secret",
config: redact(config)
end
end
defp redact(config) do
[:client_id, :client_secret]
|> Enum.reduce(config, fn k, acc ->
case Map.get(acc, k) do
nil -> acc
_ -> Map.put(acc, k, "[FILTERED]")
end
end)
end
end