lib/Net/Domain/Regex.pm

filter: Statementstmt 34.0% 30 / 88 Branchbran 13.6% 3 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.0% 7 / 10 Podpod 60.0% 3 / 5 totaltotal 33.0% 43 / 130 CC 17 SCAR 31.9
CC
17
Cov
29%
match
56.0
refresh
30.5
generate_regex
12.0
CRAP 24.2
↑ Summary
1 package Net::Domain::Regex;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use version; our $VERSION = qv('0.2.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $LOCAL = '/tmp/effective_tld_names.dat';
8 1 our $CACHE = '/tmp/effective_tld_names.dat.cache';
9 1 our $SOURCE = 'http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1';
10  
11 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶sub import {
Subroutine import: called CC=2 SCAR=8.1
14 1 ▶ if( grep { /:pdata/ } @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/:pdata/;} @_) 0 1 1
15 0 $SOURCE = 'https://raw.github.com/petermblair/Perl-CPAN/master/Net-Domain-Regex/misc/tld.txt';
16 }
17 }
18  
19 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
20 1 my $class = shift;
21  
22 1 my $args = {
23 local => $LOCAL,
24 source => $SOURCE,
25 cache => $CACHE,
26 @_,
27 };
28  
29 1 my $o = bless $args => $class;
30  
31 1 ▶ unless( -e $o->{local} ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $o->{"local"}) 1 0 1
32 1 $o->pull;
33 }
34  
35 0 $o->refresh;
36  
37 0 return $o;
38 }
39  
40 0 ▶sub refresh {
Subroutine refresh: not called CC=6 SCAR=34.2
Pod documented
41 0 my $self = shift;
42  
43 1 ▶ use open qw(:std :utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 0 open FD, "<$self->{local}";
45  
46 0 my $tlds = {};
47 0 my $slds = {};
48  
49 0 ▶ while( <FD> ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FD)) 0 0 0
50 0 chomp;
51  
52 0 ▶ if(/^(\S[^\.\s]+)$/){
Branch: 2 branches
BranchTrueFalseTotal
if (/^(\S[^\.\s]+)$/) { } 0 0 0
elsif (/^\S[^\.\s]+\.(.+)$/ and exists $tlds->{$1}) { } 0 0 0
53 0 $tlds->{$1}++;
54 }
55 ▶ elsif ( /^\S[^\.\s]+\.(.+)$/ && exists $tlds->{$1} ) {
Condition0%0/3
/^\S[^\.\s]+\.(.+)$/ and exists $tlds->{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
/^\S[^\.\s]+\.(.+)$/ and exists $tlds->{$1}
/^\S[^\.\s]+\.(.+)$/ exists $tlds->{$1}
Truth table0%0/3
A and B
A= /^\S[^\.\s]+\.(.+)$/B= exists $tlds->{$1}
ABresult
0X0
100
111
56 0 $slds->{$_}++;
57 }
58 }
59  
60 # any manual overrides
61 0 for( qw/ co.uk / ){
62 0 $tlds->{"$_"}++;
63 }
64  
65 0 $self->{tld} = $tlds;
66 0 $self->{sld} = $slds;
67 }
68  
69 1 ▶sub pull {
Subroutine pull: called CC=2 SCAR=7.8
Pod documented
70 1 my $self = shift;
71  
72 1 my $ua = LWP::UserAgent->new;
73 1 my $req = HTTP::Request->new( GET => $self->{source} );
74 1 my $res = $ua->request( $req );
75  
76 1 ▶ if( $res->is_success ){
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 1 1
77 0 open FD, ">$self->{local}";
78 0 local $/;
79 0 print FD $res->content;
80 } else {
81 1 die $res->status_line;
82 }
83 }
84  
85 0 ▶sub generate_regex {
Subroutine generate_regex: not called CC=3 SCAR=24.8
Pod undocumented
86 0 my $self = shift;
87  
88 0 my @a;
89  
90 0 for( keys %{$self->{sld}} ){
91 0 push( @a, $_ );
92 }
93  
94 0 for( keys %{$self->{tld}} ){
95 0 push( @a, $_ );
96 }
97  
98 0 my @atld = sort { length $b cmp length $a } @a;
99  
100 0 my $tld = join( "|", @atld );
101 0 $tld =~ s/\./\\./g;
102  
103 #my $regex = "((?:[a-zA-Z0-9]\\w+\\.)(($tld)|($sld)))";
104 0 my $regex = "((?:[a-zA-Z0-9][\\w\\-]+\\.)+(com|net|org|edu|$tld))\$";
105  
106 0 return $regex;
107 }
108  
109 0 ▶sub match {
Subroutine match: not called CC=7 SCAR=40.3
Pod undocumented
110 0 my $self = shift;
111 0 my $target = shift;
112 0 my $orig = $target;
113  
114 0 my $regex = $self->generate_regex;
115
116 #print "Regex: [$regex]\n";
117  
118 0 my @tokens = split /[^\w\.\-]/, $target;
119 0 my @results;
120  
121 0 for my $target( @tokens ){
122 0 ▶ if( $target =~ /$regex/g ){
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ /$regex/g) 0 0 0
123 0 my $match = $orig = $1;
124 # Extract the TLD
125 0 my @atld = sort { length $b cmp length $a } keys %{$self->{tld}};
126 #my $tld = join( "|", sort keys %{$self->{tld}} );
127 0 my $tld = join( "|", @atld );
128 0 $tld =~ s/\./\\./g;
129  
130 # Extract the TLD from the match
131 0 ▶ my $t = $1 if $match =~ /^.*?\.($tld)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $match =~ /^.*?\.($tld)$/ 0 0 0
132  
133 0 ▶ if( $t ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 0 0 0
134 0 $match =~ s/\.$t$//;
135 }
136  
137 # Extract the domain from the match
138 0 ▶ my $d = $1 if $match =~ /([^\.]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $match =~ /([^\.]+)$/ 0 0 0
139 0 $d =~ s/\.$//;
140  
141 0 ▶ if( $d ){
Branch: 1 branch
BranchTrueFalseTotal
if ($d) 0 0 0
142 0 $match =~ s/$d$//;
143 }
144  
145 0 my $h = $match;
146 0 $h =~ s/\.$//;
147  
148 0 push( @results, { match => $orig, hostname => $h, domain => $d, tld => $t } );
149 }
150 }
151 0 return @results;
152 }
153  
154 1 1;
155  
156 __END__
↑ Summary