AnyEvent/Server.pm

filter: Statementstmt 91.0% 91 / 100 Branchbran 71.4% 30 / 42 Conditioncond 91.6% 11 / 12 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 95.8% 23 / 24 Podpod 100% 3 / 3 totaltotal 86.5% 161 / 186 CC 28 SCAR 15.2
CC
28
Cov
86%
_check_format
11.3
_parse_argspec
5.3
__ANON__
4.7
CRAP 4.6
« AnyEvent/Server/Handle.pm ↑ Summary AnyEvent.pm »
1 package JSON::RPC2::AnyEvent::Server;
2 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = "0.03";
7  
8 5 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use JSON::RPC2::AnyEvent::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 4 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
17 4 my $class = shift;
18 4 my $self = bless {}, $class;
19 4 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 4 12
20 8 my $method = shift;
21 8 my $spec = shift;
22 8 ▶ if ( (reftype $spec // '') eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($spec) // '') eq 'CODE') { } 5 3 8
Condition100%2/2
builtin::reftype($spec) // ""
l!l
53
MC/DC100%1/1
builtin::reftype($spec) // ""
builtin::reftype($spec)
Truth table100%2/2
A
A= builtin::reftype($spec)
Aresult
11
00
23 5 $self->register($method, $spec);
24 } else {
25 3 $self->register($method, $spec, shift);
26 }
27 }
28 4 $self;
29 }
30  
31 11 ▶sub dispatch {
Subroutine dispatch: called CC=1 SCAR=0.3
Pod documented
32 11 my $self = shift;
33 11 my $json = shift;
34 11 my $ret_cv = AE::cv;
35 11 ▶ try{
Subroutine __ANON__: called CC=3 SCAR=12.6
36 11 my $type = _check_format($json); # die when $json's format is invalid
37 9 my $method = $self->{$json->{method}};
38 9 ▶ unless ( $method ) { # Method not found
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 9 9
39 0 $ret_cv->send(_make_error_response($json->{id}, ERR_METHOD_NOT_FOUND, 'Method not found'));
40 0 return $ret_cv;
41 }
42 9 ▶ if ( $type eq 'c' ) { # RPC call
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'c') { } 9 0 9
43 9 ▶ $method->(AE::cv{
Subroutine __ANON__: called CC=1 SCAR=0.2
44 9 my $cv = shift;
45 9 ▶ try{
Subroutine __ANON__: called CC=1 SCAR=0.0
46 9 $ret_cv->send(_make_response($json->{id}, $cv->recv));
47 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 $ret_cv->send(_make_error_response($json->{id}, ERR_SERVER_ERROR, 'Server error', shift));
49 9 };
50 9 }, $json->{params});
51 9 return $ret_cv;
52 } else { # Notification request (no response)
53 0 $method->(AE::cv, $json->{params}); # pass dummy cv
54 0 return undef;
55 }
56 2 ▶ } catch { # Invalid request
Subroutine __ANON__: called CC=2 SCAR=7.1
57 2 ▶ my $err = _make_error_response((reftype $json eq 'HASH' ? $json->{id} : undef), ERR_INVALID_REQUEST, 'Invalid Request', shift);
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($json) eq 'HASH' ? : 2 0 2
58 2 $ret_cv->send($err);
59 2 return $ret_cv;
60 11 };
61 }
62  
63 11 ▶sub _check_format {
Subroutine _check_format: called CC=11 SCAR=24.2
64 # Returns
65 # "c" : when the value represents rpc call
66 # "n" : when the value represents notification
67 # croak: when the value is in invalid format
68 11 my $json = shift;
69 11 ▶ reftype $json eq 'HASH' or croak "JSON-RPC request MUST be an Object (hash)";
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($json) eq "HASH" or croak("JSON-RPC request MUST be an Object (hash)") 11 0 11
70 #$json->{jsonrpc} eq "2.0" or croak "Unsupported JSON-RPC version"; # This module supports only JSON-RPC 2.0 spec, but here just ignores this member.
71 11 ▶ exists $json->{method} && not ref $json->{method} or croak "`method' MUST be a String value";
Branch: 1 branch
BranchTrueFalseTotal
exists $json->{"method"} and not ref $json->{"method"} or croak("`method' MUST be a String value") 10 1 11
Condition66%2/3
exists $json->{"method"} and not ref $json->{"method"}
!ll&&!rl&&r
1010
MC/DC50%1/2
exists $json->{"method"} and not ref $json->{"method"}
exists $json->{"method"} not ref $json->{"method"}
Truth table66%2/3
A and not(B)
A= exists $json->{"method"}B= not ref $json->{"method"}
ABresult
0X0
100
111
72 10 ▶ if ( exists $json->{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $json->{'params'}) { } 9 1 10
73 9 ▶ (reftype $json->{params} // '') eq 'ARRAY' || (reftype $json->{params} // '') eq 'HASH' or croak "`params' MUST be an array or an object";
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($json->{"params"}) // "") eq "ARRAY" or (builtin::reftype($json->{"params"}) // "") eq "HASH" or croak("`params' MUST be an array or an object") 8 1 9
Condition100%7/7
(builtin::reftype($json->{"params"}) // "") eq "ARRAY" or (builtin::reftype($json->{"params"}) // "") eq "HASH"
l!l&&r!l&&!r
711
builtin::reftype($json->{"params"}) // ""
l!l
81
builtin::reftype($json->{"params"}) // ""
l!l
11
MC/DC50%1/2
(builtin::reftype($json->{"params"}) // "") eq "ARRAY" or (builtin::reftype($json->{"params"}) // "") eq "HASH"
builtin::reftype($json->{"params"}) builtin::reftype($json->{"params"})
Truth table66%2/3
A or B
A= builtin::reftype($json->{"params"})B= builtin::reftype($json->{"params"})
ABresult
1X1
011
000
74 } else {
75 1 $json->{params} = [];
76 }
77 9 ▶ return 'n' unless exists $json->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $json->{"id"} 0 9 9
78 9 ▶ not ref $json->{id} or croak "`id' MUST be neighter an array nor an object";
Branch: 1 branch
BranchTrueFalseTotal
ref $json->{"id"} and croak("`id' MUST be neighter an array nor an object") 0 9 9
79 9 return 'c';
80 }
81  
82 9 ▶sub _make_response {
Subroutine _make_response: called CC=1 SCAR=0.0
83 9 my ($id, $result) = @_;
84 {
85 9 jsonrpc => '2.0',
86 id => $id,
87 result => $result,
88 };
89 }
90  
91 2 ▶sub _make_error_response {
Subroutine _make_error_response: called CC=2 SCAR=7.2
92 2 my ($id, $code, $msg, $data) = @_;
93 {
94 2 ▶ jsonrpc => '2.0',
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 2 0 2
95 id => $id,
96 error => {
97 code => $code,
98 message => "$msg",
99 (defined $data ? (data => $data) : ()),
100 },
101 };
102 }
103  
104  
105 8 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
106 8 my $self = shift;
107 8 my ($method, $spec, $code) = @_;
108 8 ▶ if ( UNIVERSAL::isa($spec, "CODE") ) { # spec is omitted.
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($spec, 'CODE')) { } 5 3 8
109 5 $code = $spec;
110 5 ▶ $spec = sub{ $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
111 } else {
112 3 $spec = _parse_argspec($spec);
113 3 ▶ croak "`$code' is not CODE ref" unless UNIVERSAL::isa($code, 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($code, "CODE") 0 3 3
114 }
115 9 ▶ $self->{$method} = sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
116 9 my ($cv, $params) = @_;
117 9 $code->($cv, $spec->($params), $params);
118 8 };
119 8 $self;
120 }
121  
122 3 ▶sub _parse_argspec {
Subroutine _parse_argspec: called CC=5 SCAR=16.7
123 3 my $orig = my $spec = shift;
124 3 ▶ if ( $spec =~ s/^\s*\[\s*// ) { # Wants array
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ s/^\s*\[\s*//) { } 1 2 3
elsif ($spec =~ s/\s*\{\s*//) { } 2 0 2
125 1 ▶ croak "Invalid argspec. Unmatched '[' in argspec: $orig" unless $spec =~ s/\s*\]\s*$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ s/\s*\]\s*$// 0 1 1
126 1 my @parts = split /\s*,\s*/, $spec;
127 1 ▶ return sub{
Subroutine __ANON__: called CC=3 SCAR=15.4
128 1 my $params = shift;
129 1 ▶ return $params if UNIVERSAL::isa($params, 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($params, "ARRAY") 1 0 1
130 # Got a hash! Then, convert it to an array!
131 0 my $args = [];
132 0 push @$args, $params->{$_} foreach @parts;
133 0 return $args;
134 1 };
135 } elsif ( $spec =~ s/\s*\{\s*// ) { # Wants hash
136 2 ▶ croak "Invalid argspec. Unmatched '{' in argspec: $orig" unless $spec =~ s/\s*\}\s*$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ s/\s*\}\s*$// 0 2 2
137 2 my @parts = split /\s*,\s*/, $spec;
138 3 ▶ return sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
139 3 my $params = shift;
140 3 ▶ return $params if UNIVERSAL::isa($params, 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($params, "HASH") 1 2 3
141 # Got an array! Then, convert it to a hash!
142 2 my $args = {};
143 2 ▶ for ( my $i=0; $i < @parts; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @parts) 8 2 10
144 8 $args->{$parts[$i]} = $params->[$i];
145 }
146 2 return $args;
147 2 };
148 } else {
149 0 croak "Invalid argspec. Argspec must be enclosed in [] or {}: $orig";
150 }
151 }
152  
153  
154  
155 5 1;
156 __END__
« AnyEvent/Server/Handle.pm ↑ Summary AnyEvent.pm »