RPC/Parser.pm

filter: Statementstmt 100% 46 / 46 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 94.3% 67 / 71 CC 8 SCAR 10.1
CC
8
Cov
100%
construct_from_post_req
5.0
construct_from_req
3.0
construct_from_get_req
2.0
CRAP 2.8
« RPC/Legacy/Server/CGI.pm ↑ Summary RPC/Test.pm »
1 package JSON::RPC::Parser;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use JSON::RPC::Procedure;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use Class::Accessor::Lite
7 3 ▶ new => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
8 rw => [ qw(
9 coder
10 ) ]
11 3 ;
12  
13 20 ▶sub construct_procedure {
Subroutine construct_procedure: called CC=1 SCAR=0.0
Pod undocumented
14 20 my $self = shift;
15 20 JSON::RPC::Procedure->new( @_ );
16 }
17  
18 21 ▶sub construct_from_req {
Subroutine construct_from_req: called CC=3 SCAR=11.0
Pod undocumented
19 21 my ($self, $req) = @_;
20  
21 21 my $method = $req->method;
22 21 my $proc;
23 21 ▶ if ($method eq 'POST') {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'POST') { } 15 6 21
elsif ($method eq 'GET') { } 5 1 6
24 15 $proc = $self->construct_from_post_req( $req );
25 } elsif ($method eq 'GET') {
26 5 $proc = $self->construct_from_get_req( $req );
27 } else {
28 1 Carp::croak( "Invalid method: $method" );
29 }
30  
31 18 return $proc;
32 }
33  
34 15 ▶sub construct_from_post_req {
Subroutine construct_from_post_req: called CC=5 SCAR=16.1
Pod undocumented
35 15 my ($self, $req) = @_;
36  
37 15 my $request = eval { $self->coder->decode( $req->content ) };
38 15 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 14 15
39 1 Carp::croak( "JSON parse error: $@" );
40 }
41  
42 14 my $ref = ref $request;
43 14 ▶ if ($ref ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref ne "ARRAY") 8 6 14
44 # is not a batch request
45 return $self->construct_procedure(
46 method => $request->{method},
47 id => $request->{id},
48 params => $request->{params},
49 jsonrpc => $request->{jsonrpc},
50 has_id => exists $request->{id},
51 8 );
52 }
53  
54 6 my @procs;
55 6 foreach my $req ( @$request ) {
56 8 ▶ Carp::croak( "Invalid parameter") unless ref $req eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $req eq "HASH" 1 7 8
57 push @procs, $self->construct_procedure(
58 method => $req->{method},
59 id => $req->{id},
60 params => $req->{params},
61 jsonrpc => $req->{jsonrpc},
62 has_id => exists $req->{id}, # when not true it's a notification in JSON-RPC 2.0
63 7 );
64 }
65 5 return \@procs;
66 }
67  
68 5 ▶sub construct_from_get_req {
Subroutine construct_from_get_req: called CC=2 SCAR=6.9
Pod undocumented
69 5 my ($self, $req) = @_;
70  
71 5 my $params = $req->query_parameters;
72 5 my $decoded_params;
73 5 ▶ if ($params->{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"params"}) 4 1 5
74 4 $decoded_params = eval { $self->coder->decode( $params->{params} ) };
75 }
76 return $self->construct_procedure(
77 method => $params->{method},
78 id => $params->{id},
79 params => $decoded_params,
80 jsonrpc => $params->{jsonrpc},
81 has_id => exists $params->{id},
82 5 );
83 }
84  
85 3 1;
86  
87 __END__
« RPC/Legacy/Server/CGI.pm ↑ Summary RPC/Test.pm »