blib/lib/WWW/HostipInfo.pm

filter: Statementstmt 90.3% 75 / 83 Branchbran 79.5% 35 / 44 Conditioncond 68.1% 15 / 22 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 82.7% 24 / 29 Podpod 100% 6 / 6 totaltotal 81.7% 161 / 197 CC 32 SCAR 17.2
CC
32
Cov
84%
_check_ipaddr
11.4
_set_info_to_obj
9.9
get_info
8.3
CRAP 5.6
↑ Summary
1 ##############################################################################
2 package WWW::HostipInfo;
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use vars qw( $VERSION $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 $VERSION = '0.14';
10  
11 2 my $GetAPI = 'http://api.hostip.info/get_html.php?position=true&ip=';
12 2 my $RoughAPI = 'http://api.hostip.info/get_html.php?position=true&ip=';
13  
14  
15 ## PUBLIC METHOD
16  
17 3 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
18 3 my ( $class, $ip ) = @_;
19 3 ▶ my $opts = ! defined $ip ? {}
Branch: 2 branches
BranchTrueFalseTotal
!defined($ip) ? : 2 1 3
ref $ip eq 'HASH' ? : 0 1 1
20 : ref( $ip ) eq 'HASH' ? $ip
21 : { ip => $ip }
22 ;
23  
24 3 my $self = {
25 url => $GetAPI,
26 %$opts,
27 };
28  
29 3 bless $self, $class;
30  
31 3 ▶ $self->_init() unless ( $self->ua );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ua 3 0 3
32 3 $self;
33 }
34  
35  
36 3 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
37 3 my $self = shift;
38 3 $self->ua( LWP::UserAgent->new(agent => "WWW::HostipInfo/$VERSION") );
39 }
40  
41  
42 7 ▶sub ua {
Subroutine ua: called CC=2 SCAR=6.9
Pod documented
43 7 ▶ $_[0]->{ua} = $_[1] if(@_ > 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 3 4 7
44 $_[0]->{ua}
45 7 }
46  
47  
48 11 ▶sub ip {
Subroutine ip: called CC=2 SCAR=6.9
Pod documented
49 11 ▶ $_[0]->{ip} = $_[1] if(@_ > 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 2 9 11
50 $_[0]->{ip}
51 11 }
52  
53 1 ▶sub url {
Subroutine url: called CC=2 SCAR=7.2
Pod documented
54 1 ▶ $_[0]->{url} = $_[1] if(@_ > 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 1 1
55 $_[0]->{url}
56 1 }
57  
58  
59 2 ▶sub recent_info { $_[0]->{_recent_info}; }
Subroutine recent_info: called CC=1 SCAR=0.0
Pod documented
60  
61  
62 8 ▶sub get_info {
Subroutine get_info: called CC=8 SCAR=21.1
Pod documented
63 8 my $self = shift;
64 8 ▶ my $ip = shift || $self->ip;
Condition66%2/3
shift() || $self->ip
l!l&&r!l&&!r
701
MC/DC50%1/2
shift() || $self->ip
shift() $self->ip
Truth table66%2/3
A || B
A= shift()B= $self->ip
ABresult
1X1
011
000
65 8 ▶ my $opt = shift || {};
Condition50%1/2
shift() || {}
l!l
08
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
66  
67 8 my $chk = $self->_check_ipaddr($ip);
68 # $chk : 0..invalid ip address, 1..class A - C, 2..class D or E, 3..private
69 8 ▶ if(!$chk){
Branch: 3 branches
BranchTrueFalseTotal
if (not $chk) { } 1 7 8
elsif ($chk == 2) { } 1 6 7
elsif ($chk == 3) { } 5 1 6
70 1 croak "IP address is required."
71 }
72 elsif($chk == 2){
73 1 return $self->_get_info_as_null;
74 }
75 elsif($chk == 3){
76 5 return $self->_get_info_as_private
77 }
78  
79 1 $self->ip($ip);
80  
81 1 my $url = $self->url;
82 1 my $response = $self->ua->get($url . $ip);
83  
84 1 ▶ return unless($response->is_success);
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 1 1
85  
86 1 my $content = $response->content;
87  
88 0 ▶ if($DEBUG){ warn $content; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::HostipInfo::DEBUG) 0 1 1
89  
90 1 return $self->_set_info_to_obj($content);
91 }
92  
93  
94 ## PRIVATE METHOD
95  
96 8 ▶sub _check_ipaddr {
Subroutine _check_ipaddr: called CC=11 SCAR=24.3
97 8 ▶ return 0 if(!defined $_[1] or $_[1] !~ /^(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] and $_[1] =~ /^(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/ 1 7 8
Condition66%2/3
defined $_[1] and $_[1] =~ /^(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/
!ll&&!rl&&r
017
MC/DC50%1/2
defined $_[1] and $_[1] =~ /^(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/
defined $_[1] $_[1] =~ /^(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/
Truth table66%2/3
A and B
A= defined $_[1]B= $_[1] =~ /^(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/
ABresult
0X0
100
111
98 7 ▶ return 2 if($1 >= 224); # class D or E
Branch: 1 branch
BranchTrueFalseTotal
if $1 >= 224 1 6 7
99 6 ▶ return ($1 == 127 or $1 == 10) ? 3
Branch: 3 branches
BranchTrueFalseTotal
$1 == 127 || $1 == 10 ? : 2 4 6
$1 == 192 && $2 == 168 ? : 1 3 4
$1 == 172 && $2 >= 16 && $2 <= 31 ? : 2 1 3
Condition75%9/12
$1 == 127 or $1 == 10
l!l&&r!l&&!r
114
$1 == 192 and $2 == 168
!ll&&!rl&&r
301
$1 == 172 and $2 >= 16 and $2 <= 31
!ll&&!rl&&r
102
$1 == 172 and $2 >= 16
!ll&&!rl&&r
102
MC/DC100%2/2
$1 == 127 or $1 == 10
$1 == 127 $1 == 10
MC/DC50%1/2
$1 == 192 and $2 == 168
$1 == 192 $2 == 168
MC/DC33%1/3
$1 == 172 and $2 >= 16 and $2 <= 31
$1 == 172 $2 >= 16 $2 <= 31
Truth table100%3/3
A or B
A= $1 == 127B= $1 == 10
ABresult
1X1
011
000
Truth table66%2/3
A and B
A= $1 == 192B= $2 == 168
ABresult
0X0
100
111
Truth table50%2/4
A and B and C
A= $1 == 172B= $2 >= 16C= $2 <= 31
ABCresult
0XX0
10X0
1100
1111
100 : ($1 == 192 and $2 == 168) ? 3
101 : ($1 == 172 and $2 >= 16 and $2 <= 31) ? 3 : 1;
102 }
103  
104  
105 1 ▶sub _set_info_to_obj {
Subroutine _set_info_to_obj: called CC=9 SCAR=23.0
106 1 my ( $self, $content ) = @_;
107 1 my ( $country_code, $city, $lat, $lon, $region, $name, $code );
108  
109 1 for my $line ( split/\n/, $content ) {
110 3 ▶ if ( $line =~ /^Country: (.+?) \(([A-Z][A-Z])\)$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^Country: (.+?) \(([A-Z][A-Z])\)$/) { } 1 2 3
elsif ($line =~ /^City: ([^,]+)(?:, (\w+))?$/) { } 1 1 2
elsif ($line =~ /^Latitude: (.+)$/) { } 0 1 1
elsif ($line =~ /^Longitude: (.+)$/) { } 0 1 1
111 1 ( $name, $code ) = ( $1, $2 );
112 }
113 elsif ( $line =~ /^City: ([^,]+)(?:, (\w+))?$/ ) {
114 1 ($city, $region) = ( $1, $2 );
115 }
116 elsif ( $line =~ /^Latitude: (.+)$/ ) {
117 0 $lat = $1;
118 }
119 elsif ( $line =~ /^Longitude: (.+)$/ ) {
120 0 $lon = $1;
121 }
122 }
123  
124 1 my ($unknown_city, $unknown_country) = (0,0);
125 1 ▶ if($city =~ /^\([uU]nknown [cC]ity/){
Branch: 1 branch
BranchTrueFalseTotal
if ($city =~ /^\([uU]nknown [cC]ity/) 1 0 1
126 1 $city = ''; $region = ''; $unknown_city = 1;
127 }
128 1 ▶ if($name =~ /^\([uU]nknown [cC]ountry/){
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\([uU]nknown [cC]ountry/) 1 0 1
129 1 $name = ''; $unknown_country = 1;
130 }
131  
132 1 ▶ $self->{_recent_info} = bless {
Condition50%1/2
$region || ''
l!l
01
MC/DC0%0/1
$region || ''
$region
Truth table50%1/2
A
A= $region
Aresult
11
00
133 _ipaddr => $self->ip,
134 _CountryName => $name,
135 _CountryCode => $code,
136 _City => $city,
137 _Region => ($region || ''),
138 _Latitude => $lat,
139 _Longitude => $lon,
140 _private => 0,
141 _un_city => $unknown_city,
142 _un_country => $unknown_country,
143 }, 'WWW::HostipInfo::Info';
144 }
145  
146  
147 1 ▶sub _get_info_as_null {
Subroutine _get_info_as_null: called CC=1 SCAR=0.0
148 1 $_[0]->{_recent_info} = bless {
149 _ipaddr => $_[0]->ip,
150 _CountryName => '',
151 _CountryCode => 'XX',
152 _City => '',
153 _Region => undef,
154 _Latitude => undef,
155 _Longitude => undef,
156 _private => 0,
157 _un_city => 1,
158 _un_country => 1,
159 }, 'WWW::HostipInfo::Info';
160 }
161  
162  
163 5 ▶sub _get_info_as_private {
Subroutine _get_info_as_private: called CC=1 SCAR=0.0
164 5 $_[0]->{_recent_info} = bless {
165 _ipaddr => $_[0]->ip,
166 _CountryName => '',
167 _CountryCode => 'XX',
168 _City => '',
169 _Region => undef,
170 _Latitude => undef,
171 _Longitude => undef,
172 _private => 1,
173 _un_city => 1,
174 _un_country => 1,
175 }, 'WWW::HostipInfo::Info';
176 }
177  
178  
179 ##############################################################################
180 # Information Class
181 ##############################################################################
182  
183 package WWW::HostipInfo::Info;
184  
185 7 ▶sub is_private { $_[0]->{_private}; }
Subroutine is_private: called CC=1 SCAR=0.0
186  
187 1 ▶sub is_guessed { $_[0]->{_guessed}; }
Subroutine is_guessed: called CC=1 SCAR=0.0
188  
189 1 ▶sub has_unknown_city { $_[0]->{_un_city}; }
Subroutine has_unknown_city: called CC=1 SCAR=0.0
190  
191 2 ▶sub has_unknown_country { $_[0]->{_un_country}; }
Subroutine has_unknown_country: called CC=1 SCAR=0.0
192  
193 4 ▶sub ip { $_[0]->{_ipaddr}; }
Subroutine ip: called CC=1 SCAR=0.0
194  
195 0 ▶sub name { $_[0]->{_CountryName}; }
Subroutine name: not called CC=1 SCAR=6.9
196  
197 2 ▶sub code { $_[0]->{_CountryCode}; }
Subroutine code: called CC=1 SCAR=0.0
198  
199 1 ▶sub country_name { $_[0]->{_CountryName}; }
Subroutine country_name: called CC=1 SCAR=0.0
200  
201 4 ▶sub country_code { $_[0]->{_CountryCode}; }
Subroutine country_code: called CC=1 SCAR=0.0
202  
203 1 ▶sub country { $_[0]->{_CountryName}; }
Subroutine country: called CC=1 SCAR=0.0
204  
205 0 ▶sub city { $_[0]->{_City}; }
Subroutine city: not called CC=1 SCAR=6.9
206  
207 0 ▶sub region { $_[0]->{_Region}; }
Subroutine region: not called CC=1 SCAR=6.9
208  
209 0 ▶sub latitude { $_[0]->{_Latitude}; }
Subroutine latitude: not called CC=1 SCAR=6.9
210  
211 0 ▶sub longitude { $_[0]->{_Longitude} }
Subroutine longitude: not called CC=1 SCAR=6.9
212  
213 ##############################################################################
214 2 1;
215 __END__
↑ Summary