View Source Tamnoon.MethodManager (Tamnoon v1.0.0-a.2)
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 thedefmethod/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
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
@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.