RPC/Legacy/Client.pm

filter: Statementstmt 20.1% 22 / 109 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 21.2% 7 / 33 Podpod 75.0% 9 / 12 totaltotal 15.7% 38 / 242 CC 41 SCAR 30.4
CC
41
Cov
12%
_post
72.0
AUTOLOAD
56.0
call
42.0
CRAP 20.9
« RPC/Legacy/Procedure.pm ↑ Summary RPC/Legacy/Server/Apache2.pm »
1 ##############################################################################
2 # JSONRPC version 1.1
3 # http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
4 ##############################################################################
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 ##############################################################################
11  
12 package JSON::RPC::Legacy::Client;
13  
14 1 $JSON::RPC::Legacy::Client::VERSION = '1.06';
15  
16 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=16.7
20 1 for my $method (qw/uri ua json content_type version id allow_call status_line/) {
21 0 ▶ eval qq|
Subroutine allow_call: not called CC=2 SCAR=17.3
Subroutine content_type: not called CC=2 SCAR=17.3
Subroutine id: not called CC=2 SCAR=17.3
Subroutine json: not called CC=2 SCAR=17.3
Subroutine status_line: not called CC=2 SCAR=17.3
Subroutine ua: not called CC=2 SCAR=17.3
Subroutine uri: not called CC=2 SCAR=17.3
Subroutine version: not called CC=2 SCAR=17.3
Pod undocumented
Pod undocumented
Pod documented
Pod documented
Pod documented
Pod documented
Pod undocumented
Pod documented
Branch: 8 branches
BranchTrueFalseTotal
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
22 sub $method {
23 \$_[0]->{$method} = \$_[1] if defined \$_[1];
24 \$_[0]->{$method};
25 }
26 |;
27 }
28 }
29  
30  
31  
32 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=7 SCAR=40.3
33 0 my $self = shift;
34 0 my $method = $JSON::RPC::Legacy::Client::AUTOLOAD;
35  
36 0 $method =~ s/.*:://;
37  
38 0 ▶ return if ($method eq 'DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 0 0
39  
40 0 $method =~ s/^__(\w+)__$/$1/; # avoid to call built-in methods (ex. __VERSION__ => VERSION)
41  
42 0 ▶ unless ( exists $self->allow_call->{ $method } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->allow_call->{$method}) 0 0 0
43 0 Carp::croak("Can't call the method not allowed by prepare().");
44 }
45  
46 0 my @params = @_;
47 0 ▶ my $obj = {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
48 method => $method,
49 params => (ref $_[0] ? $_[0] : [@_]),
50 };
51  
52 0 my $ret = $self->call($self->uri, $obj);
53  
54 0 ▶ if ( $ret and $ret->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret and $ret->is_success) { } 0 0 0
Condition0%0/3
$ret and $ret->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$ret and $ret->is_success
$ret $ret->is_success
Truth table0%0/3
A and B
A= $retB= $ret->is_success
ABresult
0X0
100
111
55 0 return $ret->result;
56 }
57 else {
58 0 ▶ Carp::croak ( $ret ? '(Procedure error) ' . $ret->error_message : $self->status_line );
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 0 0 0
59 }
60  
61 }
62  
63  
64 0 ▶sub create_json_coder {
Subroutine create_json_coder: not called CC=1 SCAR=6.9
Pod documented
65 0 JSON->new->allow_nonref->utf8;
66 }
67  
68  
69 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
70 0 my $proto = shift;
71 0 ▶ my $self = bless {}, (ref $proto ? ref $proto : $proto);
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 0 0 0
72  
73 0 my $ua = LWP::UserAgent->new(
74 agent => 'JSON::RPC::Legacy::Client/' . $JSON::RPC::Legacy::Client::VERSION . ' beta ',
75 timeout => 10,
76 );
77  
78 0 $self->ua($ua);
79 0 $self->json( $proto->create_json_coder );
80 0 $self->version('1.1');
81 0 $self->content_type('application/json');
82  
83 0 return $self;
84 }
85  
86  
87 0 ▶sub prepare {
Subroutine prepare: not called CC=1 SCAR=6.9
Pod documented
88 0 my ($self, $uri, $procedures) = @_;
89 0 $self->uri($uri);
90 0 $self->allow_call({ map { ($_ => 1) } @$procedures });
91 }
92  
93  
94 0 ▶sub call {
Subroutine call: not called CC=6 SCAR=37.4
Pod documented
95 0 my ($self, $uri, $obj) = @_;
96 0 my $result;
97  
98 0 ▶ if ($uri =~ /\?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /\?/) { } 0 0 0
99 0 $result = $self->_get($uri);
100 }
101 else {
102 0 ▶ Carp::croak "not hashref." unless (ref $obj eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj eq "HASH" 0 0 0
103 0 $result = $self->_post($uri, $obj);
104 }
105  
106 0 ▶ my $service = $obj->{method} =~ /^system\./ if ( $obj );
Branch: 1 branch
BranchTrueFalseTotal
if $obj 0 0 0
107  
108 0 $self->status_line($result->status_line);
109  
110 0 ▶ return unless($result->content); # notification?
Branch: 1 branch
BranchTrueFalseTotal
unless $result->content 0 0 0
111  
112 0 ▶ if ($service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service) 0 0 0
113 0 return JSON::RPC::Legacy::ServiceObject->new($result, $self->json);
114 }
115  
116 0 return JSON::RPC::Legacy::ReturnObject->new($result, $self->json);
117 }
118  
119  
120 0 ▶sub _post {
Subroutine _post: not called CC=8 SCAR=42.8
121 0 my ($self, $uri, $obj) = @_;
122 0 my $json = $self->json;
123  
124 0 ▶ $obj->{version} ||= $self->{version} || '1.1';
Condition0%0/5
$obj->{"version"} ||= $self->{"version"} || "1.1"
l!l&&r!l&&!r
000
$self->{'version'} || '1.1'
l!l
00
MC/DC0%0/2
$obj->{"version"} ||= $self->{"version"} || "1.1"
$obj->{"version"} $self->{'version'}
Truth table0%0/3
A ||= B
A= $obj->{"version"}B= $self->{'version'}
ABresult
1X1
011
000
125  
126 0 ▶ if ($obj->{version} eq '1.0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{'version'} eq '1.0') { } 0 0 0
127 0 delete $obj->{version};
128 0 ▶ if (exists $obj->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{'id'}) { } 0 0 0
129 0 ▶ $self->id($obj->{id}) if ($obj->{id}); # if undef, it is notification.
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"id"} 0 0 0
130 }
131 else {
132 0 ▶ $obj->{id} = $self->id || ($self->id('JSON::RPC::Legacy::Client'));
Condition0%0/3
$self->id || $self->id('JSON::RPC::Legacy::Client')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->id || $self->id('JSON::RPC::Legacy::Client')
$self->id $self->id('JSON::RPC::Legacy::Client')
Truth table0%0/3
A || B
A= $self->idB= $self->id('JSON::RPC::Legacy::Client')
ABresult
1X1
011
000
133 }
134 }
135 else {
136 0 ▶ $obj->{id} = $self->id if (defined $self->id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->id 0 0 0
137 }
138  
139 0 my $content = $json->encode($obj);
140  
141 $self->ua->post(
142 $uri,
143 Content_Type => $self->{content_type},
144 0 Content => $content,
145 Accept => 'application/json',
146 );
147 }
148  
149  
150 0 ▶sub _get {
Subroutine _get: not called CC=1 SCAR=6.9
151 0 my ($self, $uri) = @_;
152 0 $self->ua->get(
153 $uri,
154 Accept => 'application/json',
155 );
156 }
157  
158  
159  
160 ##############################################################################
161  
162 package JSON::RPC::Legacy::ReturnObject;
163  
164 1 $JSON::RPC::Legacy::ReturnObject::VERSION = $JSON::RPC::Legacy::VERSION;
165  
166 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=15.7
167 1 for my $method (qw/is_success content jsontext version/) {
168 0 ▶ eval qq|
Subroutine content: not called CC=2 SCAR=16.7
Subroutine is_success: not called CC=2 SCAR=16.7
Subroutine jsontext: not called CC=2 SCAR=16.7
Subroutine version: not called CC=2 SCAR=16.7
Branch: 4 branches
BranchTrueFalseTotal
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
169 sub $method {
170 \$_[0]->{$method} = \$_[1] if defined \$_[1];
171 \$_[0]->{$method};
172 }
173 |;
174 }
175 }
176  
177  
178 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
179 0 my ($class, $obj, $json) = @_;
180 0 ▶ my $content = ( $json || JSON->new->utf8 )->decode( $obj->content );
Condition0%0/3
$json || 'JSON'->new->utf8
l!l&&r!l&&!r
000
MC/DC0%0/2
$json || 'JSON'->new->utf8
$json 'JSON'->new->utf8
Truth table0%0/3
A || B
A= $jsonB= 'JSON'->new->utf8
ABresult
1X1
011
000
181  
182 0 my $self = bless {
183 jsontext => $obj->content,
184 content => $content,
185 }, $class;
186  
187 0 ▶ $content->{error} ? $self->is_success(0) : $self->is_success(1);
Branch: 1 branch
BranchTrueFalseTotal
$content->{'error'} ? : 0 0 0
188  
189 0 ▶ $content->{version} ? $self->version(1.1) : $self->version(0) ;
Branch: 1 branch
BranchTrueFalseTotal
$content->{'version'} ? : 0 0 0
190  
191 0 $self;
192 }
193  
194  
195 0 ▶sub is_error { !$_[0]->is_success; }
Subroutine is_error: not called CC=1 SCAR=6.9
196  
197 0 ▶sub error_message {
Subroutine error_message: not called CC=2 SCAR=17.9
198 0 ▶ $_[0]->version ? $_[0]->{content}->{error}->{message} : $_[0]->{content}->{error};
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->version ? : 0 0 0
199 }
200  
201  
202 0 ▶sub result {
Subroutine result: not called CC=1 SCAR=6.9
203 0 $_[0]->{content}->{result};
204 }
205  
206  
207 ##############################################################################
208  
209 package JSON::RPC::Legacy::ServiceObject;
210  
211 1 ▶use base qw(JSON::RPC::Legacy::ReturnObject);
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213  
214 0 ▶sub sdversion {
Subroutine sdversion: not called CC=2 SCAR=17.9
215 0 ▶ $_[0]->{content}->{sdversion} || '';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"content"}{"sdversion"} or "" 0 0 0
216 }
217  
218  
219 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
220 0 ▶ $_[0]->{content}->{name} || '';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"content"}{"name"} or "" 0 0 0
221 }
222  
223  
224 0 ▶sub result {
Subroutine result: not called CC=2 SCAR=17.9
225 0 ▶ $_[0]->{content}->{summary} || '';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"content"}{"summary"} or "" 0 0 0
226 }
227  
228  
229  
230 1 1;
231 __END__
« RPC/Legacy/Procedure.pm ↑ Summary RPC/Legacy/Server/Apache2.pm »