Henry Tang > MojoX-JSON-RPC-0.05 > MojoX::JSON::RPC::Service

Download:
MojoX-JSON-RPC-0.05.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  4
Open  0
View/Report Bugs
Source   Latest Release: MojoX-JSON-RPC-0.06

NAME ^

MojoX::JSON::RPC::Service - JSON RPC Service registration

SYNOPSIS ^

    use MojoX::JSON::RPC::Service;

    my $svc  = MojoX::JSON::RPC::Service->new;

    $svc->register(
        'sum',
        sub {
            my @params = @_;
            my $sum    = 0;
            $sum += $_ for @params;
            return $sum;
        }
    );

    $svc->register(
        'remote_address',
        sub {
            my $tx = shift;
            return $tx->remote_address;
        },
        {
             with_mojo_tx => 1
        }
    );


    ## Then in Mojolicious application
    $self->plugin(
        'json_rpc_dispatcher',
        services => {
            '/jsonrpc'  => $svc,
        }
    );

This package can also be used as a base class to make it easy to create object-oriented JSON-RPC applications:

    package MyService;

    use Mojo::Base 'MojoX::JSON::RPC::Service';

    sub sum {
        my ($self, @params) = @_;
        my $sum    = 0;
        $sum += $_ for @params;
        return $sum;
    }

    __PACKAGE__->register_rpc_method_names( 'sum' );

    ## Then in Mojolicious application
    $self->plugin(
        'json_rpc_dispatcher',
        services => {
            '/jsonrpc'  => MyService->new,
        }
    );

DESCRIPTION ^

Register JSON-RPC service calls.

METHODS ^

MojoX::JSON::RPC::Service inherits all methods from Mojo::Base and implements the following new ones.

register

Register RPC methods.

    $svc->register(
        'sum',
        sub {
            my @params = @_;
            my $sum    = 0;
            $sum += $_ for @params;
            return $sum;
       }
    );

with_mojo_tx can be passed as options. In that case, Mojo::Transaction object will be pass as first argument of the subroutine.

    $svc->register(
        'remote_address',
        sub {
            my $tx = shift;
            return $tx->remote_address;
        },
        {
             with_mojo_tx => 1
        }
    );

register_rpc_method_names

Class method. Register a list of methods as JSON-RPC calls.

    __PACKAGE__->register_rpc_method_names( 'sum', 'multiply' );

SEE ALSO ^

MojoX::JSON::RPC::Dispatcher

syntax highlighting: