blib/lib/Geo/IP/RU/IpGeoBase.pm

filter: Statementstmt 8.6% 10 / 116 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 13.6% 3 / 22 Podpod 15.7% 3 / 19 totaltotal 6.7% 16 / 236 CC 33 SCAR 33.1
CC
33
Cov
6%
init
90.0
decode
42.0
process_file
30.0
CRAP 27.3
↑ Summary
1 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Geo::IP::RU::IpGeoBase;
6  
7 1 our $VERSION = '0.05';
8  
9 =head1 NAME
10  
11 Geo::IP::RU::IpGeoBase - look up location by IP address in Russia
12  
13 =head1 DESCRIPTION
14  
15 This module allows you to look up location in DB provided by
16 http://ipgeobase.ru service. Access to the DB is free. Contains
17 information about city, region, federal district and coordinates.
18  
19 DB provided as plain text files and is not very suitable for look
20 ups without loading all data into memory. Instead it's been decided
21 to import data into a database. Use command line utility to create
22 and update back-end DB.
23  
24 At this moment DB can be created in SQLite, mysql and Pg. If
25 you create table manually then probably module will just work.
26 It's very easy to add support for more back-end DBs. Patches are
27 welcome.
28  
29 =head1 METHODS
30  
31 =head2 new
32  
33 Returns a new object. Takes a hash with options, mostly
34 description of the back-end:
35  
36 Geo::IP::RU::IpGeoBase->new( db => {
37 dbh => $dbh, table => 'my_table',
38 } );
39 # or
40 Geo::IP::RU::IpGeoBase->new( db => {
41 dsn => 'dbi:mysql:mydb',
42 user => 'root', pass => 'secret',
43 table => 'my_table',
44 } );
45  
46 =over 4
47  
48 =item * dbh - connected L<DBI> handle, or you can use dsn.
49  
50 =item * dsn, user, pass - DSN like described in L<DBI>, for
51 example 'dbi:SQLite:my.db', user name and his password.
52  
53 =item * table - name of the table with data, default
54 is 'ip_geo_base_ru'.
55  
56 =back
57  
58 =cut
59  
60 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
61 0 my $proto = shift;
62 0 ▶ my $self = bless { @_ }, ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
63 0 return $self->init;
64 }
65  
66 0 ▶sub init {
Subroutine init: not called CC=9 SCAR=45.0
Pod undocumented
67 0 my $self = shift;
68  
69 die "No information about database"
70 0 ▶ unless my $db = $self->{'db'};
Branch: 1 branch
BranchTrueFalseTotal
unless my $db = $self->{"db"} 0 0 0
71  
72 0 ▶ unless ( $db->{'dbh'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db->{'dbh'}) { } 0 0 0
73 0 ▶ die "No dsn and no dbh" unless $db->{'dsn'};
Branch: 1 branch
BranchTrueFalseTotal
unless $db->{"dsn"} 0 0 0
74  
75 0 require DBI;
76 $db->{'dbh'} = DBI->connect(
77 0 ▶ $db->{'dsn'}, $db->{'user'}, $db->{'pass'},
Branch: 1 branch
BranchTrueFalseTotal
$db->{"dbh"} = "DBI"->connect($db->{"dsn"}, $db->{"user"}, $db->{"pass"}, {"RaiseError", 0, "PrintError", 0}) or die q[Couldn't connect to the DB: ] . "DBI"->errstr 0 0 0
78 { RaiseError => 0, PrintError => 0 }
79 ) or die "Couldn't connect to the DB: ". DBI->errstr;
80 0 $db->{'dbh'}->do("SET NAMES 'utf8'");
81 0 $db->{'decode'} = 1;
82 } else {
83 $db->{'decode'} = 1
84 0 ▶ unless exists $db->{'decode'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $db->{"decode"} 0 0 0
85 }
86 0 ▶ if ( $db->{'decode'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db->{"decode"}) 0 0 0
87 0 require Encode;
88 0 $db->{'decoder'} = Encode::find_encoding('UTF-8');
89 }
90  
91 0 ▶ $db->{'driver'} = $db->{'dbh'}{'Driver'}{'Name'}
Branch: 1 branch
BranchTrueFalseTotal
$db->{"driver"} = $db->{"dbh"}{"Driver"}{"Name"} or die "Couldn't figure out driver name of the DB" 0 0 0
92 or die "Couldn't figure out driver name of the DB";
93  
94 0 ▶ $db->{'table'} ||= 'ip_geo_base_ru';
Condition0%0/2
$db->{"table"} ||= "ip_geo_base_ru"
l!l
00
MC/DC0%0/1
$db->{"table"} ||= "ip_geo_base_ru"
$db->{"table"}
Truth table0%0/2
A
A= $db->{"table"}
Aresult
11
00
95 0 $db->{'quoted_table'} = $db->{'dbh'}->quote_identifier($db->{'table'});
96  
97 0 return $self;
98 }
99  
100 =head2 find_by_ip
101  
102 Takes an IP in 'xxx.xxx.xxx.xxx' format and returns information
103 about blocks that contains this IP. Yep, blocks, not a block.
104 In theory DB may contain intersecting blocks.
105  
106 Each record is a hash reference with the fields matching table
107 columns: istart, iend, start, end, city, region, federal_district,
108 latitude and longitude.
109  
110 =cut
111  
112 0 ▶sub find_by_ip {
Subroutine find_by_ip: not called CC=2 SCAR=17.9
Pod documented
113 0 my $self = shift;
114 0 ▶ my $ip = shift or die 'No IP provided';
Branch: 1 branch
BranchTrueFalseTotal
my $ip = shift() or die "No IP provided" 0 0 0
115 0 my $int = $self->ip2int($ip);
116 0 return $self->intersections( $int, $int, order => 'ASC', @_ );
117 }
118  
119 0 ▶sub ip2int { return unpack 'N', pack 'C4', split /[.]/, $_[1] }
Subroutine ip2int: not called CC=1 SCAR=6.9
Pod undocumented
120 0 ▶sub int2ip { return join '.', unpack "C4", pack "N", $_[1] }
Subroutine int2ip: not called CC=1 SCAR=6.9
Pod undocumented
121  
122 0 ▶sub intersections {
Subroutine intersections: not called CC=4 SCAR=30.0
Pod undocumented
123 0 my $self = shift;
124 0 my ($istart, $iend, %rest) = @_;
125 0 my $table = $self->db_info->{'quoted_table'};
126 0 my $dbh = $self->dbh;
127 0 my $query = "SELECT * FROM $table WHERE "
128 . $dbh->quote_identifier('istart') .' <= '. $dbh->quote($iend)
129 .' AND '. $dbh->quote_identifier('iend') .' >= '. $dbh->quote($istart);
130 $query .= ' ORDER BY iend - istart '. $rest{'order'}
131 0 ▶ if $rest{'order'};
Branch: 1 branch
BranchTrueFalseTotal
if $rest{"order"} 0 0 0
132 0 my $res = $dbh->selectall_arrayref( $query, { Slice => {} } );;
133 0 ▶ die "Couldn't execute '$query': ". $dbh->errstr if !$res && $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
if not $res and $dbh->errstr 0 0 0
Condition0%0/3
not $res and $dbh->errstr
!ll&&!rl&&r
000
MC/DC0%0/2
not $res and $dbh->errstr
not $res $dbh->errstr
Truth table0%0/3
not(A) and B
A= not $resB= $dbh->errstr
ABresult
0X0
100
111
134 0 return @{ $self->decode( $res ) };
135 }
136  
137 0 ▶sub fetch_record {
Subroutine fetch_record: not called CC=3 SCAR=24.8
Pod undocumented
138 0 my $self = shift;
139 0 my ($istart, $iend) = @_;
140 0 my $table = $self->db_info->{'quoted_table'};
141 0 my $dbh = $self->dbh;
142 0 my $query = "SELECT * FROM $table WHERE "
143 . $dbh->quote_identifier('istart') .' = '. $dbh->quote($istart)
144 .' AND '. $dbh->quote_identifier('iend') .' = '. $dbh->quote($iend);
145 0 my $res = $self->dbh->selectrow_hashref( $query );
146 0 ▶ die "Couldn't execute '$query': ". $dbh->errstr if !$res && $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
if not $res and $dbh->errstr 0 0 0
Condition0%0/3
not $res and $dbh->errstr
!ll&&!rl&&r
000
MC/DC0%0/2
not $res and $dbh->errstr
not $res $dbh->errstr
Truth table0%0/3
not(A) and B
A= not $resB= $dbh->errstr
ABresult
0X0
100
111
147 0 return $self->decode( $res );
148 }
149  
150 0 ▶sub insert_record {
Subroutine insert_record: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my $self = shift;
152 0 my %rec = @_;
153  
154 0 my $table = $self->db_info->{'quoted_table'};
155 0 my @keys = keys %rec;
156 0 my $dbh = $self->dbh;
157 my $query =
158 "INSERT INTO $table(". join( ', ', map $dbh->quote_identifier($_), @keys) .")"
159 0 ." VALUES (". join( ', ', map $dbh->quote( $rec{$_} ), @keys ) .")";
160 0 ▶ return $dbh->do( $query ) || die "Couldn't execute '$query': ". $dbh->errstr;
Condition0%0/2
$dbh->do($query) || die("Couldn't execute '${query}': " . $dbh->errstr)
l!l
00
MC/DC0%0/1
$dbh->do($query) || die("Couldn't execute '${query}': " . $dbh->errstr)
$dbh->do($query)
Truth table0%0/2
A
A= $dbh->do($query)
Aresult
11
00
161 }
162  
163 0 ▶sub update_record {
Subroutine update_record: not called CC=3 SCAR=24.8
Pod undocumented
164 0 my $self = shift;
165 0 my %rec = @_;
166  
167 0 my $table = $self->db_info->{'quoted_table'};
168  
169 0 ▶ my @keys = grep $_ ne 'istart' && $_ ne 'iend', keys %rec;
Condition0%0/3
$_ ne 'istart' && $_ ne 'iend'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne 'istart' && $_ ne 'iend'
$_ ne 'istart' $_ ne 'iend'
Truth table0%0/3
A && B
A= $_ ne 'istart'B= $_ ne 'iend'
ABresult
0X0
100
111
170 0 my $dbh = $self->dbh;
171 my $query =
172 "UPDATE $table SET "
173 . join(
174 ' AND ',
175 map $dbh->quote_identifier($_) .' = '. $dbh->quote($rec{$_}),
176 @keys
177 )
178 ." WHERE "
179 . join(
180 ' AND ',
181 0 map $dbh->quote_identifier($_) .' = '. $dbh->quote($rec{$_}),
182 qw(istart iend)
183 );
184 0 ▶ return $dbh->do( $query ) || die "Couldn't execute '$query': ". $dbh->errstr;
Condition0%0/2
$dbh->do($query) || die("Couldn't execute '${query}': " . $dbh->errstr)
l!l
00
MC/DC0%0/1
$dbh->do($query) || die("Couldn't execute '${query}': " . $dbh->errstr)
$dbh->do($query)
Truth table0%0/2
A
A= $dbh->do($query)
Aresult
11
00
185 }
186  
187 0 ▶sub delete_record {
Subroutine delete_record: not called CC=2 SCAR=17.9
Pod undocumented
188 0 my $self = shift;
189 0 my ($istart, $iend) = @_;
190 0 my $table = $self->db_info->{'quoted_table'};
191 0 my $dbh = $self->dbh;
192 0 my $query = "DELETE FROM $table WHERE "
193 . $dbh->quote_identifier('istart') .' = '. $dbh->quote($istart)
194 .' AND '. $dbh->quote_identifier('iend') .' = '. $dbh->quote($iend);
195 0 ▶ return $dbh->do( $query ) || die "Couldn't execute '$query': ". $dbh->errstr;
Condition0%0/2
$dbh->do($query) || die("Couldn't execute '${query}': " . $dbh->errstr)
l!l
00
MC/DC0%0/1
$dbh->do($query) || die("Couldn't execute '${query}': " . $dbh->errstr)
$dbh->do($query)
Truth table0%0/2
A
A= $dbh->do($query)
Aresult
11
00
196 }
197  
198 0 ▶sub decode {
Subroutine decode: not called CC=6 SCAR=37.4
Pod undocumented
199 0 my $self = shift;
200 0 my $value = shift;
201 0 ▶ return $value unless $self->{'db'}{'decode'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"db"}{"decode"} 0 0 0
202 0 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
203  
204 0 my $decoder = $self->{'db'}{'decoder'};
205 0 ▶ foreach my $rec ( ref($value) eq 'ARRAY'? (@$value) : ($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 0 0
206 0 $_ = $decoder->decode($_) foreach grep defined, values %$rec;
207 }
208 0 return $value;
209 }
210  
211 0 ▶sub process_file {
Subroutine process_file: not called CC=5 SCAR=34.0
Pod undocumented
212 0 my $self = shift;
213 0 ▶ my %args = (@_%2? (path => @_) : @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
214  
215 0 my $file = $args{'path'};
216 0 my @fields = @{ $args{'fields'} };
217  
218 0 ▶ open my $fh, '<:encoding(cp1251)', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(cp1251)", $file or die "Couldn't open $file" 0 0 0
219 or die "Couldn't open $file";
220  
221 0 ▶ while ( my $str = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $str = readline $fh)) 0 0 0
222 0 chomp $str;
223  
224 0 my %rec;
225 0 @rec{ @fields } = split /\t/, $str;
226 0 delete $rec{'country'};
227 @rec{'start', 'end'} = $self->split_block( delete $rec{'block'} )
228 0 ▶ if exists $rec{'block'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rec{"block"} 0 0 0
229  
230 0 $args{'callback'}->( \%rec );
231 }
232 0 close $fh;
233 }
234  
235 0 ▶sub split_block { return split /\s*-\s*/, $_[1], 2; }
Subroutine split_block: not called CC=1 SCAR=6.9
Pod undocumented
236  
237  
238 0 ▶sub db_info { return $_[0]->{'db'} }
Subroutine db_info: not called CC=1 SCAR=6.9
Pod undocumented
239  
240 0 ▶sub dbh { return $_[0]->{'db'}{'dbh'} }
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
241  
242 0 ▶sub create_table {
Subroutine create_table: not called CC=2 SCAR=17.9
Pod undocumented
243 0 my $self = shift;
244  
245 0 my $driver = $self->db_info->{'driver'};
246  
247 0 my $call = 'create_'. lc( $driver ) .'_table';
248 0 ▶ die "Table creation is not supported for $driver"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($call) 0 0 0
249 unless $self->can($call);
250  
251 0 return $self->$call();
252 }
253  
254 0 ▶sub create_sqlite_table {
Subroutine create_sqlite_table: not called CC=2 SCAR=17.9
Pod undocumented
255 0 my $self = shift;
256  
257 0 my $table = $self->db_info->{'quoted_table'};
258 0 my $query = <<END;
259 CREATE TABLE $table (
260 istart INTEGER NOT NULL,
261 iend INTEGER NOT NULL,
262 start TEXT NOT NULL,
263 end TEXT NOT NULL,
264 city TEXT,
265 region TEXT,
266 federal_district TEXT,
267 latitude REAL,
268 longitude REAL,
269 in_update INT NOT NULL DEFAULT(0),
270 PRIMARY KEY (istart ASC, iend ASC)
271 )
272 END
273 0 ▶ return $self->dbh->do( $query )
Condition0%0/2
$self->dbh->do($query) || die("Couldn't execute '${query}': " . $self->dbh->errstr)
l!l
00
MC/DC0%0/1
$self->dbh->do($query) || die("Couldn't execute '${query}': " . $self->dbh->errstr)
$self->dbh->do($query)
Truth table0%0/2
A
A= $self->dbh->do($query)
Aresult
11
00
274 || die "Couldn't execute '$query': ". $self->dbh->errstr;
275 }
276  
277 0 ▶sub create_mysql_table {
Subroutine create_mysql_table: not called CC=2 SCAR=17.9
Pod undocumented
278 0 my $self = shift;
279 0 my $table = $self->db_info->{'quoted_table'};
280 0 my $query = <<END;
281 CREATE TABLE $table (
282 istart INTEGER UNSIGNED NOT NULL,
283 iend INTEGER UNSIGNED NOT NULL,
284 start VARCHAR(15) NOT NULL,
285 end VARCHAR(15) NOT NULL,
286 city TEXT,
287 region TEXT,
288 federal_district TEXT,
289 latitude FLOAT(8,6),
290 longitude FLOAT(8,6),
291 in_update TINYINT NOT NULL DEFAULT 0,
292 PRIMARY KEY (istart, iend)
293 ) CHARACTER SET 'utf8'
294 END
295 0 ▶ return $self->dbh->do( $query )
Condition0%0/2
$self->dbh->do($query) || die("Couldn't execute '${query}': " . $self->dbh->errstr)
l!l
00
MC/DC0%0/1
$self->dbh->do($query) || die("Couldn't execute '${query}': " . $self->dbh->errstr)
$self->dbh->do($query)
Truth table0%0/2
A
A= $self->dbh->do($query)
Aresult
11
00
296 || die "Couldn't execute '$query': ". $self->dbh->errstr;
297 }
298  
299 0 ▶sub create_pg_table {
Subroutine create_pg_table: not called CC=2 SCAR=17.9
Pod undocumented
300 0 my $self = shift;
301 0 my $table = $self->db_info->{'quoted_table'};
302 0 my $endq = $self->dbh->quote_identifier('end');
303 0 my $query = <<END;
304 CREATE TABLE $table (
305 istart BIGINT NOT NULL,
306 iend BIGINT NOT NULL,
307 start VARCHAR(15) NOT NULL,
308 $endq VARCHAR(15) NOT NULL,
309 city TEXT,
310 region TEXT,
311 federal_district TEXT,
312 latitude NUMERIC(8,6),
313 longitude NUMERIC(8,6),
314 in_update INT2 NOT NULL DEFAULT 0,
315 PRIMARY KEY (istart, iend)
316 )
317 END
318 0 ▶ return $self->dbh->do( $query )
Condition0%0/2
$self->dbh->do($query) || die("Couldn't execute '${query}': " . $self->dbh->errstr)
l!l
00
MC/DC0%0/1
$self->dbh->do($query) || die("Couldn't execute '${query}': " . $self->dbh->errstr)
$self->dbh->do($query)
Truth table0%0/2
A
A= $self->dbh->do($query)
Aresult
11
00
319 || die "Couldn't execute '$query': ". $self->dbh->errstr;
320 }
321  
322 =head1 AUTHOR
323  
324 Ruslan Zakirov E<gt>Ruslan.Zakirov@gmail.comE<lt>
325  
326 =head1 LICENSE
327  
328 Under the same terms as perl itself.
329  
330 =cut
331  
332 1 1;
↑ Summary