Skip to content

Commit

Permalink
refactor: rename :token_fetcher to :auth (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpopp authored Sep 10, 2024
1 parent eb296f3 commit fec0405
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ config :pigeon, PigeonTest.APNS.JWT,

config :pigeon, PigeonTest.FCM,
adapter: Pigeon.FCM,
project_id: System.get_env("FCM_PROJECT"),
token_fetcher: PigeonTest.Goth
auth: PigeonTest.Goth,
project_id: System.get_env("FCM_PROJECT")

config :pigeon, PigeonTest.Sandbox, adapter: Pigeon.Sandbox
8 changes: 4 additions & 4 deletions lib/pigeon/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ defmodule Pigeon.Dispatcher do
# FCM as an example, but use the relevant options for your push type.
opts = [
adapter: Pigeon.FCM,
project_id: "example-project-123",
token_fetcher: YourApp.Goth
auth: YourApp.Goth,
project_id: "example-project-123"
]
{:ok, pid} = Pigeon.Dispatcher.start_link(opts)
Expand Down Expand Up @@ -61,9 +61,9 @@ defmodule Pigeon.Dispatcher do
defp push_spec(%{type: "fcm"} = config) do
{Pigeon.Dispatcher, [
adapter: Pigeon.FCM,
auth: String.to_existing_atom(config.auth),
name: {:via, Registry, {Registry.YourApp, config.name}},
project_id: config.project_id,
token_fetcher: String.to_existing_atom(config.token_fetcher)
project_id: config.project_id
]}
end
end
Expand Down
13 changes: 7 additions & 6 deletions lib/pigeon/fcm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ defmodule Pigeon.FCM do
config :your_app, YourApp.FCM,
adapter: Pigeon.FCM,
project_id: "example-project-123",
token_fetcher: YourApp.Goth
auth: YourApp.Goth,
project_id: "example-project-123"
```
3. Start your dispatcher on application boot.
Expand Down Expand Up @@ -66,8 +66,8 @@ defmodule Pigeon.FCM do
defp fcm_opts do
[
adapter: Pigeon.FCM,
project_id: "example-project-123",
token_fetcher: YourApp.Goth
auth: YourApp.Goth,
project_id: "example-project-123"
]
end
end
Expand All @@ -90,8 +90,9 @@ defmodule Pigeon.FCM do
YourApp.FCM.push(n)
```
## Customizable Goth Token Fetcher
If you need a customizable `:token_fetcher` that handles fetching its own configuration, here's
## Customizing Goth
If you need a customizable `:auth` that handles fetching its own configuration, here's
an example you can use to get started.
For other `:source` configurations of `YourApp.Goth`, check out the `goth` documentation for [`Goth.start_link/1`](https://hexdocs.pm/goth/Goth.html#start_link/1)
Expand Down
20 changes: 10 additions & 10 deletions lib/pigeon/fcm/config.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Pigeon.FCM.Config do
@moduledoc false

defstruct token_fetcher: nil,
defstruct auth: nil,
project_id: nil,
uri: ~c"fcm.googleapis.com",
port: 443
Expand All @@ -11,10 +11,10 @@ defmodule Pigeon.FCM.Config do
This is passed directly to `Goth.fetch!/1`.
"""
@type token_fetcher :: module() | term()
@type auth :: module() | term()

@type t :: %__MODULE__{
token_fetcher: nil | token_fetcher(),
auth: nil | auth(),
project_id: nil | String.t(),
uri: String.t(),
port: pos_integer()
Expand All @@ -26,23 +26,23 @@ defmodule Pigeon.FCM.Config do
## Examples
iex> Pigeon.FCM.Config.new(
...> project_id: "example-project",
...> token_fetcher: YourApp.Goth
...> auth: YourApp.Goth,
...> project_id: "example-project"
...> )
%Pigeon.FCM.Config{
auth: YourApp.Goth,
port: 443,
project_id: "example-project",
token_fetcher: YourApp.Goth,
uri: ~c"fcm.googleapis.com"
}
"""
def new(opts) do
opts = Map.new(opts)

%__MODULE__{
auth: opts[:auth],
port: Map.get(opts, :port, 443),
project_id: opts[:project_id],
token_fetcher: opts[:token_fetcher],
uri: Map.get(opts, :uri, ~c"fcm.googleapis.com")
}
end
Expand Down Expand Up @@ -90,7 +90,7 @@ defimpl Pigeon.Configurable, for: Pigeon.FCM.Config do
_notification,
_opts
) do
token = Goth.fetch!(config.token_fetcher)
token = Goth.fetch!(config.auth)

[
{":method", "POST"},
Expand Down Expand Up @@ -134,10 +134,10 @@ defimpl Pigeon.Configurable, for: Pigeon.FCM.Config do
|> Enum.each(&do_validate!(&1, config))
end

defp do_validate!({:token_fetcher, mod}, config)
defp do_validate!({:auth, mod}, config)
when not is_atom(mod) or is_nil(mod) do
raise Pigeon.ConfigError,
reason: "attempted to start without valid :token_fetcher module",
reason: "attempted to start without valid :auth module",
config: redact(config)
end

Expand Down
10 changes: 5 additions & 5 deletions test/pigeon/fcm_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Pigeon.FCMTest do

@data %{"message" => "Test push"}
@invalid_project_msg ~r/^attempted to start without valid :project_id/
@invalid_fetcher_msg ~r/^attempted to start without valid :token_fetcher module/
@invalid_auth_msg ~r/^attempted to start without valid :auth module/

defp valid_fcm_reg_id do
Application.get_env(:pigeon, :test)[:valid_fcm_reg_id]
Expand All @@ -18,14 +18,14 @@ defmodule Pigeon.FCMTest do
describe "init/1" do
test "raises if configured with invalid project" do
assert_raise(Pigeon.ConfigError, @invalid_project_msg, fn ->
[project_id: nil, token_fetcher: PigeonTest.Goth]
[project_id: nil, auth: PigeonTest.Goth]
|> Pigeon.FCM.init()
end)
end

test "raises if configured with invalid token_fetcher module" do
assert_raise(Pigeon.ConfigError, @invalid_fetcher_msg, fn ->
[project_id: "example", token_fetcher: nil]
test "raises if configured with invalid auth module" do
assert_raise(Pigeon.ConfigError, @invalid_auth_msg, fn ->
[project_id: "example", auth: nil]
|> Pigeon.FCM.init()
end)
end
Expand Down

0 comments on commit fec0405

Please sign in to comment.