blib/lib/HON/Http/UrlChecker/Service.pm

filter: Statementstmt 100% 65 / 65 Branchbran 100% 8 / 8 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 100% 96 / 96 CC 9 SCAR 9.3
CC
9
Cov
100%
p_retrieveInfo
5.0
checkUrl
3.0
p_parseResponse
3.0
CRAP 2.5
↑ Summary
1 package HON::Http::UrlChecker::Service;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 HON::Http::UrlChecker::Service - HTTP Status Code Checker
15  
16 =head1 VERSION
17  
18 Version 0.01
19  
20 =cut
21  
22 4 our $VERSION = '0.01';
23  
24 =head1 SYNOPSIS
25  
26 use HON::Http::UrlChecker::Service;
27  
28 my @listOfStatus = checkUrl('http://www.example.com');
29  
30 =head1 DESCRIPTION
31  
32 Check status code, response headers, redirect location and redirect chain
33 of a HTTP connection.
34  
35 =cut
36  
37 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 our @EXPORT_OK = qw/p_createUserAgent p_getUrl p_parseResponse checkUrl/;
39  
40 4 Readonly::Scalar my $TIMEOUT => 1200;
41  
42 4 Readonly::Scalar my $MAXREDIRECT => 10;
43  
44 4 Readonly::Array my @HEADERFIELDS => qw(
45 location
46 server
47 content-type
48 title
49 date
50 );
51  
52 4 Readonly::Array my @RESPONSEFIELDS => qw(
53 protocol
54 code
55 message
56 );
57  
58 =head1 SUBROUTINES/METHODS
59  
60 =head2 checkUrl( $url )
61  
62 Check a url (status code, response headers, redirect location and
63 redirect chain).
64  
65 =cut
66  
67 sub checkUrl {
68 8 ▶ my $url = shift;
Subroutine checkUrl: called CC=3 SCAR=11.0
Pod documented
69  
70 8 my $uri = URI->new($url);
71 8 ▶ if ( $uri->scheme and $uri->opaque ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme and $uri->opaque) { } 2 6 8
Condition100%3/3
$uri->scheme and $uri->opaque
!ll&&!rl&&r
512
MC/DC100%2/2
$uri->scheme and $uri->opaque
$uri->scheme $uri->opaque
Truth table100%3/3
A and B
A= $uri->schemeB= $uri->opaque
ABresult
0X0
100
111
72 2 my $ua = p_createUserAgent();
73 2 my $response = p_getUrl( $ua, $url );
74 2 my @listStatus = p_parseResponse($response);
75  
76 2 return @listStatus;
77 }
78 else {
79 6 croak "Wrong url: $url";
80 }
81 }
82  
83 =head1 PRIVATE SUBROUTINES/METHODS
84  
85 =head2 p_createUserAgent
86  
87 Return a LWP::UserAgent.
88 LWP::UserAgent objects can be used to dispatch web requests.
89  
90 =cut
91  
92 sub p_createUserAgent {
93 3 ▶ my $ua = LWP::UserAgent->new;
Subroutine p_createUserAgent: called CC=1 SCAR=0.0
Pod documented
94  
95 3 $ua->timeout($TIMEOUT);
96 3 $ua->agent('HonBot');
97 3 $ua->env_proxy;
98 3 $ua->max_redirect($MAXREDIRECT);
99  
100 3 return $ua;
101 }
102  
103 =head2 p_getUrl
104  
105 Dispatch a GET request on the given $url
106 The return value is a response object. See HTTP::Response for a description
107 of the interface it provides.
108  
109 =cut
110  
111 sub p_getUrl {
112 4 ▶ my ( $ua, $url ) = @_;
Subroutine p_getUrl: called CC=1 SCAR=0.0
Pod documented
113  
114 4 return $ua->get($url);
115 }
116  
117 =head2 p_retrieveInfo
118  
119 Retrieve desired fields from an HTTP::Response
120  
121 =cut
122  
123 sub p_retrieveInfo {
124 5 ▶ my $response = shift;
Subroutine p_retrieveInfo: called CC=5 SCAR=16.1
Pod documented
125  
126 5 my %locationStatus = ();
127 5 foreach my $field (@HEADERFIELDS) {
128 25 ▶ if ( defined $response->header($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->header($field)) 8 17 25
129 8 $locationStatus{$field} = $response->header($field);
130 }
131 }
132  
133 5 foreach my $field (@RESPONSEFIELDS) {
134 15 ▶ if ( defined $response->$field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->$field) 9 6 15
135 9 $locationStatus{$field} = $response->$field;
136 }
137 }
138  
139 5 return %locationStatus;
140 }
141  
142 =head2 p_parseResponse
143  
144 Retrieve a list of Status from HTTP::Response
145  
146 =cut
147  
148 sub p_parseResponse {
149 4 ▶ my $response = shift;
Subroutine p_parseResponse: called CC=3 SCAR=11.0
Pod documented
150  
151 4 my @listStatus = ();
152 4 my @redirects = $response->redirects;
153 4 ▶ if ( scalar @redirects > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @redirects > 0) 1 3 4
154 1 foreach my $redirect (@redirects) {
155 1 my %status = p_retrieveInfo($redirect);
156 1 push @listStatus, \%status;
157 }
158 }
159 4 my %status = p_retrieveInfo($response);
160 4 push @listStatus, \%status;
161  
162 4 return @listStatus;
163 }
164  
165 =head1 AUTHOR
166  
167 William Belle, C<< <william.belle at gmail.com> >>
168  
169 =head1 BUGS AND LIMITATIONS
170  
171 Please report any bugs or feature requests to C<bug-hon-http-urlchecker-service at rt.cpan.org>, or through
172 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HON-Http-UrlChecker-Service>. I will be notified, and then you'll
173 automatically be notified of progress on your bug as I make changes.
174  
175 =head1 SUPPORT
176  
177 You can find documentation for this module with the perldoc command.
178  
179 perldoc HON::Http::UrlChecker::Service
180  
181 You can also look for information at:
182  
183 =over 4
184  
185 =item * RT: CPAN's request tracker (report bugs here)
186  
187 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HON-Http-UrlChecker-Service>
188  
189 =item * AnnoCPAN: Annotated CPAN documentation
190  
191 L<http://annocpan.org/dist/HON-Http-UrlChecker-Service>
192  
193 =item * CPAN Ratings
194  
195 L<http://cpanratings.perl.org/d/HON-Http-UrlChecker-Service>
196  
197 =item * Search CPAN
198  
199 L<http://search.cpan.org/dist/HON-Http-UrlChecker-Service/>
200  
201 =back
202  
203 =head1 LICENSE AND COPYRIGHT
204  
205 Copyright 2016 William Belle.
206  
207 This program is distributed under the MIT (X11) License:
208 L<http://www.opensource.org/licenses/mit-license.php>
209  
210 Permission is hereby granted, free of charge, to any person
211 obtaining a copy of this software and associated documentation
212 files (the "Software"), to deal in the Software without
213 restriction, including without limitation the rights to use,
214 copy, modify, merge, publish, distribute, sublicense, and/or sell
215 copies of the Software, and to permit persons to whom the
216 Software is furnished to do so, subject to the following
217 conditions:
218  
219 The above copyright notice and this permission notice shall be
220 included in all copies or substantial portions of the Software.
221  
222 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
223 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
224 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
225 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
226 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
227 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
228 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
229 OTHER DEALINGS IN THE SOFTWARE.
230  
231 =cut
232  
233 4 1; # End of HON::Http::UrlChecker::Service
↑ Summary