View Source UAInspector.Plug (UAInspector Plug v0.3.0)
UAInspector Plug
usage
Usage
After ensuring :ua_inspector
is configured you need to add the plug:
defmodule MyRouter do
use Plug.Router
# ...
plug UAInspector.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 user-agent
header:
defmodule MyRouter do
get "/" do
case UAInspector.Plug.get_result(conn) do
nil -> send_resp(conn, 500, "No lookup done")
%{user_agent: ""} -> send_resp(conn, 404, "Empty or missing user agent")
%{device: :unknown} -> send_resp(conn, 404, "Unknown device type")
%{device: %{type: type}} -> send_resp(conn, 200, "Device type: " <> type)
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 UAInspector.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 | UAInspector.Result.t()
Returns the lookup result from the connection.
Callback implementation for Plug.init/1
.