blib/lib/Mojolicious/Plugin/JSONRPC2.pm

filter: Statementstmt 100% 62 / 62 Branchbran 100% 14 / 14 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 98.1% 105 / 107 CC 12 SCAR 12.8
CC
12
Cov
99%
_srv
8.0
_shortcut
4.0
__ANON__
2.0
CRAP 3.6
↑ Summary
1 package Mojolicious::Plugin::JSONRPC2;
2  
3 3 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = 'v2.0.4';
7  
8 3 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # to ensure callback runs on notification
10 3 ▶use JSON::RPC2::Server 0.004000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use constant TIMEOUT => 5*60; # sec
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use constant HTTP_200 => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use constant HTTP_204 => 204;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use constant HTTP_415 => 415;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 my $Type = 'application/json';
18 3 my %HEADERS = (
19 'Content-Type' => qr{\A\s*\Q$Type\E\s*(?:;|\z)}msi,
20 'Accept' => qr{(?:\A|,)\s*\Q$Type\E\s*(?:[;,]|\z)}msi,
21 );
22  
23  
24 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
25 1 my ($self, $app, $conf) = @_;
26  
27 1 ▶ $app->helper(jsonrpc2_headers => sub { return %HEADERS });
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 1 ▶ $app->routes->add_shortcut(jsonrpc2 => sub { _shortcut('POST', @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 ▶ $app->routes->add_shortcut(jsonrpc2_get => sub { _shortcut('GET', @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32 1 return;
33 }
34  
35 9 ▶sub _shortcut {
Subroutine _shortcut: called CC=4 SCAR=13.9
36 9 my ($method, $r, $path, $server) = @_;
37 9 ▶ croak 'usage: $r->jsonrpc2'.($method eq 'GET' ? '_get' : q{}).'("/rpc/path", JSON::RPC2::Server->new)'
Branch: 2 branches
BranchTrueFalseTotal
unless ref $server and $server->isa("JSON::RPC2::Server") 4 5 9
$method eq 'GET' ? : 1 3 4
Condition100%3/3
ref $server and $server->isa("JSON::RPC2::Server")
!ll&&!rl&&r
315
MC/DC100%2/2
ref $server and $server->isa("JSON::RPC2::Server")
ref $server $server->isa("JSON::RPC2::Server")
Truth table100%3/3
A and B
A= ref $serverB= $server->isa("JSON::RPC2::Server")
ABresult
0X0
100
111
38 if !(ref $server && $server->isa('JSON::RPC2::Server'));
39 5 ▶ return $r->any([$method] => $path, [format => 0], sub { _srv($server, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
40 }
41  
42 15 ▶sub _srv {
Subroutine _srv: called CC=8 SCAR=20.8
43 15 my ($server, $c) = @_;
44  
45 15 ▶ if (($c->req->headers->content_type // q{}) !~ /$HEADERS{'Content-Type'}/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($c->req->headers->content_type // "") =~ /$HEADERS{"Content-Type"}/msu) 5 10 15
Condition100%2/2
$c->req->headers->content_type // ""
l!l
123
MC/DC100%1/1
$c->req->headers->content_type // ""
$c->req->headers->content_type
Truth table100%2/2
A
A= $c->req->headers->content_type
Aresult
11
00
46 5 return $c->render(status => HTTP_415, data => q{});
47 }
48 10 ▶ if (($c->req->headers->accept // q{}) !~ /$HEADERS{'Accept'}/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($c->req->headers->accept // "") =~ /$HEADERS{"Accept"}/msu) 2 8 10
Condition50%1/2
$c->req->headers->accept // ""
l!l
100
MC/DC0%0/1
$c->req->headers->accept // ""
$c->req->headers->accept
Truth table50%1/2
A
A= $c->req->headers->accept
Aresult
11
00
49 2 return $c->render(status => HTTP_415, data => q{});
50 }
51  
52 8 $c->res->headers->content_type($Type);
53 8 $c->render_later;
54  
55 8 ▶ my $timeout = $c->stash('jsonrpc2.timeout') || TIMEOUT;
Condition100%2/2
$c->stash('jsonrpc2.timeout') || 300
l!l
17
MC/DC100%1/1
$c->stash('jsonrpc2.timeout') || 300
$c->stash('jsonrpc2.timeout')
Truth table100%2/2
A
A= $c->stash('jsonrpc2.timeout')
Aresult
11
00
56 8 $c->inactivity_timeout($timeout);
57  
58 8 my $request;
59 8 ▶ if ($c->req->method eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->method eq 'GET') { } 2 6 8
60 2 $request = $c->req->query_params->to_hash;
61 2 ▶ if (exists $request->{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{"params"}) 1 1 2
62 1 $request->{params} = eval { decode_json($request->{params}) };
63 }
64 } else {
65 6 $request = eval { decode_json($c->req->body) };
66 }
67  
68 7 ▶ $server->execute($request, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
69 7 my ($json_response) = @_;
70 7 ▶ my $status = $json_response ? HTTP_200 : HTTP_204;
Branch: 1 branch
BranchTrueFalseTotal
$json_response ? : 6 1 7
71 7 $c->render(status => $status, data => $json_response);
72 8 });
73  
74 8 return;
75 }
76  
77  
78 3 1; # Magic true value required at end of module
79 __END__
↑ Summary