blib/lib/HTTP/Thin/UserAgent.pm

filter: Statementstmt 53.4% 78 / 146 Branchbran 0.0% 0 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 45.6% 21 / 46 Podpod 22.2% 2 / 9 totaltotal 47.2% 102 / 216 CC 12 SCAR 12.7
CC
12
Cov
49%
_build_response
12.0
find
12.0
as_json
6.0
CRAP 3.5
↑ Summary
1 package HTTP::Thin::UserAgent;
2 4 $HTTP::Thin::UserAgent::VERSION = '0.016';
3 4 ▶use 5.12.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: A Thin UserAgent around some useful modules.
7  
8  
9 {
10 package
11 HTTP::Thin::UserAgent::HTTPExceptionWithResponse;
12 4 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 has response => ( is => 'ro' );
14 }
15  
16 {
17 4 package
18 HTTP::Thin::UserAgent::Error::UnexpectedResponse;
19  
20 4 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 extends qw(Throwable::Error);
22 4 with qw(HTTP::Thin::UserAgent::HTTPExceptionWithResponse);
23  
24 }
25  
26 {
27 4 package
28 HTTP::Thin::UserAgent::HTTP::Throwable::Factory;
29 4 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 4 extends qw(HTTP::Throwable::Factory);
32  
33 0 ▶ sub extra_roles {
Subroutine extra_roles: not called CC=1 SCAR=6.9
Pod documented
34 0 return qw(
35 HTTP::Throwable::Role::TextBody
36 HTTP::Thin::UserAgent::HTTPExceptionWithResponse
37 );
38 }
39 }
40  
41 {
42  
43 4 package
44 HTTP::Thin::UserAgent::Client;
45  
46 4 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶ use MooX::late;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶ use MooX::ChainedAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶ use HTTP::Thin;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 ▶ use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 ▶ use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 4 ▶ use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 4 ▶ use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 4 ▶ use constant TRACE => $ENV{TRACE} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"TRACE"} // 0
l!l
04
MC/DC0%0/1
$ENV{"TRACE"} // 0
$ENV{"TRACE"}
Truth table50%1/2
A
A= $ENV{"TRACE"}
Aresult
11
00
56 4 ▶ use constant UnexpectedResponse => 'HTTP::Thin::UserAgent::Error::UnexpectedResponse';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶ use constant HTTPException => 'HTTP::Thin::UserAgent::HTTP::Throwable::Factory';
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 has ua => (
60 is => 'ro',
61 0 ▶ default => sub { HTTP::Thin->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 4 );
63  
64 4 has request => (
65 is => 'ro',
66 required => 1,
67 );
68  
69 has on_error => (
70 is => 'rw',
71 0 ▶ default => sub { sub { confess "$_" } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 4 chained => 1,
73 );
74  
75 has decoder => (
76 is => 'rw',
77 chained => 1,
78 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 ▶ sub { shift->decoded_content }
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 },
81 4 );
82  
83 0 ▶ sub decoded_content {
Subroutine decoded_content: not called CC=1 SCAR=6.9
Pod undocumented
84 0 my $self = shift;
85 0 return $self->decoder->( $self->response );
86 }
87  
88 0 ▶ sub decode { warn 'decode is deprecated, please call decoded_content instead'; shift->decoded_content }
Subroutine decode: not called CC=1 SCAR=6.9
Pod undocumented
89  
90 4 has response => (
91 is => 'ro',
92 lazy => 1,
93 builder => '_build_response',
94 handles => { 'content' => 'decoded_content' },
95 );
96  
97 0 ▶ sub _build_response {
Subroutine _build_response: not called CC=3 SCAR=24.8
98 0 my $self = shift;
99 0 my $ua = $self->ua;
100 0 my $request = $self->request;
101  
102 0 warn $request->dump if TRACE;
103 0 my $res = $ua->request($request);
104 0 warn $res->dump if TRACE;
105  
106 0 ▶ if ( $res->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
107 0 my $e;
108 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 $e = HTTPException->new_exception(
110 $res->code => {
111 additional_headers => [ $res->headers->flatten() ],
112 response => $res,
113 }
114 );
115 }
116 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 $e = HTTPException->new_exception(
118 {
119 status_code => $res->code,
120 reason => $res->message,
121 additional_headers => [ $res->headers->flatten(), ],
122 response => $res,
123 }
124 );
125 0 };
126 0 for ($e) { $self->on_error->($e) }
127 }
128  
129 0 return $res;
130 }
131  
132 0 ▶ sub as_json {
Subroutine as_json: not called CC=2 SCAR=17.9
Pod undocumented
133 0 my $self = shift;
134  
135 0 my $request = $self->request;
136  
137 0 $request->header(
138 'Accept' => 'application/json',
139 'Content-Type' => 'application/json',
140 );
141  
142 0 ▶ if ( my $data = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = shift()) 0 0 0
143 0 $request->content( JSON::Any->encode($data) );
144 }
145  
146 0 weaken($self);
147 $self->decoder(
148 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 0 my $res = shift;
150 0 my $content_type = $res->header('Content-Type');
151 0 ▶ my $data = try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
152 0 ▶ die "Content-Type was $content_type not application/json"
Branch: 1 branch
BranchTrueFalseTotal
unless $content_type =~ m[application/json]u 0 0 0
153 unless $content_type =~ m'application/json';
154 0 JSON::Any->decode( $res->decoded_content );
155 }
156 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
157 0 my $error = UnexpectedResponse->new(
158 message => $_,
159 response => $res,
160 );
161 0 for ($error) {
162 0 $self->on_error->($error);
163 }
164 0 };
165 }
166 0 );
167 0 return $self;
168 }
169  
170 0 ▶ sub dump { require Data::Dumper; return Data::Dumper::Dumper(shift) }
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
171  
172 0 ▶ sub scraper {
Subroutine scraper: not called CC=1 SCAR=6.9
Pod undocumented
173 0 my ( $self, $scraper ) = @_;
174  
175 0 weaken($self);
176 $self->decoder(
177 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 my $res = shift;
179 0 ▶ my $data = try { $scraper->scrape( $res->decoded_content ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
181 0 my $error = UnexpectedResponse->new(
182 message => $_,
183 response => $res
184 );
185 0 for ($error) { $self->on_error->($error); }
186 0 };
187 0 return $data;
188 }
189 0 );
190 0 return $self;
191 }
192  
193 0 ▶ sub tree {
Subroutine tree: not called CC=2 SCAR=17.9
Pod undocumented
194 0 my ($self) = @_;
195 0 my $t = HTML::TreeBuilder::XPath->new;
196 0 ▶ $t->store_comments(1) if ( $t->can('store_comments') );
Branch: 1 branch
BranchTrueFalseTotal
if $t->can("store_comments") 0 0 0
197 0 $t->ignore_unknown(0);
198 0 $t->parse( $self->content );
199 0 return $t;
200 }
201  
202 0 ▶ sub find {
Subroutine find: not called CC=3 SCAR=24.8
Pod undocumented
203 0 my ( $self, $exp ) = @_;
204  
205 0 ▶ my $xpath =
Branch: 1 branch
BranchTrueFalseTotal
$exp =~ m[^(?:/|id\()]u ? : 0 0 0
206 $exp =~ m!^(?:/|id\()!
207 ? $exp
208 : HTML::Selector::XPath::selector_to_xpath($exp);
209  
210 0 ▶ my @nodes = try { $self->tree->findnodes($xpath) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
212 0 for ($_) { $self->on_error($_) }
213 0 };
214 0 ▶ return unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 0 0
215 0 return \@nodes;
216 }
217  
218 }
219  
220 4 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
221 4 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
222 4 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 4 ▶use Web::Scraper;
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 4 our @EXPORT = qw(http);
226  
227 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
228 3 shift->export_to_level(1);
229 3 HTTP::Request::Common->import::into( scalar caller );
230 3 Web::Scraper->import::into( scalar caller );
231 }
232  
233 0 ▶sub http { HTTP::Thin::UserAgent::Client->new( request => shift ) }
Subroutine http: not called CC=1 SCAR=6.9
Pod documented
234  
235 4 1;
236  
237 __END__
↑ Summary