Skip to content

Commit

Permalink
Merge pull request #41 from camirmas/minor-fixes
Browse files Browse the repository at this point in the history
[1.2.4] Minor fixes
  • Loading branch information
camirmas committed Jun 6, 2018
2 parents 1ab6ba4 + 792955e commit 61fad89
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 134 deletions.
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ use Mix.Config
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
12 changes: 6 additions & 6 deletions lib/grapple.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Grapple do
use Application

def start(_type, _args) do
Grapple.Supervisor.start_link []
Grapple.Supervisor.start_link([])
end

# Topics
Expand Down Expand Up @@ -43,7 +43,7 @@ defmodule Grapple do
true
"""
def get_topics do
Grapple.Server.get_topics
Grapple.Server.get_topics()
end

@doc """
Expand All @@ -52,7 +52,7 @@ defmodule Grapple do
Returns `:ok`.
"""
def clear_topics do
Grapple.Server.clear_topics
Grapple.Server.clear_topics()
end

# Hooks
Expand Down Expand Up @@ -194,10 +194,10 @@ defmodule Grapple do
defmacro defhook({name, _, _} = func, do: block) do
quote do
def unquote(func) do
topic = unquote name
result = unquote block
topic = unquote(name)
result = unquote(block)

broadcast topic, result
broadcast(topic, result)

result
end
Expand Down
44 changes: 21 additions & 23 deletions lib/grapple/hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,45 @@ defmodule Grapple.Hook do
body: %{},
headers: [],
options: [],
query: %{},
query: %{}
]

# API

def start_link(hook) do
GenServer.start_link __MODULE__, hook
GenServer.start_link(__MODULE__, hook)
end

def get_info(pid) do
GenServer.call pid, :get_info
GenServer.call(pid, :get_info)
end

def get_responses(pid) do
GenServer.call pid, :get_responses
GenServer.call(pid, :get_responses)
end

def start_polling(pid) do
GenServer.call pid, :start_polling
GenServer.call(pid, :start_polling)
end

def start_polling(pid, interval) when is_integer(interval) do
GenServer.call pid, {:start_polling, interval}
GenServer.call(pid, {:start_polling, interval})
end

def broadcast(pid) do
GenServer.cast pid, :broadcast
GenServer.cast(pid, :broadcast)
end

def broadcast(pid, body) when is_nil(body) do
GenServer.cast pid, :broadcast
GenServer.cast(pid, :broadcast)
end

def broadcast(pid, body) do
GenServer.cast pid, {:broadcast, body}
GenServer.cast(pid, {:broadcast, body})
end

def stop_polling(pid) do
GenServer.cast pid, :stop_polling
GenServer.cast(pid, :stop_polling)
end

# Callbacks
Expand All @@ -80,6 +80,7 @@ defmodule Grapple.Hook do
case interval do
nil ->
{:reply, {:error, "No interval specified, use `start_polling/2`."}, state}

_ ->
{:ok, tref} = start_timer(interval)

Expand All @@ -95,19 +96,19 @@ defmodule Grapple.Hook do
end

def handle_cast(:broadcast, %{hook: hook, responses: responses} = state) do
response = notify(hook, hook.body)
new_state = %{state | responses: [response | responses]}
send_to_owner(hook, response)
response = notify(hook, hook.body)
new_state = %{state | responses: [response | responses]}
send_to_owner(hook, response)

{:noreply, new_state}
{:noreply, new_state}
end

def handle_cast({:broadcast, body}, %{hook: hook, responses: responses} = state) do
response = notify(hook, body)
new_state = %{state | responses: [response | responses]}
send_to_owner(hook, response)
response = notify(hook, body)
new_state = %{state | responses: [response | responses]}
send_to_owner(hook, response)

{:noreply, new_state}
{:noreply, new_state}
end

def handle_cast(:stop_polling, %{tref: tref} = state) do
Expand Down Expand Up @@ -136,18 +137,15 @@ defmodule Grapple.Hook do

defp send_to_owner(%{owner: owner}, response) when is_pid(owner) do
if Process.alive?(owner) do
send(owner, {:hook_response, self, response})
send(owner, {:hook_response, self(), response})
end
end

defp send_to_owner(_, _), do: nil

defp start_timer(interval) do
# Uses Erlang :timer to `broadcast` at the given interval
:timer.apply_interval(interval,
__MODULE__,
:broadcast,
[self])
:timer.apply_interval(interval, __MODULE__, :broadcast, [self()])
end

defp stop_timer(tref) when is_nil(tref) do
Expand Down
11 changes: 5 additions & 6 deletions lib/grapple/hook_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ defmodule Grapple.HookServer do
end

def handle_call({:subscribe, hook}, _from, %{hook_sup: hook_sup} = state) do
pid = add_hook(hook, hook_sup)
pid = add_hook(hook, hook_sup)

{:reply, {:ok, pid}, state}
{:reply, {:ok, pid}, state}
end

def handle_call(:get_hooks, _from, %{hook_sup: hook_sup} = state) do
Expand All @@ -65,11 +65,10 @@ defmodule Grapple.HookServer do
{:reply, responses, state}
end

def handle_cast({:remove_hook, hook_pid},
%{hook_sup: hook_sup} = state) do
HookSupervisor.terminate_child(hook_sup, hook_pid)
def handle_cast({:remove_hook, hook_pid}, %{hook_sup: hook_sup} = state) do
HookSupervisor.terminate_child(hook_sup, hook_pid)

{:noreply, state}
{:noreply, state}
end

def handle_cast(:broadcast, %{hook_sup: hook_sup} = state) do
Expand Down
45 changes: 23 additions & 22 deletions lib/grapple/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Grapple.Server do
use GenServer

defmodule Topic do
defstruct [:sup, :name,]
defstruct [:sup, :name]
end

alias Grapple.TopicsSupervisor
Expand Down Expand Up @@ -76,39 +76,40 @@ defmodule Grapple.Server do
end

def handle_call({:add_topic, topic_name}, _from, %{topics: topics} = state) do
topic = Enum.find(topics, fn topic -> topic.name == topic_name end)
topic = Enum.find(topics, fn topic -> topic.name == topic_name end)

case topic do
nil ->
{:ok, sup} = TopicsSupervisor.start_child(topic_name)
new_topic = %Topic{sup: sup, name: topic_name}
{:reply, {:ok, new_topic}, %{state | topics: [new_topic | topics]}}
topic ->
{:reply, {:ok, topic}, state}
end
case topic do
nil ->
{:ok, sup} = TopicsSupervisor.start_child(topic_name)
new_topic = %Topic{sup: sup, name: topic_name}
{:reply, {:ok, new_topic}, %{state | topics: [new_topic | topics]}}

topic ->
{:reply, {:ok, topic}, state}
end
end

def handle_call(:get_topics, _from, %{topics: topics} = state) do
{:reply, topics, state}
end

def handle_call(:clear_topics, _from, %{topics: topics} = state) do
Enum.each(topics, &(TopicsSupervisor.terminate_child(&1.sup)))
Enum.each(topics, &TopicsSupervisor.terminate_child(&1.sup))

{:reply, :ok, %{state | topics: []}}
{:reply, :ok, %{state | topics: []}}
end

def handle_cast({:remove_topic, topic_name}, %{topics: topics} = state) do
topic = Enum.find(topics, fn topic -> topic.name == topic_name end)
topic = Enum.find(topics, fn topic -> topic.name == topic_name end)

case topic do
nil ->
{:noreply, state}

case topic do
nil ->
{:noreply, state}
%Topic{} ->
TopicsSupervisor.terminate_child(topic.sup)
new_topics = List.delete(topics, topic)
{:noreply, %{state | topics: new_topics}}
end
%Topic{} ->
TopicsSupervisor.terminate_child(topic.sup)
new_topics = List.delete(topics, topic)
{:noreply, %{state | topics: new_topics}}
end
end
end

2 changes: 1 addition & 1 deletion lib/grapple/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ defmodule Grapple.Supervisor do
end

def init(_) do
supervise [], strategy: :one_for_all
supervise([], strategy: :one_for_all)
end
end
3 changes: 2 additions & 1 deletion lib/grapple/topic_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Grapple.TopicSupervisor do
supervisor(Grapple.HookSupervisor, [topic]),
worker(Grapple.HookServer, [topic])
]
supervise children, strategy: :one_for_all

supervise(children, strategy: :one_for_all)
end
end
40 changes: 20 additions & 20 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ defmodule Grapple.Mixfile do
use Mix.Project

def project do
[app: :grapple,
version: "1.2.3",
elixir: "~> 1.6.4",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
description: "Webhook magic in Elixir",
package: package,
deps: deps(),
elixirc_paths: elixirc_paths(Mix.env),
[
app: :grapple,
version: "1.2.3",
elixir: "~> 1.6.4",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
description: "Webhook magic in Elixir",
package: package(),
deps: deps(),
elixirc_paths: elixirc_paths(Mix.env()),

# Docs
name: "Grapple",
source_url: "https://github.com/camirmas/grapple",
docs: [# logo: "",
canonical: "https://hexdocs.com/grapple",
extras: ["README.md"]]]
# Docs
name: "Grapple",
source_url: "https://github.com/camirmas/grapple",
# logo: "",
docs: [canonical: "https://hexdocs.com/grapple", extras: ["README.md"]]
]
end

# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
defp elixirc_paths(_), do: ["lib"]

def package do
[
Expand All @@ -38,7 +39,7 @@ defmodule Grapple.Mixfile do
def application do
[
applications: [:httpoison, :logger, :gen_stage],
mod: {Grapple, []},
mod: {Grapple, []}
]
end

Expand All @@ -53,10 +54,9 @@ defmodule Grapple.Mixfile do
# Type "mix help deps" for more examples and options
defp deps do
[
{:graphql, "~> 0.3"},
{:httpoison, "~> 0.9.0"},
{:httpoison, "~> 1.0"},
{:gen_stage, "~> 0.4"},
{:ex_doc, "~> 0.13", only: :dev},
{:ex_doc, "~> 0.13", only: :dev}
]
end
end
14 changes: 9 additions & 5 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
%{"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []},
%{
"certifi": {:hex, :certifi, "2.3.1", "d0f424232390bf47d82da8478022301c561cf6445b5b5fb6a84d49a9e76d2639", [:rebar3], [{:parse_trans, "3.2.0", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"combine": {:hex, :combine, "0.9.3", "192e609b48b3f2210494e26f85db1712657be1a8f15795656710317ea43fc449", [:mix], []},
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.14.4", "a0a79a6896075814f4bc6802b74ccbed6549f47cc5ab34c71eaee2303170b8ef", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"gen_stage": {:hex, :gen_stage, "0.9.0", "9daf0b5ae224405e0456b7df5a282a9a958176cdb9b963811ca38140e7e1b3c4", [:mix], []},
"gettext": {:hex, :gettext, "0.12.1", "c0624f52763469ef7a3674919ae28b8286d88195b90fa1516180f31bbbd26d14", [:mix], []},
"graphql": {:hex, :graphql, "0.3.2", "0aa7a00948c456831f8fa1bb4ead3e4e15d145b8656902134d180d1369775dff", [:mix], []},
"hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [:mix, :rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"httpoison": {:hex, :httpoison, "0.9.2", "a211a8e87403a043c41218e64df250d321f236ac57f786c6a0ccf3e9e817c819", [:mix], [{:hackney, "~> 1.6.0", [hex: :hackney, optional: false]}]},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
"hackney": {:hex, :hackney, "1.12.1", "8bf2d0e11e722e533903fe126e14d6e7e94d9b7983ced595b75f532e04b7fdc7", [:rebar3], [{:certifi, "2.3.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "1.1.1", "96ed7ab79f78a31081bb523eefec205fd2900a02cda6dbc2300e7a1226219566", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "5.1.1", "cbc3b2fa1645113267cc59c760bafa64b2ea0334635ef06dbac8801e42f7279c", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [:rebar3], [], "hexpm"},
"plug": {:hex, :plug, "1.2.2", "cfbda521b54c92ab8ddffb173fbaabed8d8fc94bec07cd9bb58a84c1c501b0bd", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"timex": {:hex, :timex, "3.1.5", "413d6d8d6f0162a5d47080cb8ca520d790184ac43e097c95191c7563bf25b428", [:mix], [{:combine, "~> 0.7", [hex: :combine, optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, optional: false]}]},
"tzdata": {:hex, :tzdata, "0.5.9", "575be217b039057a47e133b72838cbe104fb5329b19906ea4e66857001c37edb", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, optional: false]}]},
"uuid": {:hex, :uuid, "1.1.5", "96cb36d86ee82f912efea4d50464a5df606bf3f1163d6bdbb302d98474969369", [:mix], []}}
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], [], "hexpm"},
"uuid": {:hex, :uuid, "1.1.5", "96cb36d86ee82f912efea4d50464a5df606bf3f1163d6bdbb302d98474969369", [:mix], []},
}
Loading

0 comments on commit 61fad89

Please sign in to comment.