blib/lib/JSON/RPC2/Server.pm

filter: Statementstmt 63.2% 81 / 128 Branchbran 40.3% 21 / 52 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 73.9% 17 / 23 Podpod 100% 6 / 6 totaltotal 56.5% 129 / 228 CC 32 SCAR 33.9
CC
32
Cov
55%
_execute
60.7
execute
37.5
__ANON__
20.0
CRAP 29.6
« blib/lib/JSON/RPC2/Client.pm ↑ Summary Build »
1 package JSON::RPC2::Server;
2 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = 'v2.1.1';
9  
10 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use constant ERR_PARSE => -32700;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use constant ERR_REQ => -32600;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use constant ERR_METHOD => -32601;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use constant ERR_PARAMS => -32602;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 sub new {
19 2 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 2 my $self = {
21 method => {},
22 };
23 2 return bless $self, $class;
24 }
25  
26 sub register {
27 4 ▶ my ($self, $name, $cb) = @_;
Subroutine register: called CC=1 SCAR=0.0
Pod documented
28 4 $self->{method}{ $name } = [ $cb, 1, 0 ];
29 4 return;
30 }
31  
32 sub register_named {
33 0 ▶ my ($self, $name, $cb) = @_;
Subroutine register_named: not called CC=1 SCAR=6.9
Pod documented
34 0 $self->{method}{ $name } = [ $cb, 1, 1 ];
35 0 return;
36 }
37  
38 sub register_nb {
39 0 ▶ my ($self, $name, $cb) = @_;
Subroutine register_nb: not called CC=1 SCAR=6.9
Pod documented
40 0 $self->{method}{ $name } = [ $cb, 0, 0 ];
41 0 return;
42 }
43  
44 sub register_named_nb {
45 0 ▶ my ($self, $name, $cb) = @_;
Subroutine register_named_nb: not called CC=1 SCAR=6.9
Pod documented
46 0 $self->{method}{ $name } = [ $cb, 0, 1 ];
47 0 return;
48 }
49  
50 sub execute {
51 4 ▶ my ($self, $json, $cb) = @_;
Subroutine execute: called CC=9 SCAR=36.2
Pod documented
52 4 ▶ croak 'require 2 params' if 1+2 != @_;
Branch: 1 branch
BranchTrueFalseTotal
if 3 != @_ 0 4 4
53 4 ▶ croak 'second param must be callback' if ref $cb ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cb ne "CODE" 0 4 4
54  
55 4 undef $@;
56 4 ▶ my $request = ref $json ? $json : eval { decode_json($json) };
Branch: 1 branch
BranchTrueFalseTotal
ref $json ? : 2 2 4
57 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
58 0 return _error($cb, undef, ERR_PARSE, 'Parse error.');
59 }
60 4 ▶ if (ref $request eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request eq "HASH") 4 0 4
61 4 return $self->_execute($request, $cb);
62 }
63 0 ▶ if (ref $request ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request ne "ARRAY") 0 0 0
64 0 return _error($cb, undef, ERR_REQ, 'Invalid Request: expect Array or Object.');
65 }
66 0 ▶ if (!@{$request}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$request;}) 0 0 0
67 0 return _error($cb, undef, ERR_REQ, 'Invalid Request: empty Array.');
68 }
69  
70 0 my $pending = @{$request};
71 0 my @responses;
72 my $cb_acc = sub {
73 0 ▶ my ($json_response) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
74 0 ▶ if ($json_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_response) 0 0 0
75 0 push @responses, $json_response;
76 }
77 0 ▶ if (!--$pending) {
Branch: 1 branch
BranchTrueFalseTotal
unless (--$pending) 0 0 0
78 0 ▶ if (@responses) {
Branch: 1 branch
BranchTrueFalseTotal
if (@responses) { } 0 0 0
79 0 $cb->( '[' . join(q{,}, @responses) . ']' );
80 } else {
81 0 $cb->( q{} );
82 }
83 }
84 0 return;
85 0 };
86 0 for (@{$request}) {
87 0 $self->_execute($_, $cb_acc);
88 }
89  
90 0 return;
91 }
92  
93 sub _execute {
94 4 ▶ my ($self, $request, $cb) = @_;
Subroutine _execute: called CC=19 SCAR=41.1
95  
96 4 my $error = \&_error;
97 4 my $done = \&_done;
98  
99 # jsonrpc =>
100 4 ▶ if (!defined $request->{jsonrpc} || ref $request->{jsonrpc} || $request->{jsonrpc} ne '2.0') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $request->{"jsonrpc"} or ref $request->{"jsonrpc"} or $request->{"jsonrpc"} ne "2.0") 0 4 4
Condition33%2/6
not defined $request->{"jsonrpc"} or ref $request->{"jsonrpc"} or $request->{"jsonrpc"} ne "2.0"
l!l&&r!l&&!r
004
not defined $request->{"jsonrpc"} or ref $request->{"jsonrpc"}
l!l&&r!l&&!r
004
MC/DC0%0/3
not defined $request->{"jsonrpc"} or ref $request->{"jsonrpc"} or $request->{"jsonrpc"} ne "2.0"
not defined $request->{"jsonrpc"} ref $request->{"jsonrpc"} $request->{"jsonrpc"} ne "2.0"
Truth table25%1/4
not(A) or B or C
A= not defined $request->{"jsonrpc"}B= ref $request->{"jsonrpc"}C= $request->{"jsonrpc"} ne "2.0"
ABCresult
1XX1
01X1
0011
0000
101 0 return $error->($cb, undef, ERR_REQ, 'Invalid Request: expect {jsonrpc}="2.0".');
102 }
103  
104 # id =>
105 4 my $id;
106 4 ▶ if (exists $request->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{"id"}) 4 0 4
107 # Request
108 4 ▶ if (ref $request->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request->{"id"}) 0 4 4
109 0 return $error->($cb, undef, ERR_REQ, 'Invalid Request: expect {id} is scalar.');
110 }
111 4 $id = $request->{id};
112 }
113  
114 # method =>
115 4 ▶ if (!defined $request->{method} || ref $request->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $request->{"method"} or ref $request->{"method"}) 0 4 4
Condition33%1/3
not defined $request->{"method"} or ref $request->{"method"}
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $request->{"method"} or ref $request->{"method"}
not defined $request->{"method"} ref $request->{"method"}
Truth table33%1/3
not(A) or B
A= not defined $request->{"method"}B= ref $request->{"method"}
ABresult
1X1
011
000
116 0 return $error->($cb, $id, ERR_REQ, 'Invalid Request: expect {method} is String.');
117 }
118 4 my $handler = $self->{method}{ $request->{method} };
119 4 ▶ if (!$handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 0 4 4
120 0 return $error->($cb, $id, ERR_METHOD, 'Method not found.');
121 }
122 4 my ($method, $is_blocking, $is_named) = @{$handler};
123  
124 # params =>
125 4 ▶ if (!exists $request->{params}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $request->{"params"}) 0 4 4
126 0 ▶ $request->{params} = $is_named ? {} : [];
Branch: 1 branch
BranchTrueFalseTotal
$is_named ? : 0 0 0
127 }
128 4 ▶ if (ref $request->{params} ne 'ARRAY' && ref $request->{params} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request->{"params"} ne "ARRAY" and ref $request->{"params"} ne "HASH") 0 4 4
Condition33%1/3
ref $request->{"params"} ne "ARRAY" and ref $request->{"params"} ne "HASH"
!ll&&!rl&&r
400
MC/DC0%0/2
ref $request->{"params"} ne "ARRAY" and ref $request->{"params"} ne "HASH"
ref $request->{"params"} ne "ARRAY" ref $request->{"params"} ne "HASH"
Truth table33%1/3
A and B
A= ref $request->{"params"} ne "ARRAY"B= ref $request->{"params"} ne "HASH"
ABresult
0X0
100
111
129 0 return $error->($cb, $id, ERR_REQ, 'Invalid Request: expect {params} is Array or Object.');
130 }
131 4 ▶ if (ref $request->{params} ne ($is_named ? 'HASH' : 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $request->{"params"} ne ($is_named ? "HASH" : "ARRAY")) 0 4 4
$is_named ? : 0 4 4
132 0 ▶ return $error->($cb, $id, ERR_PARAMS, 'This method expect '.($is_named ? 'named' : 'positional').' params.');
Branch: 1 branch
BranchTrueFalseTotal
$is_named ? : 0 0 0
133 }
134 4 ▶ my @params = $is_named ? %{ $request->{params} } : @{ $request->{params} };
Branch: 1 branch
BranchTrueFalseTotal
$is_named ? : 0 4 4
135  
136 # id => (continue)
137 4 ▶ if (!exists $request->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $request->{"id"}) 0 4 4
138 # Notification
139 0 $error = \&_nothing;
140 0 $done = \&_nothing;
141 }
142  
143 # execute
144 4 ▶ if ($is_blocking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_blocking) { } 4 0 4
145 4 my @returns = $method->( @params );
146 4 $done->($cb, $id, \@returns);
147 }
148 else {
149 0 ▶ my $cb_done = sub { $done->($cb, $id, \@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 $method->( $cb_done, @params );
151 }
152 4 return;
153 }
154  
155 sub _done {
156 4 ▶ my ($cb, $id, $returns) = @_;
Subroutine _done: called CC=2 SCAR=6.9
157 4 my ($result, $code, $msg, $data) = @{$returns};
158 4 ▶ if (defined $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) 2 2 4
159 2 return _error($cb, $id, $code, $msg, $data);
160 }
161 2 return _result($cb, $id, $result);
162 }
163  
164 sub _error {
165 2 ▶ my ($cb, $id, $code, $message, $data) = @_;
Subroutine _error: called CC=2 SCAR=7.1
166 2 ▶ $cb->( encode_json({
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 2 2
167 jsonrpc => '2.0',
168 id => $id,
169 error => {
170 code => $code,
171 message => $message,
172 (defined $data ? ( data => $data ) : ()),
173 },
174 }) );
175 2 return;
176 }
177  
178 sub _result {
179 2 ▶ my ($cb, $id, $result) = @_;
Subroutine _result: called CC=1 SCAR=0.0
180 2 $cb->( encode_json({
181 jsonrpc => '2.0',
182 id => $id,
183 result => $result,
184 }) );
185 2 return;
186 }
187  
188 sub _nothing {
189 0 ▶ my ($cb) = @_;
Subroutine _nothing: not called CC=1 SCAR=6.9
190 0 $cb->( q{} );
191 0 return;
192 }
193  
194  
195 3 1; # Magic true value required at end of module
196 __END__
« blib/lib/JSON/RPC2/Client.pm ↑ Summary Build »