Lim/RPC/Protocol/REST.pm

filter: Statementstmt 25.5% 38 / 149 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 60.0% 12 / 20 Podpod 100% 7 / 7 totaltotal 21.1% 57 / 269 CC 40 SCAR 49.7
CC
40
Cov
16%
handle
342.0
__ANON__
132.0
response
90.0
CRAP 144.5
« Lim/Util.pm ↑ Summary Lim/CLI.pm »
1 package Lim::RPC::Protocol::REST;
2  
3 5 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Lim::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Lim::RPC::Callback ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶use base qw(Lim::RPC::Protocol);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =encoding utf8
20  
21 =head1 NAME
22  
23 ...
24  
25 =head1 VERSION
26  
27 See L<Lim> for version.
28  
29 =cut
30  
31 5 our $VERSION = $Lim::VERSION;
32 5 our $JSON = JSON::XS->new->utf8->convert_blessed;
33 5 our %REST_CRUD = (
34 GET => 'READ',
35 PUT => 'UPDATE',
36 POST => 'CREATE',
37 DELETE => 'DELETE'
38 );
39  
40 =head1 SYNOPSIS
41  
42 ...
43  
44 =head1 SUBROUTINES/METHODS
45  
46 =head2 Init
47  
48 =cut
49  
50 0 ▶sub Init {
Subroutine Init: not called CC=2 SCAR=17.9
Pod documented
51 0 ▶ if (Lim::Config->{rpc}->{json}->{pretty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Lim::Config()->{"rpc"}{"json"}{"pretty"}) 0 0 0
52 0 $JSON->pretty(1);
53 }
54 }
55  
56 =head2 Destroy
57  
58 =cut
59  
60 0 ▶sub Destroy {
Subroutine Destroy: not called CC=1 SCAR=0.0
Pod documented
61 }
62  
63 =head2 name
64  
65 =cut
66  
67 4 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
68 4 'rest';
69 }
70  
71 =head2 serve
72  
73 =cut
74  
75 0 ▶sub serve {
Subroutine serve: not called CC=1 SCAR=0.0
Pod documented
76 }
77  
78 =head2 handle
79  
80 =cut
81  
82 0 ▶sub handle {
Subroutine handle: not called CC=18 SCAR=58.3
Pod documented
83 0 my ($self, $cb, $request) = @_;
84  
85 0 ▶ unless (blessed($request) and $request->isa('HTTP::Request')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($request) and $request->isa("HTTP::Request")) 0 0 0
Condition0%0/3
builtin::blessed($request) and $request->isa("HTTP::Request")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($request) and $request->isa("HTTP::Request")
builtin::blessed($request) $request->isa("HTTP::Request")
Truth table0%0/3
A and B
A= builtin::blessed($request)B= $request->isa("HTTP::Request")
ABresult
0X0
100
111
86 0 return;
87 }
88  
89 0 ▶ if ($request->uri =~ /^\/([a-zA-Z]+)\/(\w+)(?:\/([^\?]*)){0,1}/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->uri =~ m[^/([a-zA-Z]+)/(\w+)(?:/([^\?]*)){0,1}]ou) 0 0 0
90 0 my ($module, $function, $parameters) = ($1, $2, $3);
91 0 my $response = HTTP::Response->new;
92 0 $response->request($request);
93 0 $response->protocol($request->protocol);
94  
95 0 $module = lc($module);
96 0 my $server = $self->server;
97 0 ▶ if (defined $server and $server->have_module($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $server and $server->have_module($module)) { } 0 0 0
Condition0%0/3
defined $server and $server->have_module($module)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $server and $server->have_module($module)
defined $server $server->have_module($module)
Truth table0%0/3
A and B
A= defined $serverB= $server->have_module($module)
ABresult
0X0
100
111
98 0 my ($method, $call);
99  
100 0 ▶ if (exists $REST_CRUD{$request->method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Lim::RPC::Protocol::REST::REST_CRUD{$request->method}) { } 0 0 0
101 0 $method = lc($REST_CRUD{$request->method});
102 }
103 else {
104 0 $method = lc($request->method);
105 }
106 0 $function = lc($function);
107 0 $call = ucfirst($method).Lim::Util::Camelize($function);
108  
109 0 my $obj;
110 0 ▶ if ($server->have_module_call($module, $call)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->have_module_call($module, $call)) 0 0 0
111 0 $obj = $server->module_obj_by_protocol($module, $self->name);
112 }
113  
114 0 my ($query, $jsonp);
115 0 ▶ if (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 0 0 0
116 0 ▶ Lim::DEBUG and $self->{logger}->debug('API call ', $module, '->', $call, '()');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("API call ", $module, "->", $call, "()") 0 0 0
117  
118 0 ▶ if ($request->header('Content-Type') =~ /(?:^|\s)application\/x-www-form-urlencoded(?:$|\s|;)/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($request->header('Content-Type') =~ m[(?:^|\s)application/x-www-form-urlencoded(?:$|\s|;)]ou) { } 0 0 0
elsif ($request->header('Content-Type') =~ m[(?:^|\s)application/json(?:$|\s|;)]ou) { } 0 0 0
119 0 my $query_str = $request->content;
120 0 $query_str =~ s/[\015\012]+$//o;
121  
122 0 $query = Lim::Util::QueryDecode($query_str);
123 }
124 elsif ($request->header('Content-Type') =~ /(?:^|\s)application\/json(?:$|\s|;)/o) {
125 0 eval {
126 0 $query = $JSON->decode($request->content);
127 };
128 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
129 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
130 0 undef($query);
131 0 undef($obj);
132 }
133 }
134 else {
135 0 $query = Lim::Util::QueryDecode($request->uri->query);
136 }
137  
138 0 $jsonp = delete $query->{jsonpCallback};
139  
140 0 ▶ if (defined $parameters) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parameters) 0 0 0
141 0 my $redirect_call = $server->process_module_call_uri_map($module, $call, $parameters, $query);
142  
143 0 ▶ if (defined $redirect_call and $redirect_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redirect_call and $redirect_call) 0 0 0
Condition0%0/3
defined $redirect_call and $redirect_call
!ll&&!rl&&r
000
MC/DC0%0/2
defined $redirect_call and $redirect_call
defined $redirect_call $redirect_call
Truth table0%0/3
A and B
A= defined $redirect_callB= $redirect_call
ABresult
0X0
100
111
144 0 ▶ Lim::DEBUG and $self->{logger}->debug('API call redirected ', $call, ' => ', $redirect_call);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("API call redirected ", $call, " => ", $redirect_call) 0 0 0
145 0 $call = $redirect_call;
146 }
147 }
148 }
149  
150 0 ▶ if (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 0 0 0
151 0 my $real_self = $self;
152 0 weaken($self);
153 $obj->$call(Lim::RPC::Callback->new(
154 request => $request,
155 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
156 0 my ($result) = @_;
157  
158 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
159 0 return;
160 }
161  
162 0 ▶ if (blessed $result and $result->isa('Lim::Error')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($result) and $result->isa('Lim::Error')) { } 0 0 0
elsif (ref $result eq 'HASH') { } 0 0 0
Condition0%0/3
builtin::blessed($result) and $result->isa("Lim::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($result) and $result->isa("Lim::Error")
builtin::blessed($result) $result->isa("Lim::Error")
Truth table0%0/3
A and B
A= builtin::blessed($result)B= $result->isa("Lim::Error")
ABresult
0X0
100
111
163 0 $response->code($result->code);
164 0 eval {
165 0 $response->content($JSON->encode($result));
166 };
167 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
168 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
169 0 ▶ Lim::WARN and $self->{logger}->warn('JSON encode error: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("JSON encode error: ", $@) 0 0 0
170 }
171 else {
172 0 $response->header(
173 'Content-Type' => 'application/json; charset=utf-8',
174 'Cache-Control' => 'no-cache',
175 'Pragma' => 'no-cache'
176 );
177  
178 0 ▶ if (defined $jsonp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jsonp) 0 0 0
179 0 $response->content($jsonp.'('.$response->content().');');
180 0 $response->header('Content-Type' => 'application/javascript; charset=utf-8');
181 }
182 }
183 }
184 elsif (ref($result) eq 'HASH') {
185 0 eval {
186 0 $response->content($JSON->encode($result));
187 };
188 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
189 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
190 0 ▶ Lim::WARN and $self->{logger}->warn('JSON encode error: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("JSON encode error: ", $@) 0 0 0
191 }
192 else {
193 0 $response->header(
194 'Content-Type' => 'application/json; charset=utf-8',
195 'Cache-Control' => 'no-cache',
196 'Pragma' => 'no-cache'
197 );
198 0 $response->code(HTTP_OK);
199  
200 0 ▶ if (defined $jsonp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jsonp) 0 0 0
201 0 $response->content($jsonp.'('.$response->content().');');
202 0 $response->header('Content-Type' => 'application/javascript; charset=utf-8');
203 }
204 }
205 }
206 else {
207 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
208 }
209  
210 0 $cb->cb->($response);
211 0 return;
212 },
213 0 ▶ reset_timeout => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 $cb->reset_timeout;
215 0 }), $query);
216 0 return 1;
217 }
218 }
219 else {
220 0 return;
221 }
222  
223 0 $cb->cb->($response);
224 0 return 1;
225 }
226 0 return;
227 }
228  
229 =head2 request
230  
231 =cut
232  
233 0 ▶sub request {
Subroutine request: not called CC=4 SCAR=30.0
Pod documented
234 0 my $self = shift;
235 0 my %args = ( @_ );
236  
237 0 my ($method, $uri) = Lim::Util::URIize($args{call});
238 0 ▶ $uri = (defined $args{path} ? $args{path} : '' ).'/'.lc($args{plugin}).$uri;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'path'} ? : 0 0 0
239 0 my $request = HTTP::Request->new($method, $uri);
240  
241 0 ▶ if (defined $args{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'data'}) { } 0 0 0
242 0 eval {
243 0 $request->content($JSON->encode($args{data}));
244 };
245 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
246 0 confess 'JSON encoding of data failed: '.$@;
247 }
248 0 $request->header('Content-Type' => 'application/json; charset=utf-8');
249 0 $request->header('Content-Length' => length($request->content));
250 }
251 else {
252 0 $request->header('Content-Length' => 0);
253 }
254  
255 0 return $request;
256 }
257  
258 =head2 response
259  
260 =cut
261  
262 0 ▶sub response {
Subroutine response: not called CC=9 SCAR=45.0
Pod documented
263 0 my ($self, $response) = @_;
264 0 my $data = {};
265  
266 0 ▶ unless (blessed $response and $response->isa('HTTP::Response')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($response) and $response->isa("HTTP::Response")) 0 0 0
Condition0%0/3
builtin::blessed($response) and $response->isa("HTTP::Response")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($response) and $response->isa("HTTP::Response")
builtin::blessed($response) $response->isa("HTTP::Response")
Truth table0%0/3
A and B
A= builtin::blessed($response)B= $response->isa("HTTP::Response")
ABresult
0X0
100
111
267 0 return;
268 }
269  
270 0 ▶ if ($response->header('Content-Length')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header('Content-Length')) { } 0 0 0
271 0 ▶ if ($response->header('Content-Type') =~ /application\/json/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header('Content-Type') =~ m[application/json]iou) { } 0 0 0
272 0 eval {
273 0 $data = $JSON->decode($response->decoded_content);
274 };
275 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
276 0 return Lim::Error->new(
277 message => 'JSON decode error: '.$@,
278 module => $self
279 );
280 }
281  
282 0 ▶ if (ref($data) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "HASH") 0 0 0
283 0 return Lim::Error->new(
284 message => 'Invalid data returned, not a hash',
285 module => $self
286 );
287 }
288  
289 0 ▶ unless ($response->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->code == 200) 0 0 0
290 0 return Lim::Error->new->set($data);
291 }
292 }
293 else {
294 0 return Lim::Error->new(
295 message => 'Unknown content type ['.$response->header('Content-Type').'] returned',
296 module => $self
297 );
298 }
299 }
300 else {
301 0 ▶ unless ($response->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->code == 200) 0 0 0
302 0 return Lim::Error->new(
303 code => $response->code,
304 module => $self
305 );
306 }
307 }
308  
309 0 return $data;
310 }
311  
312 =head1 AUTHOR
313  
314 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
315  
316 =head1 BUGS
317  
318 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
319  
320 =head1 SUPPORT
321  
322 You can find documentation for this module with the perldoc command.
323  
324 perldoc Lim
325  
326 You can also look for information at:
327  
328 =over 4
329  
330 =item * Lim issue tracker (report bugs here)
331  
332 L<https://github.com/jelu/lim/issues>
333  
334 =back
335  
336 =head1 ACKNOWLEDGEMENTS
337  
338 =head1 LICENSE AND COPYRIGHT
339  
340 Copyright 2012-2013 Jerry Lundström.
341  
342 This program is free software; you can redistribute it and/or modify it
343 under the terms of either: the GNU General Public License as published
344 by the Free Software Foundation; or the Artistic License.
345  
346 See http://dev.perl.org/licenses/ for more information.
347  
348  
349 =cut
350  
351 5 1; # End of Lim::RPC::Protocol::REST
« Lim/Util.pm ↑ Summary Lim/CLI.pm »