Elixir foundationSort Order

Elixir: Custom Plug Part 3 - [010]

We're going to wrap up our plug in this episode. We need to return the connection inside the call function so it can be used inside our pipeline.

Storing the Result in the Conn

To store the result inside the connection we need to use the assign function

assign(conn, :type, :headless_chrome)

We can now finish up our plug.

Finishing up the Plug

We will now correctly return the connection in our call function. We will then need to move some of the code into a private function called bot_or_user

def call(conn, _opts) do  with %{"user-agent" => ua} <- Enum.into(conn.req_headers, %{}),       true <- ua =~ @headless_chrome do    assign(conn, :type, :headless_chrome)  else    _ -> bot_or_user(conn)  endend
defp bot_or_user(conn) do  if Browser.bot?(conn),     do: assign(conn, :type, :bot),     else: assign(conn, :type, :user)end

This should be it in terms of the code for the agent_plug.ex

Watch the video to see how we retrieve the value later on in the pipeline!