NAME

JSON::RPC::Simple::Dispatcher - Decodes JSON-RPC calls and dispatches them

DESCRIPTION

Instances of this class decodes JSON-RPC calls over HTTP and dispatches them to modules/objects registered for a given path and then encodes the result as in a JSON-RPC format.

INTERFACE

CLASS METHODS

new ( %opts )

Creates a new dispatcher instance. Can take the the following optional named arguments:

json

The encoder/decoder object to use. Defaults to JSON with utf8 on.

charset

The charset to send in the content-type when creating the response. Defaults to utf-8.

error_handler

A reference to a subroutine which is invoked when an error occurs. May optionally return an object which will be sent as the 'error' member of the result. When called it is passed the request object, the error code, error message, the call ID and target object if any.

CLASS VARIABLES

$HTTP_ERROR_CODE

This is the HTTP result code. It's reset to 500 (Internal Server Error) each time handle is called. You may change this in your error handling routine.

INSTANCE METHODS

json
json ( $json )

Gets/sets the json object to use for encoding/decoding

charset
charset ( $charset )

Gets/sets the charset to use when creating the HTTP::Response object.

error_handler ( \&handler )

Gets/sets the error handler to call when an error occurs.

dispatch_to ( \%targets )

Sets the dispatch table. The dispatch-table is a path to instance mapping where the key is a path and the value the instance of class for which to call the method on. For example

$o->dispatch_to({
  "/API" => "MyApp::API",
  "/Other/API" => MyApp::OtherAPI->new(),
});
handle ( $path, $request )

This method decodes the $request which should be a HTTP::Request look-a-like object and finds the appropriate target in the dispatch table for $path.

The $request object MUST provide the following methods:

method

The HTTP method of the request such as GET, POST, HEAD, PUT in captial letters.

content_type

The Content-Type header from the request.

content_length

The Content-Length header from the request.

content

The content of the request as we only handle POST.

The content is stripped from any unicode BOM before being passed to the JSON decoder.