Simple.pm

filter: Statementstmt 84.7% 156 / 184 Branchbran 58.1% 43 / 74 Conditioncond 50.0% 25 / 50 MC/DCmcdc 17.8% 5 / 28 Subroutinesub 86.2% 25 / 29 Podpod 88.8% 8 / 9 totaltotal 70.0% 262 / 374 CC 57 SCAR 30.7
CC
57
Cov
73%
new
52.6
post
16.9
get
16.4
CRAP 21.5
↑ Summary Simple/Parser/XML/LibXML.pm »
1 package WebService::Simple;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use base qw(LWP::UserAgent Class::Data::ConfigHash);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use HTTP::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use WebService::Simple::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 our $VERSION = '0.26';
16  
17 11 __PACKAGE__->config(
18 base_url => '',
19 response_parser => { module => "XML::Simple" },
20 );
21  
22 15 ▶sub new {
Subroutine new: called CC=25 SCAR=39.6
Pod documented
23 15 my $class = shift;
24 15 my %args = @_;
25 my $base_url = delete $args{base_url}
26 || $class->config->{base_url}
27 15 ▶ || Carp::croak("base_url is required");
Condition40%2/5
delete $args{'base_url'} || $class->config->{'base_url'} || Carp::croak('base_url is required')
l!l
150
delete $args{"base_url"} or $class->config->{"base_url"}
l!l&&r!l&&!r
1500
MC/DC0%0/2
delete $args{'base_url'} || $class->config->{'base_url'} || Carp::croak('base_url is required')
delete $args{"base_url"} $class->config->{"base_url"}
Truth table33%1/3
A or B
A= delete $args{"base_url"}B= $class->config->{"base_url"}
ABresult
1X1
011
000
28 15 $base_url = URI->new($base_url);
29 15 ▶ my $basic_params = delete $args{params} || delete $args{param} || {};
Condition100%5/5
delete $args{'params'} || delete $args{'param'} || {}
l!l
96
delete $args{"params"} or delete $args{"param"}
l!l&&r!l&&!r
546
MC/DC100%2/2
delete $args{'params'} || delete $args{'param'} || {}
delete $args{"params"} delete $args{"param"}
Truth table100%3/3
A or B
A= delete $args{"params"}B= delete $args{"param"}
ABresult
1X1
011
000
30 15 ▶ my $debug = delete $args{debug} || 0;
Condition50%1/2
delete $args{'debug'} || 0
l!l
015
MC/DC0%0/1
delete $args{'debug'} || 0
delete $args{'debug'}
Truth table50%1/2
A
A= delete $args{'debug'}
Aresult
11
00
31  
32 my $response_parser = delete $args{response_parser}
33 15 ▶ || $class->config->{response_parser};
Condition66%2/3
delete $args{'response_parser'} || $class->config->{'response_parser'}
l!l&&r!l&&!r
5100
MC/DC0%0/2
delete $args{'response_parser'} || $class->config->{'response_parser'}
delete $args{'response_parser'} $class->config->{'response_parser'}
Truth table66%2/3
A || B
A= delete $args{'response_parser'}B= $class->config->{'response_parser'}
ABresult
1X1
011
000
34 15 ▶ if ( !$response_parser
Branch: 1 branch
BranchTrueFalseTotal
unless ($response_parser and eval { do { $response_parser->isa("WebService::Simple::Parser") } }) 15 0 15
Condition33%1/3
$response_parser and eval { do { $response_parser->isa("WebService::Simple::Parser") } }
!ll&&!rl&&r
0150
MC/DC0%0/2
$response_parser and eval { do { $response_parser->isa("WebService::Simple::Parser") } }
$response_parser eval { do { $response_parser->isa("WebService::Simple::Parser") } }
Truth table33%1/3
A and B
A= $response_parserB= eval { do { $response_parser->isa("WebService::Simple::Parser") } }
ABresult
0X0
100
111
35 15 || !eval { $response_parser->isa('WebService::Simple::Parser') } )
36 {
37 15 ▶ my $config = $response_parser || $class->config->{response_parser};
Condition33%1/3
$response_parser || $class->config->{'response_parser'}
l!l&&r!l&&!r
1500
MC/DC0%0/2
$response_parser || $class->config->{'response_parser'}
$response_parser $class->config->{'response_parser'}
Truth table33%1/3
A || B
A= $response_parserB= $class->config->{'response_parser'}
ABresult
1X1
011
000
38 15 ▶ if ( !ref $config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $config) 5 10 15
39 5 $config = { module => $config };
40 }
41 15 my $module = $config->{module};
42 15 ▶ if ( $module !~ s/^\+// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($module =~ s/^\+//) 15 0 15
43 15 $module = __PACKAGE__ . "::Parser::$module";
44 }
45 15 ▶ if ( !Class::Inspector->loaded($module) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Class::Inspector"->loaded($module)) 10 5 15
46 10 ▶ $module->require or die;
Branch: 1 branch
BranchTrueFalseTotal
$module->require or die 10 0 10
47 }
48 15 ▶ $response_parser = $module->new( %{ $config->{args} || {} } );
Condition50%1/2
$config->{'args'} || {}
l!l
015
MC/DC0%0/1
$config->{'args'} || {}
$config->{'args'}
Truth table50%1/2
A
A= $config->{'args'}
Aresult
11
00
49 }
50  
51 15 my $cache = delete $args{cache};
52 15 ▶ if ( !$cache || ref $cache eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cache or ref $cache eq "HASH") 15 0 15
Condition33%1/3
not $cache or ref $cache eq "HASH"
l!l&&r!l&&!r
1500
MC/DC0%0/2
not $cache or ref $cache eq "HASH"
not $cache ref $cache eq "HASH"
Truth table33%1/3
not(A) or B
A= not $cacheB= ref $cache eq "HASH"
ABresult
1X1
011
000
53 15 ▶ my $config = ref $cache eq 'HASH' ? $cache : $class->config->{cache};
Branch: 1 branch
BranchTrueFalseTotal
ref $cache eq 'HASH' ? : 0 15 15
54 15 ▶ if ($config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) 0 15 15
55 0 ▶ if ( !ref $config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $config) 0 0 0
56 0 $config = { module => $config };
57 }
58  
59 0 my $module = $config->{module};
60 0 ▶ if ( !Class::Inspector->loaded($module) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Class::Inspector"->loaded($module)) 0 0 0
61 0 ▶ $module->require or die;
Branch: 1 branch
BranchTrueFalseTotal
$module->require or die 0 0 0
62 }
63 $cache =
64 $module->new( $config->{hashref_args}
65 ? $config->{args}
66 0 ▶ : %{ $config->{args} } );
Branch: 1 branch
BranchTrueFalseTotal
$config->{'hashref_args'} ? : 0 0 0
67 }
68 }
69  
70 15 my $self = $class->SUPER::new(%args);
71 15 $self->{base_url} = $base_url;
72 15 $self->{basic_params} = $basic_params;
73 15 $self->{response_parser} = $response_parser;
74 15 $self->{cache} = $cache;
75 15 $self->{compression} = delete $args{compression};
76 15 $self->{content_type} = delete $args{content_type};
77 15 $self->{croak} = delete $args{croak};
78 15 $self->{debug} = $debug;
79  
80 15 ▶ if($self->{content_type} && $self->{content_type} eq 'application/json'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'content_type'} and $self->{'content_type'} eq 'application/json') { } 1 14 15
Condition66%2/3
$self->{"content_type"} and $self->{"content_type"} eq "application/json"
!ll&&!rl&&r
1401
MC/DC50%1/2
$self->{"content_type"} and $self->{"content_type"} eq "application/json"
$self->{"content_type"} $self->{"content_type"} eq "application/json"
Truth table66%2/3
A and B
A= $self->{"content_type"}B= $self->{"content_type"} eq "application/json"
ABresult
0X0
100
111
81 1 $self->__init_request_parser_json();
82 }else{
83 0 ▶ $self->{request_parser} = sub { return \$_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
84 }
85  
86 15 return $self;
87 }
88  
89 15 ▶sub _agent { "libwww-perl/$LWP::VERSION+". __PACKAGE__ .'/'.$VERSION }
Subroutine _agent: called CC=1 SCAR=0.0
90  
91 37 ▶sub base_url { $_[0]->{base_url} }
Subroutine base_url: called CC=1 SCAR=0.0
Pod documented
92 37 ▶sub basic_params { $_[0]->{basic_params} }
Subroutine basic_params: called CC=1 SCAR=0.0
Pod documented
93 43 ▶sub response_parser { $_[0]->{response_parser} }
Subroutine response_parser: called CC=1 SCAR=0.0
Pod documented
94 0 ▶sub request_parser { $_[0]->{request_parser} }
Subroutine request_parser: not called CC=1 SCAR=6.9
Pod undocumented
95 34 ▶sub cache { $_[0]->{cache} }
Subroutine cache: called CC=1 SCAR=0.0
Pod documented
96  
97 17 ▶sub __cache_get {
Subroutine __cache_get: called CC=2 SCAR=8.4
98 17 my $self = shift;
99 17 my $cache = $self->cache;
100 17 ▶ return unless $cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache 17 0 17
101  
102 0 my $key = $self->__cache_key(shift);
103 0 return $cache->get( $key, @_ );
104 }
105  
106 17 ▶sub __cache_set {
Subroutine __cache_set: called CC=2 SCAR=8.4
107 17 my $self = shift;
108 17 my $cache = $self->cache;
109 17 ▶ return unless $cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache 17 0 17
110  
111 0 my $key = $self->__cache_key(shift);
112 0 return $cache->set( $key, @_ );
113 }
114  
115 0 ▶sub __cache_remove {
Subroutine __cache_remove: not called CC=2 SCAR=17.9
116 0 my $self = shift;
117 0 my $cache = $self->cache;
118 0 ▶ return unless $cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache 0 0 0
119  
120 0 my $key = $self->__cache_key(shift);
121 0 return $cache->remove( $key, @_ );
122 }
123  
124 0 ▶sub __cache_key {
Subroutine __cache_key: not called CC=1 SCAR=6.9
125 0 my $self = shift;
126 0 local $Data::Dumper::Indent = 1;
127 0 local $Data::Dumper::Terse = 1;
128 0 local $Data::Dumper::Sortkeys = 1;
129 0 return Digest::MD5::md5_hex( Data::Dumper::Dumper( $_[0] ) );
130 }
131  
132 2 ▶sub __init_request_parser_json {
Subroutine __init_request_parser_json: called CC=1 SCAR=0.0
133 2 my $self = shift;
134 2 require WebService::Simple::Parser::JSON;
135 2 my $json_parser = WebService::Simple::Parser::JSON->new();
136 2 ▶ $self->{request_parser_json} = sub { $json_parser->parse_request(@_); }
Subroutine __ANON__: called CC=1 SCAR=0.0
137 2 }
138  
139 38 ▶sub request_url {
Subroutine request_url: called CC=6 SCAR=17.9
Pod documented
140 38 my $self = shift;
141 38 my %args = @_;
142
143 38 ▶ my $uri = ref($args{url}) =~ m/^URI/ ? $args{url}->clone() : URI->new($args{url});
Branch: 1 branch
BranchTrueFalseTotal
ref($args{'url'}) =~ /^URI/ ? : 37 1 38
144 38 ▶ if ( my $extra_path = $args{extra_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_path = $args{"extra_path"}) 13 25 38
145 13 $extra_path =~ s!^/!!;
146 13 $uri->path( $uri->path . $extra_path );
147 }
148 38 ▶ if($args{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"params"}) 36 2 38
149 36 ▶ if(ref $args{params} eq 'Hash::MultiValue') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'params'} eq 'Hash::MultiValue') { } 35 1 36
150 35 for my $key (sort $args{params}->keys) {
151 43 $uri->query_param_append($key, $args{params}->get($key));
152 }
153 }else{
154 1 $uri->query_form(%{$args{params}});
155 }
156 }
157 38 return $uri;
158 }
159  
160 17 ▶sub get {
Subroutine get: called CC=11 SCAR=28.0
Pod documented
161 17 my $self = shift;
162 17 my ($url, $extra) = ("", {});
163  
164 17 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 8 9 17
165 8 $extra = shift @_;
166 }
167 else {
168 9 $url = shift @_;
169 9 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 4 5 9
170 4 $extra = shift @_;
171 }
172 }
173  
174 my $uri = $self->request_url(
175 url => $self->base_url,
176 extra_path => $url,
177 17 params => Hash::MultiValue->new(%{$self->basic_params}, %$extra),
178 );
179  
180 17 ▶ warn "Request URL is $uri\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 17 17
181  
182 17 my @headers = @_;
183 17 ▶ unless(defined($self->{compression}) && !$self->{compression}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"compression"} and not $self->{"compression"}) 17 0 17
Condition33%1/3
defined $self->{"compression"} and not $self->{"compression"}
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $self->{"compression"} and not $self->{"compression"}
defined $self->{"compression"} not $self->{"compression"}
Truth table33%1/3
A and not(B)
A= defined $self->{"compression"}B= not $self->{"compression"}
ABresult
0X0
100
111
184 17 my $can_accept = HTTP::Message::decodable();
185 17 push @headers, ('Accept-Encoding' => $can_accept) ;
186 }
187  
188 17 my $response;
189 17 $response = $self->__cache_get( [ $uri, @headers ] );
190 17 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 0 17 17
191 0 ▶ if ($response->isa('WebService::Simple::Response')) { # backward compatibility
Branch: 1 branch
BranchTrueFalseTotal
if ($response->isa('WebService::Simple::Response')) { } 0 0 0
192 0 return $response;
193 } else {
194 0 return WebService::Simple::Response->new_from_response(
195 response => $response,
196 parser => $self->response_parser
197 );
198 }
199 }
200  
201 17 $response = $self->SUPER::get( $uri, @headers );
202  
203 17 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 17 0 17
204 17 $self->__cache_set( [ $uri, @headers ], $response );
205 17 $response = WebService::Simple::Response->new_from_response(
206 response => $response,
207 parser => $self->response_parser
208 );
209 }else{
210 0 ▶ Carp::croak("request to $uri failed") unless defined($self->{croak}) && !$self->{croak};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"croak"} and not $self->{"croak"} 0 0 0
Condition0%0/3
defined $self->{"croak"} and not $self->{"croak"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"croak"} and not $self->{"croak"}
defined $self->{"croak"} not $self->{"croak"}
Truth table0%0/3
A and not(B)
A= defined $self->{"croak"}B= not $self->{"croak"}
ABresult
0X0
100
111
211 }
212  
213 17 return $response;
214 }
215  
216 20 ▶sub post {
Subroutine post: called CC=15 SCAR=28.3
Pod documented
217 20 my $self = shift;
218 20 my ( $url, $extra ) = ( '', {} );
219  
220 20 ▶ if ( ref $_[0] eq 'HASH' ) { # post(\%arg [, @header ])
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 10 10 20
elsif (ref $_[1] eq 'HASH') { } 5 5 10
elsif (@_ % 2) { } 2 3 5
221 10 $extra = shift @_;
222 }
223 elsif ( ref $_[1] eq 'HASH' ) { # post($url, \%arg [, @header ])
224 5 $url = shift @_;
225 5 $extra = shift @_;
226 }
227 elsif ( @_ % 2 ) { # post($url [, @header ])
228 2 $url = shift @_;
229 }
230  
231 20 my @headers = @_;
232 20 ▶ unless(defined($self->{compression}) && !$self->{compression}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"compression"} and not $self->{"compression"}) 20 0 20
Condition33%1/3
defined $self->{"compression"} and not $self->{"compression"}
!ll&&!rl&&r
2000
MC/DC0%0/2
defined $self->{"compression"} and not $self->{"compression"}
defined $self->{"compression"} not $self->{"compression"}
Truth table33%1/3
A and not(B)
A= defined $self->{"compression"}B= not $self->{"compression"}
ABresult
0X0
100
111
233 20 my $can_accept = HTTP::Message::decodable();
234 20 push @headers, ('Accept-Encoding' => $can_accept) ;
235 }
236 20 my %headers = @headers;
237  
238 # Content-Type tells us where "extra params" go: form-urlencoded -> $uri / json/xml -> $content
239 20 my ($uri,$response);
240
241 20 ▶ if( ($self->{content_type} && $self->{content_type} eq 'application/json') || ($headers{'Content-Type'} && $headers{'Content-Type'} eq 'application/json') ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'content_type'} and $self->{'content_type'} eq 'application/json' or $headers{'Content-Type'} and $headers{'Content-Type'} eq 'application/json') { } 2 18 20
Condition77%7/9
$self->{"content_type"} and $self->{"content_type"} eq "application/json" or $headers{"Content-Type"} and $headers{"Content-Type"} eq "application/json"
l!l&&r!l&&!r
1118
$self->{"content_type"} and $self->{"content_type"} eq "application/json"
!ll&&!rl&&r
1901
$headers{"Content-Type"} and $headers{"Content-Type"} eq "application/json"
!ll&&!rl&&r
1801
MC/DC50%2/4
$self->{"content_type"} and $self->{"content_type"} eq "application/json" or $headers{"Content-Type"} and $headers{"Content-Type"} eq "application/json"
$self->{"content_type"} $self->{"content_type"} eq "application/json" $headers{"Content-Type"} $headers{"Content-Type"} eq "application/json"
Truth table42%3/7
A and B or C and D
A= $self->{"content_type"}B= $self->{"content_type"} eq "application/json"C= $headers{"Content-Type"}D= $headers{"Content-Type"} eq "application/json"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
242 2 ▶ $self->__init_request_parser_json() unless $self->{request_parser_json};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"request_parser_json"} 1 1 2
243  
244 2 $uri = $self->request_url(
245 url => $self->base_url,
246 extra_path => $url,
247 # params => Hash::MultiValue->new(%{$self->basic_params}, %$extra), # this'll go to content
248 );
249 # $uri->query_form(undef); # we'll leave all params on the url
250  
251 2 my $req = HTTP::Request->new(POST => $uri, \@headers);
252 2 $req->content_type('application/json');
253 2 $req->content($self->{request_parser_json}->({ %{$self->basic_params}, %$extra }));
254 2 $response = $self->SUPER::request($req);
255 }else{
256 $uri = $self->request_url(
257 url => $self->base_url,
258 extra_path => $url,
259 18 params => Hash::MultiValue->new(%{$self->basic_params}, %$extra),
260 );
261 18 my $content = $uri->query_form_hash();
262 18 $uri->query_form(undef);
263  
264 18 ▶ push(@headers, 'Content-Type' => $self->{content_type}) if $self->{content_type};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content_type"} 0 18 18
265  
266 18 $response = $self->SUPER::post( $uri, $content, @headers );
267 }
268  
269 20 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 20 0 20
270 20 $response = WebService::Simple::Response->new_from_response(
271 response => $response,
272 parser => $self->response_parser
273 );
274 }else{
275 0 ▶ Carp::croak("request to $uri failed") unless defined($self->{croak}) && !$self->{croak};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"croak"} and not $self->{"croak"} 0 0 0
Condition0%0/3
defined $self->{"croak"} and not $self->{"croak"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"croak"} and not $self->{"croak"}
defined $self->{"croak"} not $self->{"croak"}
Truth table0%0/3
A and not(B)
A= defined $self->{"croak"}B= not $self->{"croak"}
ABresult
0X0
100
111
276 }
277  
278 20 return $response;
279 }
280  
281 11 1;
282  
283 __END__
↑ Summary Simple/Parser/XML/LibXML.pm »