Dispatcher/Procedure.pm

filter: Statementstmt 34.8% 15 / 43 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 22.2% 2 / 9 Podpod 100% 6 / 6 totaltotal 34.3% 23 / 67 CC 4 SCAR 16.1
CC
4
Cov
30%
__ANON__
12.0
response
6.0
error
2.0
CRAP 5.0
« Dispatcher/App.pm ↑ Summary
1 package JSON::RPC::Dispatcher::Procedure;
2 2 $JSON::RPC::Dispatcher::Procedure::VERSION = '0.0507';
3 =head1 NAME
4  
5 JSON::RPC::Dispatcher::Procedure - The data holder between RPC requests and responses.
6  
7 =head1 VERSION
8  
9 version 0.0507
10  
11 =head1 SYNOPSIS
12  
13 use JSON::RPC::Dispatcher::Procedure;
14  
15 my $proc = JSON::RPC::Dispatcher::Procedure->new;
16  
17 $proc->error_code(300);
18  
19 my $method = $proc->method;
20  
21 =head1 DESCRIPTION
22  
23 Something needs to act as an intermediary to hold the data and state of requests coming in, RPC being called, and responses going out. THis module fits that bill.
24  
25 =cut
26  
27  
28 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #--------------------------------------------------------
31  
32 =head2 error_code ( [ code ] )
33  
34 Returns the current error code.
35  
36 =head3 code
37  
38 An integer. Sets an error code.
39  
40 =head2 has_error_code ( )
41  
42 Returns a boolean indicating whether an error code has been set.
43  
44 =cut
45  
46 2 has error_code => (
47 is => 'rw',
48 predicate => 'has_error_code',
49 );
50  
51 #--------------------------------------------------------
52  
53 =head2 error_message ( [ message ] )
54  
55 Returns the current error message.
56  
57 =head3 message
58  
59 A string. Sets an error message.
60  
61 =cut
62  
63 2 has error_message => (
64 is => 'rw',
65 default => undef,
66 );
67  
68 #--------------------------------------------------------
69  
70 =head2 error_data ( [ data ] )
71  
72 Returns the current error data. Error data is entirely defined by the application (e.g. detailed error information, nested errors etc.).
73  
74 =head3 data
75  
76 A scalar or reference. Sets an error data.
77  
78 =cut
79  
80 2 has error_data => (
81 is => 'rw',
82 default => undef,
83 );
84  
85 #--------------------------------------------------------
86  
87 =head2 error ( code, message, [ data ] )
88  
89 =cut
90  
91 sub error {
92 0 ▶ my ($self, $code, $message, $data) = @_;
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
93 0 $self->error_code($code);
94 0 $self->error_message($message);
95 0 $self->error_data($data);
96 }
97  
98 #--------------------------------------------------------
99  
100 =head2 invalid_request ( [ data ] )
101  
102 Sets an Invalid Request error as defined by the JSON-RPC 2.0 spec.
103  
104 =head3 data
105  
106 Optionally set some error data for the error.
107  
108 =cut
109  
110 sub invalid_request {
111 0 ▶ my ($self, $msg) = @_;
Subroutine invalid_request: not called CC=1 SCAR=6.9
Pod documented
112 0 $self->error_code(-32600);
113 0 $self->error_message('Invalid Request.');
114 0 $self->error_data($msg);
115 }
116  
117 #--------------------------------------------------------
118  
119 =head2 method_not_found ( [ data ] )
120  
121 Sets a Method Not Found error as defined by the JSON-RPC 2.0 spec.
122  
123 =head3 data
124  
125 Optionally set some error data for the error.
126  
127 =cut
128  
129 sub method_not_found {
130 0 ▶ my ($self, $msg) = @_;
Subroutine method_not_found: not called CC=1 SCAR=6.9
Pod documented
131 0 $self->error_code(-32601);
132 0 $self->error_message('Method not found.');
133 0 $self->error_data($msg);
134 }
135  
136 #--------------------------------------------------------
137  
138 =head2 invalid_params ( [ data ] )
139  
140 Sets an Invalid Params error as defined by the JSON-RPC 2.0 spec.
141  
142 =head3 data
143  
144 Optionally set some error data for the error.
145  
146 =cut
147  
148 sub invalid_params {
149 0 ▶ my ($self, $msg) = @_;
Subroutine invalid_params: not called CC=1 SCAR=6.9
Pod documented
150 0 $self->error_code(-32602);
151 0 $self->error_message('Invalid params.');
152 0 $self->error_data($msg);
153 }
154  
155 #--------------------------------------------------------
156  
157 =head2 internal_error ( [ data ] )
158  
159 Sets an Internal Error as defined by the JSON-RPC 2.0 spec.
160  
161 =head3 data
162  
163 Optionally set some error data for the error.
164  
165 =cut
166  
167 sub internal_error {
168 0 ▶ my ($self, $msg) = @_;
Subroutine internal_error: not called CC=1 SCAR=6.9
Pod documented
169 0 $self->error_code(-32603);
170 0 $self->error_message('Internal error.');
171 0 $self->error_data($msg);
172 }
173  
174 #--------------------------------------------------------
175  
176 =head2 method ( [ name ] )
177  
178 Returns the name of the procedure to be called.
179  
180 =head3 name
181  
182 An alphanumeric string. Sets the method name. Will set an error if the method name is not alpha-numeric.
183  
184 =cut
185  
186 has method => (
187 is => 'rw',
188 default => undef,
189 trigger => sub {
190 0 ▶ my ($self, $new, $old) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
191 0 ▶ if (defined $new && $new !~ m{^[A-Za-z0-9_]+$}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new and not $new =~ /^[A-Za-z0-9_]+$/msx) 0 0 0
Condition0%0/3
defined $new and not $new =~ /^[A-Za-z0-9_]+$/msx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $new and not $new =~ /^[A-Za-z0-9_]+$/msx
defined $new not $new =~ /^[A-Za-z0-9_]+$/msx
Truth table0%0/3
A and not(B)
A= defined $newB= not $new =~ /^[A-Za-z0-9_]+$/msx
ABresult
0X0
100
111
192 0 $self->invalid_request($new.' is not a valid method name.');
193 }
194 },
195 2 );
196  
197 #--------------------------------------------------------
198  
199 =head2 params ( [ data ] )
200  
201 Returns the parameters to be passed into the procedure.
202  
203 =head3 data
204  
205 An array or hashref. Sets the parameters. Will set an error if the params are not an array ref or hash ref.
206  
207 =cut
208  
209 2 has params => (
210 is => 'rw',
211 default => undef,
212 );
213  
214  
215 #--------------------------------------------------------
216  
217 =head2 id ( [ id ] )
218  
219 Returns the id of the request.
220  
221 =head3 id
222  
223 Sets the id of the request.
224  
225 =cut
226  
227 2 has id => (
228 is => 'rw',
229 default => undef,
230 );
231  
232 #--------------------------------------------------------
233  
234 =head2 result ( [ data ] )
235  
236 Returns the data that will be sent back to the client.
237  
238 =head3 data
239  
240 Sets the data that will be sent back to the client.
241  
242 =cut
243  
244 2 has result => (
245 is => 'rw',
246 default => undef,
247 );
248  
249 #--------------------------------------------------------
250  
251 =head2 response ( )
252  
253 Formats the data stored in this object into the data structure expected by L<JSON::RPC::Dispatcher>, which will ultimately be returned to the client.
254  
255 =cut
256  
257 sub response {
258 0 ▶ my ($self) = @_;
Subroutine response: not called CC=2 SCAR=17.9
Pod documented
259 0 my $error;
260 0 ▶ if ($self->has_error_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_error_code) 0 0 0
261 0 $error = {
262 code => $self->error_code,
263 message => $self->error_message,
264 data => $self->error_data,
265 };
266 }
267 return {
268 0 jsonrpc => '2.0',
269 id => $self->id,
270 result => $self->result,
271 error => $error,
272 };
273 }
274  
275 =head1 LEGAL
276  
277 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.
278  
279 =cut
280  
281 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
282 2 __PACKAGE__->meta->make_immutable;
« Dispatcher/App.pm ↑ Summary