Dispatcher/App.pm

filter: Statementstmt 52.0% 13 / 25 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 100% 2 / 2 totaltotal 52.5% 21 / 40 SCAR 19.0
CC
4
Cov
45%
to_app
20.0
__ANON__
2.0
__ANON__
2.0
CRAP 6.7
« Dispatcher/Procedure.pm ↑ Summary
1 package JSON::RPC::Dispatcher::App;
2 BEGIN {
3 1 ▶ $JSON::RPC::Dispatcher::App::VERSION = '0.0506';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::RPC::Dispatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 JSON::RPC::Dispatcher::App - A base class for creating object oriented apps with JRD.
12  
13 =head1 VERSION
14  
15 version 0.0506
16  
17 =head1 SYNOPSIS
18  
19 Create your module:
20  
21 package MyApp;
22  
23 use Moose;
24 extends 'JSON::RPC::Dispatcher::App';
25  
26 sub sum {
27 my ($self, @params) = @_;
28 my $sum = 0;
29 $sum += $_ for @params;
30 return $sum;
31 }
32  
33 sub guess {
34 my ($self, $guess) = @_;
35 if ($guess == 10) {
36 return 'Correct!';
37 }
38 elsif ($guess > 10) {
39 confess [986, 'Too high.', $guess];
40 }
41 else {
42 confess [987, 'Too low.', $guess];
43 }
44 }
45  
46 __PACKAGE__->register_rpc_method_names( qw( sum guess ) );
47  
48 1;
49  
50 Then your plack F<app.psgi>:
51  
52 MyApp->new->to_app;
53  
54 =head1 DESCRIPTION
55  
56 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 L<Plack::App::URLMap> to mount each module on a different URL, you can make a pretty powerful application server in very little time.
57  
58 =head1 METHODS
59  
60 The following methods are available from this class.
61  
62 =head2 new ( )
63  
64 A L<Moose> generated constructor.
65  
66 When you subclass you can easily add your own attributes using L<Moose>'s C<has> function, and they will be accessible to your RPCs like this:
67  
68 package MyApp;
69  
70 use Moose;
71 extends 'JSON::RPC::Dispatcher::App';
72  
73 has db => (
74 is => 'ro',
75 required => 1,
76 );
77  
78 sub make_it_go {
79 my ($self, @params) = @_;
80 my $sth = $self->db->prepare("select * from foo");
81 ...
82 }
83  
84 __PACKAGE__->register_rpc_method_names( qw(make_it_go) );
85  
86 1;
87  
88 In F<app.psgi>:
89  
90 my $db = DBI->connect(...);
91 MyApp->new(db=>$db)->to_app;
92  
93 =cut
94  
95 #--------------------------------------------------------
96  
97 =head2 register_rpc_method_names ( names )
98  
99 Class method. Registers a list of method names using L<JSON::RPC::Dispatcher>'s C<register> method.
100  
101 __PACKAGE__->register_rpc_method_names( qw( add subtract multiply divide ));
102  
103 =head3 names
104  
105 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:
106  
107 __PACKAGE__->register_rpc_method_names(
108 'add',
109 { name => 'ip_address', options => { with_plack_request => 1 } },
110 'concat',
111 );
112  
113 =cut
114  
115 sub _rpc_method_names {
116 1 ▶ return ();
Subroutine _rpc_method_names: called CC=1 SCAR=0.0
117 }
118  
119 sub register_rpc_method_names {
120 1 ▶ my ($class, @methods) = @_;
Subroutine register_rpc_method_names: called CC=1 SCAR=0.0
Pod documented
121 $class->meta->add_around_method_modifier('_rpc_method_names', sub {
122 1 ▶ my ($orig, $self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1 return ($orig->(), @methods);
124 1 });
125 }
126  
127 #--------------------------------------------------------
128  
129 =head2 to_app ( )
130  
131 Generates a PSGI/L<Plack> compatible app.
132  
133 =cut
134  
135 sub to_app {
136 0 ▶ my $self = shift;
Subroutine to_app: not called CC=4 SCAR=30.0
Pod documented
137 0 my $rpc = JSON::RPC::Dispatcher->new;
138 0 my $ref;
139 0 ▶ if ($ref = $self->can('_rpc_method_names')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = $self->can("_rpc_method_names")) 0 0 0
140 0 foreach my $method ($ref->()) {
141 0 ▶ if (ref $method eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method eq 'HASH') { } 0 0 0
142 0 my $name = $method->{name};
143 0 ▶ $rpc->register($name, sub { $self->$name(@_) }, $method->{options});
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 }
145 else {
146 0 ▶ $rpc->register($method, sub { $self->$method(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 }
148 }
149 }
150 0 $rpc->to_app;
151 }
152  
153  
154 =head1 LEGAL
155  
156 JSON::RPC::Dispatcher is Copyright 2009-2010 Plain Black Corporation (L<http://www.plainblack.com/>) and is licensed under the same terms as Perl itself.
157  
158 =cut
159  
160 1 1;
« Dispatcher/Procedure.pm ↑ Summary