lib/MojoX/JSON/RPC/Client.pm

filter: Statementstmt 87.7% 93 / 106 Branchbran 66.6% 36 / 54 Conditioncond 58.8% 20 / 34 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 90.0% 18 / 20 Podpod 100% 2 / 2 totaltotal 73.9% 176 / 238 CC 42 SCAR 21.2
CC
42
Cov
77%
_process_result
15.8
call
14.1
prepare
11.0
CRAP 8.4
« lib/MojoX/JSON/RPC/Dispatcher.pm ↑ Summary lib/MojoX/JSON/RPC/Service.pm »
1 package MojoX::JSON::RPC::Client;
2  
3 3 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mojo::JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 has id => undef;
8 0 ▶has ua => sub { Mojo::UserAgent->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
9 3 has version => '2.0';
10 3 has content_type => 'application/json';
11 3 has tx => undef; # latest transaction
12  
13 37 ▶sub call {
Subroutine call: called CC=12 SCAR=26.5
Pod documented
14 37 my ( $self, $uri, $body, $callback ) = @_;
15  
16 # body might be json already, only encode if necessary
17 37 ▶ if ( ref $body eq 'HASH' || ref $body eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'HASH' or ref $body eq 'ARRAY') { } 30 7 37
Condition100%3/3
ref $body eq "HASH" or ref $body eq "ARRAY"
l!l&&r!l&&!r
2557
MC/DC100%2/2
ref $body eq "HASH" or ref $body eq "ARRAY"
ref $body eq "HASH" ref $body eq "ARRAY"
Truth table100%3/3
A or B
A= ref $body eq "HASH"B= ref $body eq "ARRAY"
ABresult
1X1
011
000
18 30 ▶ foreach my $o ( ref $body eq 'HASH' ? $body : @{$body} ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $body eq 'HASH' ? : 25 5 30
19 36 ▶ $o->{version} ||= $self->version;
Condition33%1/3
$o->{"version"} ||= $self->version
l!l&&r!l&&!r
0360
MC/DC0%0/2
$o->{"version"} ||= $self->version
$o->{"version"} $self->version
Truth table33%1/3
A ||= B
A= $o->{"version"}B= $self->version
ABresult
1X1
011
000
20 }
21 30 $body = encode_json($body);
22 }
23 else {
24 7 ▶ $body ||= q{};
Condition100%2/2
$body ||= ""
l!l
52
MC/DC100%1/1
$body ||= ""
$body
Truth table100%2/2
A
A= $body
Aresult
11
00
25 }
26  
27 # Always POST if $body is not empty!
28 37 ▶ if ( ref $callback ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $callback ne 'CODE') { } 35 2 37
29 35 ▶ if ( $body ne q{} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body ne '') { } 33 2 35
elsif ($uri =~ /\?/msux) { } 2 0 2
30 33 return $self->_process_result(
31 $self->ua->post(
32 $uri, { 'Content-Type' => $self->content_type }, $body
33 )
34 );
35 }
36 elsif ( $uri =~ /\?/xms ) {
37 2 return $self->_process_result( $self->ua->get($uri) );
38 }
39 }
40 else { # non-blocking
41 2 ▶ if ( $body ne q{} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body ne '') { } 2 0 2
elsif ($uri =~ /\?/msux) { } 0 0 0
42 $self->ua->post(
43 $uri,
44 { 'Content-Type' => $self->content_type },
45 $body,
46 2 ▶ sub { # callback
Subroutine __ANON__: called CC=1 SCAR=0.0
47 2 $callback->( $self->_process_result(pop) );
48 },
49 2 );
50 2 return;
51 }
52 elsif ( $uri =~ /\?/xms ) {
53 $self->ua->get(
54 0 ▶ $uri => sub { # callback
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 $callback->( $self->_process_result(pop) );
56 }
57 0 );
58 0 return;
59 }
60 }
61 0 return Carp::croak 'Cannot process call!';
62 }
63  
64 # Prepare a Proxy object
65 1 ▶sub prepare {
Subroutine prepare: called CC=9 SCAR=24.0
Pod documented
66 1 my $self = shift;
67  
68 1 my %m = ();
69 URI:
70 1 ▶ while ( my $uri = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uri = shift()) 2 1 3
71 2 my $methods = shift;
72  
73 # methods can be a name, a reference to a name or
74 # a reference to an array of names
75 2 ▶ if ( ref $methods eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $methods eq 'SCALAR') { } 0 2 2
elsif (defined $methods and ref $methods eq '') { } 1 1 2
76 0 $methods = [$$methods];
77 }
78 ▶ elsif ( defined $methods && ref $methods eq q{} ) {
Condition66%2/3
defined $methods and ref $methods eq ""
!ll&&!rl&&r
011
MC/DC50%1/2
defined $methods and ref $methods eq ""
defined $methods ref $methods eq ""
Truth table66%2/3
A and B
A= defined $methodsB= ref $methods eq ""
ABresult
0X0
100
111
79 1 $methods = [$methods];
80 }
81 2 ▶ if ( ref $methods ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $methods ne "ARRAY") 0 2 2
82 0 last URI;
83 }
84 METHOD:
85 2 foreach my $method ( @{$methods} ) {
86 3 ▶ if ( exists $m{$method} && $m{$method} ne $uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $m{$method} and $m{$method} ne $uri) 0 3 3
Condition33%1/3
exists $m{$method} and $m{$method} ne $uri
!ll&&!rl&&r
300
MC/DC0%0/2
exists $m{$method} and $m{$method} ne $uri
exists $m{$method} $m{$method} ne $uri
Truth table33%1/3
A and B
A= exists $m{$method}B= $m{$method} ne $uri
ABresult
0X0
100
111
87 0 Carp::croak qq{Cannot register method $method twice!};
88 }
89 3 $m{$method} = $uri;
90 }
91 }
92 1 return bless {
93 client => $self,
94 methods => \%m
95 },
96 'MojoX::JSON::RPC::Client::Proxy';
97 }
98  
99 37 ▶sub _process_result {
Subroutine _process_result: called CC=11 SCAR=27.6
100 37 my ( $self, $tx ) = @_;
101  
102 37 $self->tx($tx); # save latest transaction
103  
104 37 my $tx_res = $tx->res;
105 37 ▶ my $log = $self->ua->server->app->log if $self->ua->server->app;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ua->server->app 37 0 37
106 37 ▶ if ( $log && $log->is_level('debug') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log and $log->is_level("debug")) 0 37 37
Condition33%1/3
$log and $log->is_level("debug")
!ll&&!rl&&r
0370
MC/DC0%0/2
$log and $log->is_level("debug")
$log $log->is_level("debug")
Truth table33%1/3
A and B
A= $logB= $log->is_level("debug")
ABresult
0X0
100
111
107 0 $log->debug( 'TX BODY: [' . $tx_res->body . ']' );
108 }
109  
110 # Check if RPC call is succesfull
111 37 ▶ if ( !( $tx_res->is_success || $tx_res->is_client_error ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx_res->is_success or $tx_res->is_client_error) 0 37 37
Condition66%2/3
$tx_res->is_success or $tx_res->is_client_error
l!l&&r!l&&!r
2890
MC/DC0%0/2
$tx_res->is_success or $tx_res->is_client_error
$tx_res->is_success $tx_res->is_client_error
Truth table66%2/3
A or B
A= $tx_res->is_successB= $tx_res->is_client_error
ABresult
1X1
011
000
112 {
113 0 return;
114 }
115  
116 37 my $decode_error;
117 my $rpc_res;
118
119 37 ▶ eval{ $rpc_res = decode_json( $tx_res->body || '{}' ); 1; } or $decode_error = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $rpc_res = decode_json($tx_res->body || "{}"); 1 } } or $decode_error = $@ 37 0 37
Condition100%2/2
$tx_res->body || '{}'
l!l
343
MC/DC100%1/1
$tx_res->body || '{}'
$tx_res->body
Truth table100%2/2
A
A= $tx_res->body
Aresult
11
00
120 37 ▶ if ( $decode_error && $log ) { # Server result cannot be parsed!
Branch: 1 branch
BranchTrueFalseTotal
if ($decode_error and $log) 0 37 37
Condition33%1/3
$decode_error and $log
!ll&&!rl&&r
3700
MC/DC0%0/2
$decode_error and $log
$decode_error $log
Truth table33%1/3
A and B
A= $decode_errorB= $log
ABresult
0X0
100
111
121 0 $log->error( 'Cannot parse rpc result: ' . $decode_error );
122 0 return;
123 }
124  
125 # Return one or more ReturnObject's
126 return ref $rpc_res eq 'ARRAY'
127 ? [
128 map {
129 8 MojoX::JSON::RPC::Client::ReturnObject->new( rpc_response => $_ )
130 37 ▶ } ( @{$rpc_res} )
Branch: 1 branch
BranchTrueFalseTotal
ref $rpc_res eq 'ARRAY' ? : 2 35 37
131 ]
132 : MojoX::JSON::RPC::Client::ReturnObject->new(
133 rpc_response => $rpc_res );
134 }
135  
136 package MojoX::JSON::RPC::Client::Proxy;
137  
138 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 # no constructor defined. Object creation
143 # done by MojoX::JSON::RPC::Client.
144  
145 3 our $AUTOLOAD;
146  
147 # Dispatch calls
148 4 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=14.2
149 4 my $self = shift;
150  
151 4 my $method = $AUTOLOAD;
152 4 $method =~ s/.*:://;
153  
154 # We do not want to overload DESTROY
155 4 ▶ if ( $method eq 'DESTROY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "DESTROY") 1 3 4
156 1 return;
157 }
158  
159 3 ▶ if ( !exists $self->{methods}->{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"methods"}{$method}) 0 3 3
160 0 Carp::croak "Unsupported method $method";
161 }
162  
163 my $res = $self->{client}->call(
164 $self->{methods}->{$method},
165 3 { id => $self->{id}++,
166 method => $method,
167 params => \@_
168 }
169 );
170 3 ▶ return defined $res ? $res->result : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 3 0 3
171 }
172  
173 package MojoX::JSON::RPC::Client::ReturnObject;
174  
175 3 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 3 has rpc_response => undef; # rpc response
178  
179 40 ▶sub result {
Subroutine result: called CC=3 SCAR=11.0
180 40 my ($self) = @_;
181 40 my $rpc_response = $self->rpc_response;
182 return
183 ref $rpc_response eq 'HASH' && exists $rpc_response->{result}
184 ? $rpc_response->{result}
185 40 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $rpc_response eq 'HASH' && exists $rpc_response->{'result'} ? : 25 15 40
Condition66%2/3
ref $rpc_response eq "HASH" and exists $rpc_response->{"result"}
!ll&&!rl&&r
01525
MC/DC50%1/2
ref $rpc_response eq "HASH" and exists $rpc_response->{"result"}
ref $rpc_response eq "HASH" exists $rpc_response->{"result"}
Truth table66%2/3
A and B
A= ref $rpc_response eq "HASH"B= exists $rpc_response->{"result"}
ABresult
0X0
100
111
186 }
187  
188 26 ▶sub id {
Subroutine id: called CC=3 SCAR=12.5
189 26 my ($self) = @_;
190 26 my $rpc_response = $self->rpc_response;
191 return
192 ref $rpc_response eq 'HASH' && exists $rpc_response->{id}
193 ? $rpc_response->{id}
194 26 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $rpc_response eq 'HASH' && exists $rpc_response->{'id'} ? : 26 0 26
Condition33%1/3
ref $rpc_response eq "HASH" and exists $rpc_response->{"id"}
!ll&&!rl&&r
0026
MC/DC0%0/2
ref $rpc_response eq "HASH" and exists $rpc_response->{"id"}
ref $rpc_response eq "HASH" exists $rpc_response->{"id"}
Truth table33%1/3
A and B
A= ref $rpc_response eq "HASH"B= exists $rpc_response->{"id"}
ABresult
0X0
100
111
195 }
196  
197 94 ▶sub is_error {
Subroutine is_error: called CC=3 SCAR=11.0
198 94 my ($self) = @_;
199 94 my $rpc_response = $self->rpc_response;
200 return ref $rpc_response eq 'HASH' && exists $rpc_response->{error}
201 94 ▶ ? 1
Branch: 1 branch
BranchTrueFalseTotal
ref $rpc_response eq 'HASH' && exists $rpc_response->{'error'} ? : 71 23 94
Condition66%2/3
ref $rpc_response eq "HASH" and exists $rpc_response->{"error"}
!ll&&!rl&&r
02371
MC/DC50%1/2
ref $rpc_response eq "HASH" and exists $rpc_response->{"error"}
ref $rpc_response eq "HASH" exists $rpc_response->{"error"}
Truth table66%2/3
A and B
A= ref $rpc_response eq "HASH"B= exists $rpc_response->{"error"}
ABresult
0X0
100
111
202 : 0;
203 }
204  
205 14 ▶sub error_code {
Subroutine error_code: called CC=2 SCAR=7.2
206 14 my ($self) = @_;
207 14 ▶ return $self->is_error ? $self->rpc_response->{error}->{code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_error ? : 14 0 14
208 }
209  
210 14 ▶sub error_message {
Subroutine error_message: called CC=2 SCAR=7.2
211 14 my ($self) = @_;
212 14 ▶ return $self->is_error ? $self->rpc_response->{error}->{message} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_error ? : 14 0 14
213 }
214  
215 14 ▶sub error_data {
Subroutine error_data: called CC=2 SCAR=7.2
216 14 my ($self) = @_;
217 14 ▶ return $self->is_error ? $self->rpc_response->{error}->{data} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_error ? : 14 0 14
218 }
219  
220 3 1;
221  
222 __END__
« lib/MojoX/JSON/RPC/Dispatcher.pm ↑ Summary lib/MojoX/JSON/RPC/Service.pm »