blib/lib/Geo/Coder/HostIP.pm

filter: Statementstmt 13.4% 16 / 119 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 22.7% 5 / 22 Podpod 86.6% 13 / 15 totaltotal 11.7% 34 / 290 CC 51 SCAR 49.8
CC
51
Cov
7%
GoogleMap
380.0
_parse
210.0
_request
20.0
CRAP 146.0
↑ Summary
1 package Geo::Coder::HostIP;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.07';
11  
12 sub new {
13 0 ▶ my $c = shift;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
14 0 ▶ my $pkg = ref $c || $c;
Condition0%0/3
ref $c || $c
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $c || $c
ref $c $c
Truth table0%0/3
A || B
A= ref $cB= $c
ABresult
1X1
011
000
15 0 my $obj = {};
16 0 bless $obj, $pkg;
17 0 ▶ my $server = shift || 'api.hostip.info';
Condition0%0/2
shift() || 'api.hostip.info'
l!l
00
MC/DC0%0/1
shift() || 'api.hostip.info'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
18 0 $obj->{_server} = $server;
19 0 $obj->{_ua} = LWP::UserAgent->new;
20 0 $obj->{_agent} = 'Geo::Coder::HostIP/0.1';
21 0 return $obj;
22 }
23  
24 sub Agent {
25 0 ▶ my $obj = shift;
Subroutine Agent: not called CC=2 SCAR=17.9
Pod documented
26 0 ▶ my $uaname = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $uaname = shift() or return undef 0 0 0
27 0 $obj->{_agent} = $uaname;
28 }
29  
30 sub _request {
31 0 ▶ my $obj = shift;
Subroutine _request: not called CC=4 SCAR=30.0
32 0 $obj->{_ua}->agent($obj->{_agent});
33 0 ▶ return undef unless $obj->{_server} and $obj->{ip};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"_server"} and $obj->{"ip"} 0 0 0
Condition0%0/3
$obj->{"_server"} and $obj->{"ip"}
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->{"_server"} and $obj->{"ip"}
$obj->{"_server"} $obj->{"ip"}
Truth table0%0/3
A and B
A= $obj->{"_server"}B= $obj->{"ip"}
ABresult
0X0
100
111
34  
35 0 my $res = $obj->{_ua}->get("http://$obj->{_server}/get_html.php?".
36 "ip=$obj->{ip}&position=true");
37  
38 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
39 0 return $obj->_parse($res->content);
40 }
41 else {
42 0 croak $res->status_line;
43 }
44 }
45  
46 sub FetchIP {
47 0 ▶ my $obj = shift;
Subroutine FetchIP: not called CC=2 SCAR=17.9
Pod documented
48 0 ▶ my $ip = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $ip = shift() or return undef 0 0 0
49 0 $obj->{ip} = $ip;
50 0 return $obj->_request;
51 }
52  
53 sub FetchName {
54 0 ▶ my $obj = shift;
Subroutine FetchName: not called CC=2 SCAR=17.9
Pod documented
55 0 ▶ my $name = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return undef 0 0 0
56 0 my $ip = join ".", unpack('C4', (gethostbyname $obj->{domain})[4]);
57 0 return $obj->FetchIP($obj->{domain});
58 }
59  
60 sub FetchRemoteAddr {
61 0 ▶ my $obj = shift;
Subroutine FetchRemoteAddr: not called CC=3 SCAR=24.8
Pod documented
62 0 ▶ if (exists $ENV{REMOTE_ADDR} && defined $ENV{REMOTE_ADDR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'REMOTE_ADDR'} and defined $ENV{'REMOTE_ADDR'}) { } 0 0 0
Condition0%0/3
exists $ENV{"REMOTE_ADDR"} and defined $ENV{"REMOTE_ADDR"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"REMOTE_ADDR"} and defined $ENV{"REMOTE_ADDR"}
exists $ENV{"REMOTE_ADDR"} defined $ENV{"REMOTE_ADDR"}
Truth table0%0/3
A and B
A= exists $ENV{"REMOTE_ADDR"}B= defined $ENV{"REMOTE_ADDR"}
ABresult
0X0
100
111
63 0 return $obj->FetchIP($ENV{REMOTE_ADDR});
64 }
65 else {
66 0 return undef;
67 }
68 }
69  
70 sub Lat {
71 0 ▶ my $obj = shift;
Subroutine Lat: not called CC=2 SCAR=17.9
Pod documented
72 0 ▶ return exists $obj->{Latitude} ? $obj->{Latitude} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'Latitude'} ? : 0 0 0
73 }
74  
75 sub Long {
76 0 ▶ my $obj = shift;
Subroutine Long: not called CC=2 SCAR=17.9
Pod documented
77 0 ▶ return exists $obj->{Longitude} ? $obj->{Longitude} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'Longitude'} ? : 0 0 0
78 }
79  
80 sub Latitude {
81 0 ▶ my $obj = shift;
Subroutine Latitude: not called CC=1 SCAR=6.9
Pod documented
82 0 return $obj->Lat;
83 }
84  
85 sub Longitude {
86 0 ▶ my $obj = shift;
Subroutine Longitude: not called CC=1 SCAR=6.9
Pod documented
87 0 return $obj->Long;
88 }
89  
90 sub City {
91 0 ▶ my $obj = shift;
Subroutine City: not called CC=2 SCAR=17.9
Pod documented
92 0 ▶ return exists $obj->{City} ? $obj->{City} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'City'} ? : 0 0 0
93 }
94  
95 sub Country {
96 0 ▶ my $obj = shift;
Subroutine Country: not called CC=2 SCAR=17.9
Pod documented
97 0 ▶ return exists $obj->{Country} ? $obj->{Country} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'Country'} ? : 0 0 0
98 }
99  
100 sub CountryCode {
101 0 ▶ my $obj = shift;
Subroutine CountryCode: not called CC=2 SCAR=17.9
Pod documented
102 0 ▶ return exists $obj->{Country_Code} ? $obj->{Country_Code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'Country_Code'} ? : 0 0 0
103 }
104  
105 sub State {
106 0 ▶ my $obj = shift;
Subroutine State: not called CC=2 SCAR=17.9
Pod documented
107 0 ▶ return exists $obj->{State} ? $obj->{State} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'State'} ? : 0 0 0
108 }
109  
110 sub Coords {
111 0 ▶ my $obj = shift;
Subroutine Coords: not called CC=4 SCAR=30.0
Pod undocumented
112 0 ▶ if (exists $obj->{Latitude} and exists $obj->{Longitude}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{'Latitude'} and exists $obj->{'Longitude'}) { } 0 0 0
Condition0%0/3
exists $obj->{"Latitude"} and exists $obj->{"Longitude"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $obj->{"Latitude"} and exists $obj->{"Longitude"}
exists $obj->{"Latitude"} exists $obj->{"Longitude"}
Truth table0%0/3
A and B
A= exists $obj->{"Latitude"}B= exists $obj->{"Longitude"}
ABresult
0X0
100
111
113 0 ▶ return wantarray ? ($obj->{Latitude}, $obj->{Longitude}) :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
114 "Lat: $obj->{Latitude}, Long: $obj->{Longitude}";
115 }
116 else {
117 0 return undef;
118 }
119 }
120  
121 sub GoogleMap {
122 0 ▶ my $obj = shift;
Subroutine GoogleMap: not called CC=19 SCAR=59.4
Pod undocumented
123 0 my $params = pop;
124  
125 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
126 0 my $ip = shift;
127 0 $obj->FetchIP($ip);
128 }
129  
130 0 ▶ unless ( exists $obj->{Latitude} and defined $obj->{Latitude}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $obj->{"Latitude"} and defined $obj->{"Latitude"} and exists $obj->{"Longitude"} and defined $obj->{"Longitude"} and ref $params eq "HASH" and $params->{"apikey"}) 0 0 0
Condition0%0/15
exists $obj->{"Latitude"} and defined $obj->{"Latitude"} and exists $obj->{"Longitude"} and defined $obj->{"Longitude"} and ref $params eq "HASH" and $params->{"apikey"}
!ll&&!rl&&r
000
exists $obj->{"Latitude"} and defined $obj->{"Latitude"} and exists $obj->{"Longitude"} and defined $obj->{"Longitude"} and ref $params eq "HASH"
!ll&&!rl&&r
000
exists $obj->{"Latitude"} and defined $obj->{"Latitude"} and exists $obj->{"Longitude"} and defined $obj->{"Longitude"}
!ll&&!rl&&r
000
exists $obj->{"Latitude"} and defined $obj->{"Latitude"} and exists $obj->{"Longitude"}
!ll&&!rl&&r
000
exists $obj->{"Latitude"} and defined $obj->{"Latitude"}
!ll&&!rl&&r
000
MC/DC0%0/6
exists $obj->{"Latitude"} and defined $obj->{"Latitude"} and exists $obj->{"Longitude"} and defined $obj->{"Longitude"} and ref $params eq "HASH" and $params->{"apikey"}
exists $obj->{"Latitude"} defined $obj->{"Latitude"} exists $obj->{"Longitude"} defined $obj->{"Longitude"} ref $params eq "HASH" $params->{"apikey"}
Truth table0%0/7
A and B and C and D and E and F
A= exists $obj->{"Latitude"}B= defined $obj->{"Latitude"}C= exists $obj->{"Longitude"}D= defined $obj->{"Longitude"}E= ref $params eq "HASH"F= $params->{"apikey"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
131 and exists $obj->{Longitude} and defined $obj->{Longitude}
132 and ref $params eq 'HASH' and $params->{apikey}) {
133 0 return undef;
134 }
135  
136 0 my $apikey = $params->{apikey};
137 0 ▶ my $id = $params->{id} || 'googlemap';
Condition0%0/2
$params->{'id'} || 'googlemap'
l!l
00
MC/DC0%0/1
$params->{'id'} || 'googlemap'
$params->{'id'}
Truth table0%0/2
A
A= $params->{'id'}
Aresult
11
00
138 0 ▶ my $fname = $params->{func_name} || 'load';
Condition0%0/2
$params->{'func_name'} || 'load'
l!l
00
MC/DC0%0/1
$params->{'func_name'} || 'load'
$params->{'func_name'}
Truth table0%0/2
A
A= $params->{'func_name'}
Aresult
11
00
139 0 ▶ my $width = $params->{width} || 500;
Condition0%0/2
$params->{'width'} || 500
l!l
00
MC/DC0%0/1
$params->{'width'} || 500
$params->{'width'}
Truth table0%0/2
A
A= $params->{'width'}
Aresult
11
00
140 0 ▶ my $height = $params->{height} || 300;
Condition0%0/2
$params->{'height'} || 300
l!l
00
MC/DC0%0/1
$params->{'height'} || 300
$params->{'height'}
Truth table0%0/2
A
A= $params->{'height'}
Aresult
11
00
141 0 ▶ my $zoom = $params->{zoom} || 13;
Condition0%0/2
$params->{'zoom'} || 13
l!l
00
MC/DC0%0/1
$params->{'zoom'} || 13
$params->{'zoom'}
Truth table0%0/2
A
A= $params->{'zoom'}
Aresult
11
00
142  
143 0 my $controls = '';
144 0 ▶ if ($params->{control}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"control"}) 0 0 0
145 0 ▶ if ($params->{control} eq 'large') {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'control'} eq 'large') { } 0 0 0
elsif ($params->{'control'} eq 'small') { } 0 0 0
146 0 $controls = 'map.addControl(new GLargeMapControl());';
147 }
148 elsif ($params->{control} eq 'small') {
149 0 $controls = 'map.addControl(new GSmallMapControl());';
150 }
151 else {
152 0 $controls = 'map.addControl(new GSmallScaleControl());';
153 }
154 }
155  
156 0 my $scalec = '';
157 0 ▶ if ($params->{scalecontrol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"scalecontrol"}) 0 0 0
158 0 $scalec = 'map.addControl(new GScaleControl());';
159 }
160  
161 0 my $typec = '';
162 0 ▶ if ($params->{typecontrol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"typecontrol"}) 0 0 0
163 0 $typec = 'map.addControl(new GMapTypeControl());';
164 }
165
166 0 my $overc = '';
167 0 ▶ if ($params->{overviewcontrol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"overviewcontrol"}) 0 0 0
168 0 $overc = 'map.addControl(new GOverviewMapControl());';
169 }
170  
171 0 my $lat = $obj->{Latitude} + 0;
172 0 my $long = $obj->{Longitude} + 0;
173  
174 0 return <<"EOF";
175 <html>
176 <head>
177 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=$apikey"
178 type="text/javascript"></script>
179 <script type="text/javascript">
180  
181 //<![CDATA[
182  
183 function $fname() {
184 if (GBrowserIsCompatible()) {
185 var map = new GMap2(document.getElementById('$id'));
186 $controls
187 $scalec
188 $typec
189 $overc
190 map.setCenter(new GLatLng($lat, $long), $zoom);
191 }
192 }
193  
194 //]]>
195 </script>
196 <!-- {{ADDITIONAL HEAD}} -->
197 </head>
198 <body onload="$fname()" onunload="GUnload()">
199 <div id="$id" style="width: ${width}px; height: ${height}px"></div>
200 <!-- {{ADDITIONAL BODY}} -->
201 </body>
202 </html>
203 EOF
204 }
205  
206 sub _parse {
207 0 ▶ my $obj = shift;
Subroutine _parse: not called CC=14 SCAR=53.5
208 0 ▶ my $res = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $res = shift() or return undef 0 0 0
209  
210 0 for my $key (qw(Country City Longitude Latitude)) {
211 0 delete $obj->{$key};
212 }
213  
214 0 my $content = $res;
215 0 my @rows = split /[\r\n]+/, $content;
216 0 chomp @rows;
217  
218 0 for my $row (@rows) {
219 0 my ($k, $v) = split /\s*:\s*/, $row, 2;
220 0 ▶ if ($k eq 'Country') {
Branch: 3 branches
BranchTrueFalseTotal
if ($k eq 'Country') { } 0 0 0
elsif ($k eq 'City') { } 0 0 0
elsif ($k eq 'Latitude' or $k eq 'Longitude') { } 0 0 0
221 0 $v =~ /(.*)\s*\(([^\)]+)\)$/;
222 0 $obj->{Country} = $1;
223 0 $obj->{Country_Code} = $2;
224 }
225 elsif ($k eq 'City') {
226 0 ▶ if ($v =~ /(.*),\s+([A-Z]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /(.*),\s+([A-Z]+)/) 0 0 0
227 0 $obj->{City} = $1;
228 0 $obj->{State} = $2;
229 }
230 }
231 ▶ elsif ($k eq 'Latitude' or $k eq 'Longitude') {
Condition0%0/3
$k eq "Latitude" or $k eq "Longitude"
l!l&&r!l&&!r
000
MC/DC0%0/2
$k eq "Latitude" or $k eq "Longitude"
$k eq "Latitude" $k eq "Longitude"
Truth table0%0/3
A or B
A= $k eq "Latitude"B= $k eq "Longitude"
ABresult
1X1
011
000
232 0 $obj->{$k} = $v + 0;
233 }
234 else {
235 0 ▶ $obj->{$k} ||= $v;
Condition0%0/3
$obj->{$k} ||= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj->{$k} ||= $v
$obj->{$k} $v
Truth table0%0/3
A ||= B
A= $obj->{$k}B= $v
ABresult
1X1
011
000
236 }
237 }
238 0 ▶ if (defined $obj->{Latitude} and defined $obj->{Longitude}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{'Latitude'} and defined $obj->{'Longitude'}) { } 0 0 0
Condition0%0/3
defined $obj->{"Latitude"} and defined $obj->{"Longitude"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $obj->{"Latitude"} and defined $obj->{"Longitude"}
defined $obj->{"Latitude"} defined $obj->{"Longitude"}
Truth table0%0/3
A and B
A= defined $obj->{"Latitude"}B= defined $obj->{"Longitude"}
ABresult
0X0
100
111
239 0 ▶ return wantarray ? ($obj->{Latitude}, $obj->{Longitude}) :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
240 "Lat: $obj->{Latitude}, Long: $obj->{Longitude}";
241 }
242 else {
243 0 ▶ return wantarray ? () : 'Coordinates Not Found';
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
244 }
245 }
246  
247 1 1;
248  
249 __END__
↑ Summary