ewebmachine v2.2.0 Ewebmachine.Builder.Handlers View Source

use this module will use Plug.Builder (so a plug pipeline described with the plug module_or_function_plug macro), but gives you an :add_handler local function plug which adds to the conn the locally defined ewebmachine handlers (see Ewebmachine.Handlers).

So :

  • Construct your automate decision handler through multiple :add_handler plugs
  • Pipe the plug Ewebmachine.Plug.Run to run the HTTP automate which will call these handlers to take decisions.
  • Pipe the plug Ewebmachine.Plug.Send to send and halt any conn previsously passed through an automate run.

To define handlers, use the following helpers :

  • the handler specific macros (like Ewebmachine.Builder.Handlers.resource_exists/1)
  • the macro defh/2 to define any helpers, usefull for body producing handlers or to have multiple function clauses
  • in handler implementation conn and state binding are available
  • the response of the handler implementation is wrapped, so that returning :my_response is the same as returning {:my_response,conn,state}

Below a full example :

defmodule MyJSONApi do 
  use Ewebmachine.Builder.Handlers
  plug :cors
  plug :add_handlers, init: %{}

  content_types_provided do: ["application/json": :to_json]
  defh to_json, do: Poison.encode!(state[:json_obj])

  defp cors(conn,_), do: 
    put_resp_header(conn,"Access-Control-Allow-Origin","*")
end

defmodule GetUser do 
  use Ewebmachine.Builder.Handlers
  plug MyJSONApi
  plug :add_handlers
  plug Ewebmachine.Plug.Run
  plug Ewebmachine.Plug.Send
  resource_exists do:
    pass( !is_nil(user=DB.User.get(conn.params["q"])), json_obj: user)
end
defmodule GetOrder do 
  use Ewebmachine.Builder.Handlers
  plug MyJSONApi
  plug :add_handlers
  plug Ewebmachine.Plug.Run
  plug Ewebmachine.Plug.Send
  resource_exists do:
    pass(!is_nil(order=DB.Order.get(conn.params["q"])), json_obj: order)
end

defmodule API do
  use Plug.Router
  plug :match 
  plug :dispatch

  get "/get/user", do: GetUser.call(conn,[])
  get "/get/order", do: GetOrder.call(conn,[])
  end

Link to this section Summary

Functions

define a resource handler function as described at Ewebmachine.Handlers

Shortcut macro for : {response,var!(conn),Enum.into(update_state,var!(state))}

Link to this section Functions

Link to this macro defh(signature, do_block) View Source (macro)

define a resource handler function as described at Ewebmachine.Handlers.

Since there is a specific macro in this module for each handler, this macro is useful :

defh to_html, do: "hello you"
defh from_json, do: pass(:ok, json: Poison.decode!(read_body conn))
defh resources_exists(conn,%{obj: obj}) when obj !== nil, do: true
defh resources_exists(conn,_), do: false
Link to this macro pass(response, update_state) View Source (macro)

Shortcut macro for : {response,var!(conn),Enum.into(update_state,var!(state))}

use it if your handler wants to add some value to a collectable state (a map for instance), but using default “conn” current binding.

for instance a resources_exists implementation “caching” the result in the state could be :

pass (user=DB.get(state.id)) != nil, current_user: user
# same as returning :
{true,conn,%{id: "arnaud", current_user: %User{id: "arnaud"}}}
Link to this macro post_is_create(do_block) View Source (macro)

see Ewebmachine.Handlers.post_is_create/2