Ewebmachine.Builder.Handlers

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 :

To define handlers, use the following helpers :

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
Source

Summary

allow_missing_post(do_block)

see Ewebmachine.Handlers.allow_missing_post/2

allowed_methods(do_block)

see Ewebmachine.Handlers.allowed_methods/2

base_uri(do_block)

see Ewebmachine.Handlers.base_uri/2

charsets_provided(do_block)

see Ewebmachine.Handlers.charsets_provided/2

content_types_accepted(do_block)

see Ewebmachine.Handlers.content_types_accepted/2

content_types_provided(do_block)

see Ewebmachine.Handlers.content_types_provided/2

create_path(do_block)

see Ewebmachine.Handlers.create_path/2

defh(signature, do_block)

define a resource handler function as described at Ewebmachine.Handlers

delete_completed(do_block)

see Ewebmachine.Handlers.delete_completed/2

delete_resource(do_block)

see Ewebmachine.Handlers.delete_resource/2

encodings_provided(do_block)

see Ewebmachine.Handlers.encodings_provided/2

expires(do_block)

see Ewebmachine.Handlers.expires/2

finish_request(do_block)

see Ewebmachine.Handlers.finish_request/2

forbidden(do_block)

see Ewebmachine.Handlers.forbidden/2

generate_etag(do_block)

see Ewebmachine.Handlers.generate_etag/2

is_authorized(do_block)

see Ewebmachine.Handlers.is_authorized/2

is_conflict(do_block)

see Ewebmachine.Handlers.is_conflict/2

known_content_type(do_block)

see Ewebmachine.Handlers.known_content_type/2

known_methods(do_block)

see Ewebmachine.Handlers.known_methods/2

last_modified(do_block)

see Ewebmachine.Handlers.last_modified/2

malformed_request(do_block)

see Ewebmachine.Handlers.malformed_request/2

moved_permanently(do_block)

see Ewebmachine.Handlers.moved_permanently/2

moved_temporarily(do_block)

see Ewebmachine.Handlers.moved_temporarily/2

multiple_choices(do_block)

see Ewebmachine.Handlers.multiple_choices/2

options(do_block)

see Ewebmachine.Handlers.options/2

pass(response, update_state)

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

ping(do_block)

see Ewebmachine.Handlers.ping/2

post_is_create(do_block)

see Ewebmachine.Handlers.post_is_create/2

previously_existed(do_block)

see Ewebmachine.Handlers.previously_existed/2

process_post(do_block)

see Ewebmachine.Handlers.process_post/2

resource_exists(do_block)

see Ewebmachine.Handlers.resource_exists/2

service_available(do_block)

see Ewebmachine.Handlers.service_available/2

uri_too_long(do_block)

see Ewebmachine.Handlers.uri_too_long/2

valid_content_headers(do_block)

see Ewebmachine.Handlers.valid_content_headers/2

valid_entity_length(do_block)

see Ewebmachine.Handlers.valid_entity_length/2

variances(do_block)

see Ewebmachine.Handlers.variances/2

Macros

allow_missing_post(do_block)

see Ewebmachine.Handlers.allow_missing_post/2

Source
allowed_methods(do_block)

see Ewebmachine.Handlers.allowed_methods/2

Source
base_uri(do_block)

see Ewebmachine.Handlers.base_uri/2

Source
charsets_provided(do_block)

see Ewebmachine.Handlers.charsets_provided/2

Source
content_types_accepted(do_block)

see Ewebmachine.Handlers.content_types_accepted/2

Source
content_types_provided(do_block)

see Ewebmachine.Handlers.content_types_provided/2

Source
create_path(do_block)

see Ewebmachine.Handlers.create_path/2

Source
defh(signature, do_block)

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
Source
delete_completed(do_block)

see Ewebmachine.Handlers.delete_completed/2

Source
delete_resource(do_block)

see Ewebmachine.Handlers.delete_resource/2

Source
encodings_provided(do_block)

see Ewebmachine.Handlers.encodings_provided/2

Source
expires(do_block)

see Ewebmachine.Handlers.expires/2

Source
finish_request(do_block)

see Ewebmachine.Handlers.finish_request/2

Source
forbidden(do_block)

see Ewebmachine.Handlers.forbidden/2

Source
generate_etag(do_block)

see Ewebmachine.Handlers.generate_etag/2

Source
is_authorized(do_block)

see Ewebmachine.Handlers.is_authorized/2

Source
is_conflict(do_block)

see Ewebmachine.Handlers.is_conflict/2

Source
known_content_type(do_block)

see Ewebmachine.Handlers.known_content_type/2

Source
known_methods(do_block)

see Ewebmachine.Handlers.known_methods/2

Source
last_modified(do_block)

see Ewebmachine.Handlers.last_modified/2

Source
malformed_request(do_block)

see Ewebmachine.Handlers.malformed_request/2

Source
moved_permanently(do_block)

see Ewebmachine.Handlers.moved_permanently/2

Source
moved_temporarily(do_block)

see Ewebmachine.Handlers.moved_temporarily/2

Source
multiple_choices(do_block)

see Ewebmachine.Handlers.multiple_choices/2

Source
options(do_block)

see Ewebmachine.Handlers.options/2

Source
pass(response, update_state)

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"}}}
Source
ping(do_block)

see Ewebmachine.Handlers.ping/2

Source
post_is_create(do_block)

see Ewebmachine.Handlers.post_is_create/2

Source
previously_existed(do_block)

see Ewebmachine.Handlers.previously_existed/2

Source
process_post(do_block)

see Ewebmachine.Handlers.process_post/2

Source
resource_exists(do_block)

see Ewebmachine.Handlers.resource_exists/2

Source
service_available(do_block)

see Ewebmachine.Handlers.service_available/2

Source
uri_too_long(do_block)

see Ewebmachine.Handlers.uri_too_long/2

Source
valid_content_headers(do_block)

see Ewebmachine.Handlers.valid_content_headers/2

Source
valid_entity_length(do_block)

see Ewebmachine.Handlers.valid_entity_length/2

Source
variances(do_block)

see Ewebmachine.Handlers.variances/2

Source