blib/lib/URL/List.pm

filter: Statementstmt 89.6% 78 / 87 Branchbran 55.5% 10 / 18 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 17 / 17 Podpod 100% 8 / 8 totaltotal 79.0% 117 / 148 CC 17 SCAR 18.0
CC
17
Cov
79%
_build_distributions
14.7
_add
4.7
add
4.1
CRAP 6.0
↑ Summary
1 package URL::List;
2 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 URL::List - Object-oriented methods of handling list of URLs.
8  
9 =head1 VERSION
10  
11 Version 0.12
12  
13 =cut
14  
15 3 our $VERSION = '0.12';
16  
17 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Domain::PublicSuffix;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use List::MoreUtils qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 SYNOPSIS
23  
24 use URL::List;
25  
26 my $list = URL::List->new;
27 $list->add( 'http://www.google.com/' );
28 $list->add( 'http://www.bbc.co.uk/' );
29  
30 # or
31  
32 my $list = URL::List->new(
33 allow_duplicates => 1, # default false
34 urls => [ ... ], # arrayref of URLs
35 );
36  
37 my $distributed_by_hosts = $list->distributed_by_host;
38  
39 # $VAR1 = {
40 # 'www.google.com' => [
41 # 'http://www.google.com/',
42 # ],
43 # 'www.bbc.co.uk' => [
44 # 'http://www.bbc.co.uk/',
45 # ],
46 # };
47  
48 my $distributed_by_domains = $list->distributed_by_domain;
49  
50 # $VAR1 = {
51 # 'google.com' => [
52 # 'http://www.google.com/',
53 # ],
54 # 'bbc.co.uk' => [
55 # 'http://www.bbc.co.uk/',
56 # ],
57 # };
58  
59 my $distributed_by_tlds = $list->distributed_by_tld;
60  
61 # $VAR1 = {
62 # 'com' => [
63 # 'http://www.google.com/',
64 # ],
65 # 'co.uk' => [
66 # 'http://www.bbc.co.uk/',
67 # ],
68 # };
69  
70 my $urls = $list->all; # All the URLs are still there, so use this...
71 $list->clear; # ...to clear the list.
72  
73 =head1 DESCRIPTION
74  
75 URL:List is a module which helps you with distributing a list of URLs "evenly"
76 based on the URLs' host name, domain name or TLD (top-level domain).
77  
78 This can be useful for crawlers, ie. giving out a list of URLs within specific
79 hostnames, domain names and/or TLD names to different workers.
80  
81 =head1 METHODS
82  
83 =head2 new
84  
85 Returns an instance of URL::List.
86  
87 Takes one optional parameter, 'allow_duplicates', which is default 0. By setting
88 it to true (1), URL::List will not filter out duplicate articles.
89  
90 =cut
91  
92 3 has 'allow_duplicates' => ( isa => 'Bool', is => 'rw', default => 0 );
93 4 ▶has 'urls' => ( isa => 'ArrayRef[Str]', is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
94  
95 sub _add {
96 41 ▶ my $self = shift;
Subroutine _add: called CC=4 SCAR=15.5
97 41 ▶ my $url = shift // '';
Condition50%1/2
shift() // ""
l!l
410
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
98  
99 41 ▶ if ( length $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $url) { } 41 0 41
100 41 ▶ if ( my $uri = URI->new($url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uri = 'URI'->new($url)) { } 41 0 41
101 41 push( @{$self->urls}, $uri );
102 }
103 else {
104 0 carp "Can't add '" . $url . "' to the list of URLs; seems to be an invalid URL!";
105 }
106 }
107 else {
108 0 carp "Can't add an empty string as a URL!";
109 }
110 }
111  
112 =head2 add( $url )
113  
114 Add a URL to the list. C<$url> can be an array reference of URLs.
115  
116 =cut
117  
118 sub add {
119 41 ▶ my $self = shift;
Subroutine add: called CC=4 SCAR=14.1
Pod documented
120 41 my $url = shift;
121  
122 41 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 41 0 41
123 41 ▶ my $urls = ( ref $url eq 'ARRAYREF' ) ? $url : [ $url ];
Branch: 1 branch
BranchTrueFalseTotal
ref $url eq 'ARRAYREF' ? : 0 41 41
124  
125 41 foreach ( @{$urls} ) {
126 41 $self->_add( $_ );
127 }
128 }
129 }
130  
131 =head2 all
132  
133 Returns an array reference of all the URLs in the list.
134  
135 =cut
136  
137 sub all {
138 11 ▶ my $self = shift;
Subroutine all: called CC=2 SCAR=6.9
Pod documented
139  
140 11 ▶ if ( $self->allow_duplicates ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_duplicates) { } 2 9 11
141 2 return $self->urls;
142 }
143 else {
144 9 return [ List::MoreUtils::uniq(@{$self->urls}) ];
145 }
146 }
147  
148 =head2 count
149  
150 Returns the number of URLs in the list, including potential duplicates,
151 depending on the 'allow_duplicates' setting.
152  
153 =cut
154  
155 sub count {
156 7 ▶ my $self = shift;
Subroutine count: called CC=1 SCAR=0.0
Pod documented
157  
158 7 return scalar( @{$self->all} );
159 }
160  
161 =head2 clear
162  
163 Clears the URL list.
164  
165 =cut
166  
167 sub clear {
168 2 ▶ my $self = shift;
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
169  
170 2 $self->urls( [] );
171 }
172  
173 =head2 flush
174  
175 An alias for C<clear>.
176  
177 =cut
178  
179 sub flush {
180 1 ▶ return shift->clear;
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
181 }
182  
183 #
184 # DISTRIBUTIONS
185 #
186  
187 =head2 distributions
188  
189 Returns a hash reference of all the possible distributions.
190  
191 This method should not be used directly. Instead, the distributed_by_* methods
192 should be used.
193  
194 =cut
195  
196 3 has 'distributions' => ( isa => 'HashRef', is => 'ro', lazy_build => 1 );
197  
198 sub _build_distributions {
199 4 ▶ my $self = shift;
Subroutine _build_distributions: called CC=10 SCAR=26.9
200  
201 #
202 # Create a list of valid URLs
203 #
204 4 my @urls = ();
205  
206 4 foreach my $url ( @{$self->all} ) {
207 21 push( @urls, $url );
208 }
209  
210 #
211 # Build the different distributions
212 #
213 4 my %distributions = ();
214 4 my $suffix = Domain::PublicSuffix->new;
215  
216 4 foreach my $url ( @urls ) {
217 # my $host = undef;
218  
219 21 my $host = eval {
220 21 URI->new( $url )->host;
221 };
222  
223 21 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 21 21
224 0 carp "Failed to determine host from '" . $url . "'. Skipping it!";
225 0 next;
226 }
227  
228 21 ▶ if ( defined $host && length $host ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $host and length $host) { } 21 0 21
Condition33%1/3
defined $host and length $host
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table33%1/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
229 21 my $domain = $suffix->get_root_domain( $host );
230 21 my $tld = $suffix->tld;
231  
232 21 push( @{$distributions{host}->{$host}}, $url );
233  
234 21 ▶ if ( defined $domain && length $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain and length $domain) { } 21 0 21
Condition33%1/3
defined $domain and length $domain
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $domain and length $domain
defined $domain length $domain
Truth table33%1/3
A and B
A= defined $domainB= length $domain
ABresult
0X0
100
111
235 21 push( @{$distributions{domain}->{$domain}}, $url );
236 }
237 else {
238 0 carp "Failed to determine the domain name from '" . $url . "'. Skipping it!";
239 0 next;
240 }
241  
242 21 ▶ if ( defined $tld && length $tld ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tld and length $tld) { } 21 0 21
Condition33%1/3
defined $tld and length $tld
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $tld and length $tld
defined $tld length $tld
Truth table33%1/3
A and B
A= defined $tldB= length $tld
ABresult
0X0
100
111
243 21 push( @{$distributions{tld}->{$tld}}, $url );
244 }
245 else {
246 0 carp "Failed to determine the TLD from '" . $url . "'. Skipping it!";
247 0 next;
248 }
249 }
250 else {
251 0 carp "Failed to determine host from '" . $url . "'. Skipping it!";
252 }
253 }
254  
255 #
256 # Return
257 #
258 4 return \%distributions;
259 }
260  
261 =head2 distributed_by_host
262  
263 Returns a hash reference where the key is the host name, like "www.google.com",
264 and the value is an array reference to the host name's URLs.
265  
266 =cut
267  
268 sub distributed_by_host {
269 4 ▶ my $self = shift;
Subroutine distributed_by_host: called CC=1 SCAR=0.0
Pod documented
270  
271 4 return $self->distributions->{host};
272 }
273  
274 =head2 distributed_by_domain
275  
276 Returns a hash reference where the key is the domain name, like "google.com",
277 and the value is an array reference to the domain name's URLs.
278  
279 =cut
280  
281 sub distributed_by_domain {
282 4 ▶ my $self = shift;
Subroutine distributed_by_domain: called CC=1 SCAR=0.0
Pod documented
283  
284 4 return $self->distributions->{domain};
285 }
286  
287 =head2 distributed_by_tld
288  
289 Returns a hash reference where the key is the top-level domain name, like "com",
290 and the value is an array reference to the top-level domain name's URLs.
291  
292 =cut
293  
294 sub distributed_by_tld {
295 4 ▶ my $self = shift;
Subroutine distributed_by_tld: called CC=1 SCAR=0.0
Pod documented
296  
297 4 return $self->distributions->{tld};
298 }
299  
300 #
301 # The End
302 #
303 3 __PACKAGE__->meta->make_immutable;
304  
305 3 1;
306  
307 =head1 LICENSE AND COPYRIGHT
308  
309 Copyright 2012-2016 Tore Aursand.
310  
311 This program is free software; you can redistribute it and/or modify it
312 under the terms of the the Artistic License (2.0). You may obtain a
313 copy of the full license at:
314  
315 L<http://www.perlfoundation.org/artistic_license_2_0>
316  
317 Any use, modification, and distribution of the Standard or Modified
318 Versions is governed by this Artistic License. By using, modifying or
319 distributing the Package, you accept this license. Do not use, modify,
320 or distribute the Package, if you do not accept this license.
321  
322 If your Modified Version has been derived from a Modified Version made
323 by someone other than you, you are nevertheless required to ensure that
324 your Modified Version complies with the requirements of this license.
325  
326 This license does not grant you the right to use any trademark, service
327 mark, tradename, or logo of the Copyright Holder.
328  
329 This license includes the non-exclusive, worldwide, free-of-charge
330 patent license to make, have made, use, offer to sell, sell, import and
331 otherwise transfer the Package with respect to any patent claims
332 licensable by the Copyright Holder that are necessarily infringed by the
333 Package. If you institute patent litigation (including a cross-claim or
334 counterclaim) against any party alleging that the Package constitutes
335 direct or contributory patent infringement, then this Artistic License
336 to you shall terminate on the date that such litigation is filed.
337  
338 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
339 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
340 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
341 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
342 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
343 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
344 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
345 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
↑ Summary