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