NAME

JSON::RPC::Dispatcher::App - A base class for creating object oriented apps with JRD.

VERSION

version 0.0507

SYNOPSIS

Create your module:

 package MyApp;

 use Moose;
 extends 'JSON::RPC::Dispatcher::App';

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

 sub guess {
    my ($self, $guess) = @_;
    if ($guess == 10) {
	    return 'Correct!';
    }
    elsif ($guess > 10) {
        confess [986, 'Too high.', $guess];
    }
    else {
        confess [987, 'Too low.', $guess];   
    }
 }

 __PACKAGE__->register_rpc_method_names( qw( sum guess ) );

 1;

Then your plack app.psgi:

MyApp->new->to_app;

DESCRIPTION

This package gives you a base class to make it easy to create object-oriented JSON-RPC applications. This is a huge benefit when writing a larger app or suite of applications rather than just exposing a procedure or two. If you build out classes of methods using JSON::RPC::Dispatcher::App, and then use Plack::App::URLMap to mount each module on a different URL, you can make a pretty powerful application server in very little time.

METHODS

The following methods are available from this class.

new ( )

A Moose generated constructor.

When you subclass you can easily add your own attributes using Moose's has function, and they will be accessible to your RPCs like this:

package MyApp;

use Moose;
extends 'JSON::RPC::Dispatcher::App';

has db => (
   is          => 'ro',
   required    => 1,
);

sub make_it_go {
    my ($self, @params) = @_;
    my $sth = $self->db->prepare("select * from foo");
    ...
}

__PACKAGE__->register_rpc_method_names( qw(make_it_go) );

1;

In app.psgi:

my $db = DBI->connect(...);
MyApp->new(db=>$db)->to_app;

register_rpc_method_names ( names )

Class method. Registers a list of method names using JSON::RPC::Dispatcher's register method.

__PACKAGE__->register_rpc_method_names( qw( add subtract multiply divide ));

names

The list of method names to register. If you want to use any registration options with a particular method you can do that by passing the method in as a hash reference like so:

__PACKAGE__->register_rpc_method_names(
    'add',
    { name => 'ip_address', options => { with_plack_request => 1 } },
    'concat',
);

to_app ( )

Generates a PSGI/Plack compatible app.

LEGAL

JSON::RPC::Dispatcher is Copyright 2009-2010 Plain Black Corporation (http://www.plainblack.com/) and is licensed under the same terms as Perl itself.