Dispatcher/App.pm

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