View Source Tamnoon.MethodManager (Tamnoon v0.1.1)

This module handles the management of different methods as you create them. Notably, it provides the defmethod/2 macro.

Importing the module

In order to create handlers for the methods you set up, you must import Tamnoon.MethodManager in your methods module. Then, you can use the defmethod/2 macro to implement handling of the methods.

Summary

Functions

Defines a function named tmnn_[name]. Functions with this prefix in your methods module will automatically be added to the possible methods when invoking route_request/3. Inside the created function, you can access the state of the client and the request object by the state and req variables respectively. Method handlers must return a tuple of {return_value, new_state}.

The function used by Tamnoon.SocketHandler.websocket_handle/2 to route the requests to the appropriate method handler.

Functions

Link to this macro

defmethod(name, list)

View Source (macro)

Defines a function named tmnn_[name]. Functions with this prefix in your methods module will automatically be added to the possible methods when invoking route_request/3. Inside the created function, you can access the state of the client and the request object by the state and req variables respectively. Method handlers must return a tuple of {return_value, new_state}.

Example

defmethod :get do
  key = get_key(req, state)
  if (key != nil) do
    {state[key], state}
  else
    {"Error: no matching key", state}
  end
end
Link to this function

route_request(methods_module, payload, state)

View Source
@spec route_request(module(), map(), map()) ::
  {:reply, {:text, return_value :: String.t()}, new_state :: map()}

The function used by Tamnoon.SocketHandler.websocket_handle/2 to route the requests to the appropriate method handler.