View Source RefInspector.Plug (RefInspector Plug v0.2.0)

RefInspector Plug

usage

Usage

After ensuring :ref_inspector is configured you need to add the plug:

defmodule MyRouter do
  use Plug.Router

  # ...
  plug RefInspector.Plug
  # ...

  plug :match
  plug :dispatch
end

Depending on how you are using plugs the actual location may vary. Please consult your frameworks documentation to find the proper place.

Once set up the connection will be automatically enriched with the results of a lookup based on the connections referer header:

defmodule MyRouter do
  get "/" do
    case RefInspector.Plug.get_result(conn) do
      nil -> send_resp(conn, 500, "No lookup done")
      %{referer: nil} -> send_resp(conn, 404, "Missing referer")
      %{referer: ""} -> send_resp(conn, 404, "Empty referer")
      %{source: :unknown} -> send_resp(conn, 200, "Unknown referer")
      %{source: source} -> send_resp(conn, 200, "Client source: " <> source)
    end
  end
end

automatic-session-population

Automatic Session Population

You can configure the plug to use Plug.Session in order to avoid parsing more than once for the lifetime of the session:

plug RefInspector.Plug, [
  session_key: "session_key_to_store_the_result_with",
  use_session: true
]

Be sure to call Plug.Conn.fetch_session/2 earlier in your pipeline.

Link to this section Summary

Functions

Callback implementation for Plug.call/2.

Returns the lookup result from the connection.

Callback implementation for Plug.init/1.

Link to this section Functions

Callback implementation for Plug.call/2.

@spec get_result(Plug.Conn.t()) :: nil | RefInspector.Result.t()

Returns the lookup result from the connection.

Callback implementation for Plug.init/1.