Ewebmachine.Builder.Handlers (ewebmachine v2.3.0) 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

allow_missing_post(do_block)

View Source (macro)

see Ewebmachine.Handlers.allow_missing_post/2

Link to this macro

allowed_methods(do_block)

View Source (macro)

see Ewebmachine.Handlers.allowed_methods/2

Link to this macro

base_uri(do_block)

View Source (macro)

see Ewebmachine.Handlers.base_uri/2

Link to this macro

charsets_provided(do_block)

View Source (macro)

see Ewebmachine.Handlers.charsets_provided/2

Link to this macro

content_types_accepted(do_block)

View Source (macro)

see Ewebmachine.Handlers.content_types_accepted/2

Link to this macro

content_types_provided(do_block)

View Source (macro)

see Ewebmachine.Handlers.content_types_provided/2

Link to this macro

create_path(do_block)

View Source (macro)

see Ewebmachine.Handlers.create_path/2

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

delete_completed(do_block)

View Source (macro)

see Ewebmachine.Handlers.delete_completed/2

Link to this macro

delete_resource(do_block)

View Source (macro)

see Ewebmachine.Handlers.delete_resource/2

Link to this macro

encodings_provided(do_block)

View Source (macro)

see Ewebmachine.Handlers.encodings_provided/2

Link to this macro

expires(do_block)

View Source (macro)

see Ewebmachine.Handlers.expires/2

Link to this macro

finish_request(do_block)

View Source (macro)

see Ewebmachine.Handlers.finish_request/2

Link to this macro

forbidden(do_block)

View Source (macro)

see Ewebmachine.Handlers.forbidden/2

Link to this macro

generate_etag(do_block)

View Source (macro)

see Ewebmachine.Handlers.generate_etag/2

Link to this macro

is_authorized(do_block)

View Source (macro)

see Ewebmachine.Handlers.is_authorized/2

Link to this macro

is_conflict(do_block)

View Source (macro)

see Ewebmachine.Handlers.is_conflict/2

Link to this macro

known_content_type(do_block)

View Source (macro)

see Ewebmachine.Handlers.known_content_type/2

Link to this macro

known_methods(do_block)

View Source (macro)

see Ewebmachine.Handlers.known_methods/2

Link to this macro

last_modified(do_block)

View Source (macro)

see Ewebmachine.Handlers.last_modified/2

Link to this macro

malformed_request(do_block)

View Source (macro)

see Ewebmachine.Handlers.malformed_request/2

Link to this macro

moved_permanently(do_block)

View Source (macro)

see Ewebmachine.Handlers.moved_permanently/2

Link to this macro

moved_temporarily(do_block)

View Source (macro)

see Ewebmachine.Handlers.moved_temporarily/2

Link to this macro

multiple_choices(do_block)

View Source (macro)

see Ewebmachine.Handlers.multiple_choices/2

Link to this macro

options(do_block)

View Source (macro)

see Ewebmachine.Handlers.options/2

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

ping(do_block)

View Source (macro)

see Ewebmachine.Handlers.ping/2

Link to this macro

post_is_create(do_block)

View Source (macro)

see Ewebmachine.Handlers.post_is_create/2

Link to this macro

previously_existed(do_block)

View Source (macro)

see Ewebmachine.Handlers.previously_existed/2

Link to this macro

process_post(do_block)

View Source (macro)

see Ewebmachine.Handlers.process_post/2

Link to this macro

resource_exists(do_block)

View Source (macro)

see Ewebmachine.Handlers.resource_exists/2

Link to this macro

service_available(do_block)

View Source (macro)

see Ewebmachine.Handlers.service_available/2

Link to this macro

uri_too_long(do_block)

View Source (macro)

see Ewebmachine.Handlers.uri_too_long/2

Link to this macro

valid_content_headers(do_block)

View Source (macro)

see Ewebmachine.Handlers.valid_content_headers/2

Link to this macro

valid_entity_length(do_block)

View Source (macro)

see Ewebmachine.Handlers.valid_entity_length/2

Link to this macro

variances(do_block)

View Source (macro)

see Ewebmachine.Handlers.variances/2