Skip to content

Commit 0999464

Browse files
author
Tomas Koutsky
authored
Merge pull request #12 from stepnivlk/0.2.2
Fix Api gun response handling & cleanup
2 parents 60d1fef + f15f9bd commit 0999464

File tree

5 files changed

+41
-35
lines changed

5 files changed

+41
-35
lines changed

lib/pushest.ex

+8-26
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,6 @@ defmodule Pushest do
9797

9898
defmacro __using__(opts) do
9999
quote bind_quoted: [opts: opts] do
100-
@typedoc ~S"""
101-
Options for Pushest to properly communicate with Pusher server.
102-
103-
- `:app_id` - Pusher Application ID.
104-
- `:key` - Pusher Application key.
105-
- `:secret` - Necessary to subscribe to private/presence channels and trigger events.
106-
- `:cluster` - Cluster where your Pusher app is configured.
107-
- `:encrypted` - When set to true communication with Pusher is fully encrypted.
108-
"""
109-
@type pusher_opts :: %{
110-
app_id: String.t(),
111-
secret: String.t(),
112-
key: String.t(),
113-
cluster: String.t(),
114-
encrypted: boolean
115-
}
116-
117-
@typedoc ~S"""
118-
Optional options for trigger function.
119-
120-
- `:force_api` - Always triggers via Pusher REST API endpoint when set to `true`
121-
"""
122-
@type trigger_opts :: [force_api: boolean]
123-
124100
@behaviour Pushest
125101

126102
@config Pushest.Supervisor.config(__MODULE__, opts)
@@ -179,8 +155,6 @@ defmodule Pushest do
179155
Same as trigger/3 but adds a possiblity to enforce triggering via API endpoint.
180156
For enforced API trigger provide `force_api: true` as an `opts`.
181157
E.g.: `Mod.trigger("channel", "event", %{message: "m"}, force_api: true)`
182-
183-
For trigger_opts values see `t:trigger_opts/0`.
184158
"""
185159
def trigger(channel, event, data, opts) do
186160
Router.cast({:trigger, channel, event, data}, opts)
@@ -215,6 +189,14 @@ defmodule Pushest do
215189
Router.cast({:unsubscribe, channel})
216190
end
217191

192+
def authenticate(_channel, _socket_id) do
193+
{:error, "TBD"}
194+
end
195+
196+
def authenticate(_channel, _socket_id, _user_data) do
197+
{:error, "TBD"}
198+
end
199+
218200
@doc ~S"""
219201
Function meant to be overwritten in user module, e.g.:
220202
```

lib/pushest/api.ex

+16-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ defmodule Pushest.Api do
1515
@client Pushest.Client.for_env()
1616
@version Mix.Project.config()[:version]
1717

18+
## ==========================================================================
19+
## Client
20+
## ==========================================================================
21+
1822
def start_link({pusher_opts, _callback_module, _init_channels}) do
1923
GenServer.start_link(
2024
__MODULE__,
@@ -23,6 +27,10 @@ defmodule Pushest.Api do
2327
)
2428
end
2529

30+
## ==========================================================================
31+
## Server
32+
## ==========================================================================
33+
2634
def init(state = %State{url: %Url{domain: domain, port: port}}) do
2735
{:ok, conn_pid} = @client.open(domain, port)
2836
Process.monitor(conn_pid)
@@ -78,11 +86,12 @@ defmodule Pushest.Api do
7886
end
7987

8088
def handle_info(
81-
{:gun_response, conn_pid, stream_ref, :fin, status, _headers},
89+
{:gun_response, conn_pid, stream_ref, :nofin, status, _headers},
8290
state = %State{conn_pid: conn_pid}
8391
) do
8492
case status do
85-
200 -> {:ok, _body} = :gun.await_body(conn_pid, stream_ref)
93+
200 ->
94+
{:ok, _body} = :gun.await_body(conn_pid, stream_ref)
8695
_ -> Logger.error("Api | Pusher response status #{inspect(status)}")
8796
end
8897

@@ -107,17 +116,18 @@ defmodule Pushest.Api do
107116
{:noreply, state}
108117
end
109118

119+
## ==========================================================================
120+
## Private
121+
## ==========================================================================
122+
110123
defp get_headers do
111124
[
112125
{"X-Pusher-Library", "Pushest #{@version}"}
113126
]
114127
end
115128

116129
defp post_headers do
117-
[
118-
{"content-type", "application/json"},
119-
{"X-Pusher-Library", "Pushest #{@version}"}
120-
]
130+
[{"content-type", "application/json"} | get_headers()]
121131
end
122132

123133
defp client_sync(conn_pid, stream_ref) do

lib/pushest/socket.ex

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ defmodule Pushest.Socket do
1414

1515
@client Pushest.Client.for_env()
1616

17+
## ==========================================================================
18+
## Client
19+
## ==========================================================================
20+
1721
def start_link(opts) do
1822
GenServer.start_link(
1923
__MODULE__,
@@ -22,6 +26,10 @@ defmodule Pushest.Socket do
2226
)
2327
end
2428

29+
## ==========================================================================
30+
## Server
31+
## ==========================================================================
32+
2533
def init(state = %State{url: %Url{domain: domain, path: path, port: port}}) do
2634
{:ok, conn_pid} = @client.open(domain, port)
2735
Process.monitor(conn_pid)
@@ -166,6 +174,10 @@ defmodule Pushest.Socket do
166174
{:noreply, state}
167175
end
168176

177+
## ==========================================================================
178+
## Private
179+
## ==========================================================================
180+
169181
@spec do_subscribe(String.t(), map, %State{}) :: term
170182
defp do_subscribe(channel, user_data, state = %State{conn_pid: conn_pid}) do
171183
auth = Utils.auth(state, channel, user_data)

mix.exs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
defmodule Pushest.MixProject do
2+
@moduledoc false
3+
24
use Mix.Project
35

46
def project do
57
[
68
app: :pushest,
7-
version: "0.2.1",
9+
version: "0.2.2",
810
elixir: "~> 1.6",
911
start_permanent: Mix.env() == :prod,
1012
deps: deps(),

mix.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
%{
22
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], []},
33
"cowlib": {:hex, :cowlib, "2.2.1", "1afa5b233cee8f642c153ec6f1dc48db0dd9a43e2114bc8b43e9d59636f6ae1f", [:rebar3], []},
4-
"credo": {:hex, :credo, "0.9.0", "5d1b494e4f2dc672b8318e027bd833dda69be71eaac6eedd994678be74ef7cb4", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, optional: false]}]},
4+
"credo": {:hex, :credo, "0.9.1", "f021affa11b32a94dc2e807a6472ce0914289c9132f99644a97fc84432b202a1", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, optional: false]}]},
55
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], []},
66
"earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], []},
77
"ex_doc": {:hex, :ex_doc, "0.18.3", "f4b0e4a2ec6f333dccf761838a4b253d75e11f714b85ae271c9ae361367897b7", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
88
"gun": {:hex, :gun, "1.0.0-pre.5", "69a11e259d5bf7fd9b1a74e1aa98e7979c2ae3ebbda27fe1b1c9cb4481f23ba3", [:rebar3], [{:cowlib, "~> 2.1", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.4", [hex: :ranch, optional: false]}]},
99
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
10-
"ranch": {:hex, :ranch, "1.4.0", "10272f95da79340fa7e8774ba7930b901713d272905d0012b06ca6d994f8826b", [:rebar3], []},
10+
"ranch": {:hex, :ranch, "1.5.0", "f04166f456790fee2ac1aa05a02745cc75783c2bfb26d39faf6aefc9a3d3a58a", [:rebar3], []},
1111
}

0 commit comments

Comments
 (0)