blib/lib/WWW/JSON.pm

filter: Statementstmt 87.9% 102 / 116 Branchbran 77.7% 28 / 36 Conditioncond 57.8% 11 / 19 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 81.8% 27 / 33 Podpod 85.7% 6 / 7 totaltotal 79.3% 177 / 223 SCAR 34.3
CC
27
Cov
82%
req
8.1
__ANON__
5.2
_determine_query_params
5.1
CRAP 30.9
↑ Summary blib/lib/WWW/JSON/Role/Authentication.pm »
1 package WWW::JSON;
2 8 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = "1.00";
7 8 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use WWW::JSON::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 has ua => (
17 is => 'lazy',
18 handles => [qw/timeout default_header/],
19 0 ▶ default => sub { LWP::UserAgent->new( %{ $_[0]->ua_options } ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 8 );
21 has base_url => (
22 is => 'rw',
23 coerce => sub {
24 7 ▶ my $base_url = shift;
Subroutine __ANON__: called CC=4 SCAR=16.5
25 7 my $u;
26 7 ▶ if ( ref($base_url) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $base_url eq 'ARRAY') { } 0 7 7
27 0 my ( $url, $params ) = @{$base_url};
28 0 $u = URI->new($url);
29 0 $u->query_form(%$params);
30 }
31 else {
32 7 $u = URI->new($base_url);
33 }
34 7 ▶ if ( my $path = $u->path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $u->path) 1 6 7
35 1 ▶ unless ( $path =~ m|/$| ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[/$]) 0 1 1
36 0 $path =~ s|$|/|;
37 0 $u->path($path);
38 }
39 }
40 7 return $u;
41 }
42 8 );
43 7 ▶has body_params => ( is => 'rw', default => sub { +{} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
44 7 ▶has query_params => ( is => 'rw', default => sub { +{} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
45 has post_body_format => (
46 is => 'rw',
47 7 ▶ default => sub { 'serialized' },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 clearer => 1,
49 isa => sub {
50 8 ▶ die "Invalid post_body_format $_[0]"
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] eq "serialized" or $_[0] eq "JSON" 0 8 8
Condition66%2/3
$_[0] eq "serialized" or $_[0] eq "JSON"
l!l&&r!l&&!r
710
MC/DC0%0/2
$_[0] eq "serialized" or $_[0] eq "JSON"
$_[0] eq "serialized" $_[0] eq "JSON"
Truth table66%2/3
A or B
A= $_[0] eq "serialized"B= $_[0] eq "JSON"
ABresult
1X1
011
000
51 unless ( $_[0] eq 'serialized' || $_[0] eq 'JSON' );
52 }
53 8 );
54 has json =>
55 7 ▶ ( is => 'ro', default => sub { JSON::XS->new->utf8->allow_nonref } );
Subroutine __ANON__: called CC=1 SCAR=0.0
56  
57 8 has content_type => ( is => 'rw', clearer => 1 );
58  
59 has default_response_transform => (
60 is => 'rw',
61 clearer => 1,
62 isa => sub {
63 1 ▶ die "default_response_transform takes a coderef"
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "CODE" 0 1 1
64 unless ref( $_[0] ) eq 'CODE';
65 }
66 8 );
67  
68 0 ▶has ua_options => ( is => 'lazy', default => sub { +{} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
69  
70 8 with 'WWW::JSON::Role::Authentication';
71 8 my %METHOD_DISPATCH = (
72 GET => \&HTTP::Request::Common::GET,
73 POST => \&HTTP::Request::Common::POST,
74 PUT => \&HTTP::Request::Common::PUT,
75 DELETE => \&HTTP::Request::Common::DELETE,
76 HEAD => \&HTTP::Request::Common::HEAD
77 );
78  
79 7 ▶sub get { shift->req( 'GET', @_ ) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
80 10 ▶sub post { shift->req( 'POST', @_ ) }
Subroutine post: called CC=1 SCAR=0.0
Pod documented
81 0 ▶sub put { shift->req( 'PUT', @_ ) }
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
82 0 ▶sub delete { shift->req( 'DELETE', @_ ) }
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
83 0 ▶sub head { shift->req( 'HEAD', @_ ) }
Subroutine head: not called CC=1 SCAR=6.9
Pod undocumented
84  
85  
86 sub _http_method_uses_post_body {
87 44 ▶ my ($self,$method) = @_;
Subroutine _http_method_uses_post_body: called CC=2 SCAR=7.1
88 44 ▶ return ($method eq 'POST' || $method eq 'PUT');
Condition66%2/3
$method eq 'POST' || $method eq 'PUT'
l!l&&r!l&&!r
30014
MC/DC50%1/2
$method eq 'POST' || $method eq 'PUT'
$method eq 'POST' $method eq 'PUT'
Truth table66%2/3
A || B
A= $method eq 'POST'B= $method eq 'PUT'
ABresult
1X1
011
000
89 }
90  
91 sub req {
92 17 ▶ my ( $self, $method, $path, $params, $opts ) = @_;
Subroutine req: called CC=8 SCAR=20.9
Pod documented
93 17 ▶ $params = {} unless defined($params);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params 6 11 17
94 17 ▶ $opts = {} unless defined($opts);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 16 1 17
95 17 my $body_params;
96 17 ▶ $body_params = { %{ $self->body_params }, %{$params} }
Branch: 1 branch
BranchTrueFalseTotal
if $self->_http_method_uses_post_body($method) 10 7 17
97 if $self->_http_method_uses_post_body($method);
98 17 ▶ ( $path, $params ) = $self->_do_templating( $path, $params )
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\[\%.*\%\]/ 3 14 17
99 if ( $path =~ /\[\%.*\%\]/ );
100 17 ▶ unless ( $path->$_isa('URI') && $path->scheme ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path->$WWW::JSON::_isa("URI") and $path->scheme) 17 0 17
Condition33%1/3
$path->$WWW::JSON::_isa("URI") and $path->scheme
!ll&&!rl&&r
1700
MC/DC0%0/2
$path->$WWW::JSON::_isa("URI") and $path->scheme
$path->$WWW::JSON::_isa("URI") $path->scheme
Truth table33%1/3
A and B
A= $path->$WWW::JSON::_isa("URI")B= $path->scheme
ABresult
0X0
100
111
101 17 $path =~ s|^/|./|;
102 17 $path = URI->new($path);
103 }
104  
105 17 ▶ my $abs_uri =
Branch: 1 branch
BranchTrueFalseTotal
$path->scheme ? : 1 16 17
106 ( $path->scheme ) ? $path : URI->new_abs( $path, $self->base_url );
107  
108 $abs_uri->query_form(
109 $self->_determine_query_params( $method, $path, $params,
110 $opts->{query_params} )
111 17 );
112  
113 17 my $request_obj = $self->_create_request_obj( $method, $abs_uri, $body_params );
114  
115 17 return $self->http_request( $request_obj);
116 }
117  
118 sub _determine_query_params {
119 17 ▶ my ( $self, $method, $path, $params, $opt_params ) = @_;
Subroutine _determine_query_params: called CC=5 SCAR=16.3
120 my %query_params = (
121 $self->base_url->query_form,
122 17 ▶ ($self->query_params) ? %{$self->query_params} : (),
Branch: 3 branches
BranchTrueFalseTotal
$self->query_params ? : 17 0 17
$params && !$self->_http_method_uses_post_body($method) ? : 7 10 17
$opt_params ? : 1 16 17
Condition66%2/3
$params and not $self->_http_method_uses_post_body($method)
!ll&&!rl&&r
0107
MC/DC50%1/2
$params and not $self->_http_method_uses_post_body($method)
$params not $self->_http_method_uses_post_body($method)
Truth table66%2/3
A and not(B)
A= $paramsB= not $self->_http_method_uses_post_body($method)
ABresult
0X0
100
111
123 $path->query_form,
124 ( $params && ! $self->_http_method_uses_post_body($method) ) ? %$params : (),
125 ($opt_params) ? (%$opt_params) : ()
126 );
127 17 return %query_params;
128 }
129  
130 sub _do_templating {
131 3 ▶ my ( $self, $path, $params ) = @_;
Subroutine _do_templating: called CC=3 SCAR=11.0
132 3 my %modified_params = %$params;
133 3 for my $key ( grep { $_ =~ /^-/ } keys(%$params) ) {
134 4 (my $search_key = $key) =~ s/^-//;
135 4 ▶ delete $modified_params{$key}
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ s/\[\%\s*$search_key\s*\%\]/$params->{$key}/g 4 0 4
136 if ( $path =~ s/\[\%\s*$search_key\s*\%\]/$params->{$key}/g );
137 }
138 3 return ( $path, \%modified_params );
139 }
140  
141 sub body_param {
142 0 ▶ my ( $self, $k, $v ) = @_;
Subroutine body_param: not called CC=1 SCAR=6.9
Pod documented
143 0 $self->body_param->{$k} = $v;
144 }
145  
146 sub _create_post_body {
147 10 ▶ my ( $self, $p ) = @_;
Subroutine _create_post_body: called CC=4 SCAR=14.2
148 10 ▶ if ( $self->post_body_format eq 'JSON' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->post_body_format eq "JSON") 1 9 10
149 return (
150 1 ▶ 'Content-Type' => $self->content_type || 'application/json',
Condition50%1/2
$self->content_type || 'application/json'
l!l
01
MC/DC0%0/1
$self->content_type || 'application/json'
$self->content_type
Truth table50%1/2
A
A= $self->content_type
Aresult
11
00
151 Content => $self->json->encode($p)
152 );
153 }
154 return (
155 9 ▶ Content => $self->_encode_content_body($p),
Condition50%1/2
$self->content_type || 'application/x-www-form-urlencoded'
l!l
09
MC/DC0%0/1
$self->content_type || 'application/x-www-form-urlencoded'
$self->content_type
Truth table50%1/2
A
A= $self->content_type
Aresult
11
00
156 'Content-Type' => $self->content_type || 'application/x-www-form-urlencoded'
157 );
158 }
159  
160 sub _encode_content_body {
161 9 ▶ my ($self,$p) = @_;
Subroutine _encode_content_body: called CC=1 SCAR=0.0
162 9 my $u = URI->new;
163 9 $u->query_form(%$p);
164 9 return $u->query;
165 }
166  
167 sub _create_request_obj {
168 17 ▶ my ( $self, $method, $uri, $p ) = @_;
Subroutine _create_request_obj: called CC=4 SCAR=14.0
169 17 ▶ my $dispatch = $METHOD_DISPATCH{$method}
Branch: 1 branch
BranchTrueFalseTotal
my $dispatch = $METHOD_DISPATCH{$method} or die "Method $method not implemented" 0 17 17
170 or die "Method $method not implemented";
171  
172 17 my %payload;
173  
174 17 ▶ if ( $p && $self->_http_method_uses_post_body($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p and $self->_http_method_uses_post_body($method)) 10 7 17
Condition66%2/3
$p and $self->_http_method_uses_post_body($method)
!ll&&!rl&&r
7010
MC/DC50%1/2
$p and $self->_http_method_uses_post_body($method)
$p $self->_http_method_uses_post_body($method)
Truth table66%2/3
A and B
A= $pB= $self->_http_method_uses_post_body($method)
ABresult
0X0
100
111
175 10 %payload = $self->_create_post_body($p);
176 }
177 17 return $dispatch->( $uri->as_string, %payload );
178 }
179  
180 sub http_request {
181 my ( $self, $request_obj ) = @_;
182 my $resp = $self->ua->request($request_obj);
183  
184 return WWW::JSON::Response->new(
185 {
186 http_response => $resp,
187 _response_transform => $self->default_response_transform,
188 json => $self->json,
189 request_object => $request_obj,
190 }
191 );
192 }
193  
194 8 1;
195 __END__
↑ Summary blib/lib/WWW/JSON/Role/Authentication.pm »