Grapple v1.2.4 Grapple
This is the main module for Grapple. It defines the functions and macros that are available for adding, removing, and viewing topics and hooks, and broadcasting hooks.
Summary
Functions
Adds a new topic. Topic must be an atom. Returns a Grapple.Server.Topic
struct which has a name
and a sup
(Supervisor pid)
Sends HTTP requests for all hooks subscribed to the given topic
Like broadcast/1, but will send all hooks for a topic with the given body
instead of what was originally defined as the body
on the Hook
Clears all topics
Returns a list of all hooks subscribed to a topic
Returns a list of responses for each hook on the given topic
Lists all topics
Removes a subscribed hook given a topic and the hook pid
Removes a topic
Called when an application is started
Starts polling for a hook if the hook already has a specified interval (milliseconds)
Starts polling for a hook with the given interval (milliseconds)
Stops a hook from polling
Subscribes a hook to a topic. The first argument must be an atom
representing an existing topic and the second must be a valid Hook
struct
Macros
Allows modules to use
Grapple.Hook in them
Allows users to define hookable functions that automatically publish to subscribers whenever they are invoked
Functions
Adds a new topic. Topic must be an atom. Returns a Grapple.Server.Topic
struct which has a name
and a sup
(Supervisor pid).
iex> {:ok, topic = %Grapple.Server.Topic{}} = Grapple.add_topic(:pokemon)
iex> topic.name
:pokemon
Sends HTTP requests for all hooks subscribed to the given topic.
Returns :ok
.
iex> {:ok, _pokemon} = Grapple.add_topic(:pokemon)
iex> {:ok, _pid} = Grapple.subscribe(:pokemon, %Grapple.Hook{url: "my-api"})
iex> Grapple.broadcast(:pokemon)
:ok
Like broadcast/1, but will send all hooks for a topic with the given body
instead of what was originally defined as the body
on the Hook
.
Returns a list of all hooks subscribed to a topic.
iex> {:ok, _pokemon} = Grapple.add_topic(:pokemon)
iex> {:ok, _pid} = Grapple.subscribe(:pokemon, %Grapple.Hook{url: "my-api"})
iex> [{_pid, hook}] = Grapple.get_hooks(:pokemon)
iex> hook
%Grapple.Hook{body: %{}, headers: [], life: nil, method: "GET", owner: nil,
query: %{}, ref: nil, url: "my-api"}
Returns a list of responses for each hook on the given topic.
iex> {:ok, _pokemon} = Grapple.add_topic(:pokemon)
iex> {:ok, _pid} = Grapple.subscribe(:pokemon, %Grapple.Hook{url: "my-api"})
iex> [{_pid, responses}] = Grapple.get_responses(:pokemon)
iex> responses
[]
Lists all topics.
iex> {:ok, pokemon} = Grapple.add_topic(:pokemon)
iex> {:ok, gyms} = Grapple.add_topic(:gyms)
iex> [gyms, pokemon] == Grapple.get_topics
true
Called when an application is started.
This function is called when an application is started using
Application.start/2
(and functions on top of that, such as
Application.ensure_started/2
). This function should start the top-level
process of the application (which should be the top supervisor of the
application’s supervision tree if the application follows the OTP design
principles around supervision).
start_type
defines how the application is started:
:normal
- used if the startup is a normal startup or if the application is distributed and is started on the current node because of a failover from another node and the application specification key:start_phases
is:undefined
.{:takeover, node}
- used if the application is distributed and is started on the current node because of a failover on the nodenode
.{:failover, node}
- used if the application is distributed and is started on the current node because of a failover on nodenode
, and the application specification key:start_phases
is not:undefined
.
start_args
are the arguments passed to the application in the :mod
specification key (e.g., mod: {MyApp, [:my_args]}
).
This function should either return {:ok, pid}
or {:ok, pid, state}
if
startup is successful. pid
should be the PID of the top supervisor. state
can be an arbitrary term, and if omitted will default to []
; if the
application is later stopped, state
is passed to the stop/1
callback (see
the documentation for the c:stop/1
callback for more information).
use Application
provides no default implementation for the start/2
callback.
Callback implementation for Application.start/2
.
Starts polling for a hook if the hook already has a specified interval (milliseconds).
Use this function if the specified hook already has an interval, but polling is not currently running.
Returns :ok
if successful or {:error, msg}
otherwise.
iex> {:ok, _pokemon} = Grapple.add_topic(:pokemon)
iex> {:ok, pid} = Grapple.subscribe(:pokemon, %Grapple.Hook{url: "my-api"})
iex> Grapple.start_polling(pid)
{:error, "No interval specified, use [`start_polling/2`](#start_polling/2)."}
Starts polling for a hook with the given interval (milliseconds).
Returns :ok
if successful.
iex> {:ok, _pokemon} = Grapple.add_topic(:pokemon)
iex> {:ok, pid} = Grapple.subscribe(:pokemon, %Grapple.Hook{url: "my-api"})
iex> Grapple.start_polling(pid, 3000)
:ok
Stops a hook from polling.
Returns :ok
.
iex> {:ok, _pokemon} = Grapple.add_topic(:pokemon)
iex> {:ok, pid} = Grapple.subscribe(:pokemon, %Grapple.Hook{url: "my-api", interval: 3000})
iex> Grapple.stop_polling(pid)
:ok
Subscribes a hook to a topic. The first argument must be an atom
representing an existing topic and the second must be a valid Hook
struct.
Returns the pid
of the Hook process that was created.
iex> {:ok, _pokemon} = Grapple.add_topic(:pokemon)
iex> {:ok, pid} = Grapple.subscribe(:pokemon, %Grapple.Hook{url: "my-api"})
iex> is_pid(pid)
true