GeoIP2.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 69 CC 4 SCAR 16.4
CC
4
Cov
0%
prepare_app
12.0
call
6.0
BEGIN
2.0
CRAP 5.2
1 package Plack::Middleware::GeoIP2;
2 use strict;
3 use warnings;
4 use 5.008;
5 our $VERSION = 0.05;
6 use parent qw/Plack::Middleware/;
7 use GeoIP2::Database::Reader;
8 use Carp;
9  
10 use Plack::Util::Accessor qw( GeoIP2DBFile GeoIP2Locales );
11  
12 sub prepare_app {
13 my $self = shift;
14  
15 my $locales = $self->GeoIP2Locales || ['en'];
16  
17 if (my $dbfiles = $self->GeoIP2DBFile) {
18 my @dbfiles = ref $dbfiles ? @{ $dbfiles } : ($dbfiles);
19 foreach my $dbfile (@dbfiles) {
20 my $gi = GeoIP2::Database::Reader->new(
21 file => $dbfile,
22 locales => $locales,
23 );
24 push @{ $self->{gips} }, $gi;
25 }
26 }
27 else {
28 Carp::croak('GeoIP2 database not found!');
29 }
30 }
31  
32 sub call {
33 my $self = shift;
34 my $env = shift;
35  
36 my $ipaddr = $env->{REMOTE_ADDR};
37  
38 foreach my $gi (@{ $self->{gips} }) {
39 eval {
40 my $record = $gi->country( ip => $ipaddr );
41 $env->{GEOIP_COUNTRY_CODE} = $record->country->iso_code;
42 $env->{GEOIP_COUNTRY_NAME} = $record->country->name;
43 $env->{GEOIP_CONTINENT_CODE} = $record->continent->name;
44 };
45 if ($@) {
46 $env->{GEOIP_COUNTRY_CODE} = 'ZZ';
47 $env->{GEOIP_COUNTRY_NAME} = 'Unknown Country';
48 $env->{GEOIP_CONTINENT_CODE} = 'Unknown Continent';
49 }
50 }
51  
52 return $self->app->($env);
53 }
54  
55 1;
56  
57 =head1 NAME
58  
59 Plack::Middleware::GeoIP2 - Find country and city of origin of a web request
60  
61 =head1 SYNOPSIS
62  
63 # with Plack::Middleware::RealIP
64 enable 'Plack::Middleware::RealIP',
65 header => 'X-Forwarded-For',
66 trusted_proxy => [ qw(192.168.1.0/24 192.168.2.1) ];
67 enable 'Plack::Middleware::GeoIP',
68 GeoIP2DBFile => [ '/path/to/GeoLite2-Country.mmdb', '/path/to/GeoIP2-Country.mmdb' ];
69  
70 =head1 DESCRIPTION
71  
72 Plack::Middleware::GeoIP2 is a version of Plack::Middleware::GeoIP using
73 the more recent GeoIP2 library from MaxMind.
74  
75 All requests are looked up and GEOIP_* variables are added to PSGI
76 environment hash. For improved performance, you may want to only enable
77 this middleware for specific URL's.
78  
79 The following PSGI environment variables are set by this middleware:
80  
81 GeoIP Country Edition:
82  
83 GEOIP_COUNTRY_CODE, GEOIP_COUNTRY_NAME, GEOIP_CONTINENT_CODE
84  
85 When REMOTE_ADDR is an invalid/unknown IP, this module will set 'ZZ',
86 'Unknown Country', and 'Unknown Continent' for the above variables.
87  
88 =head1 CONFIGURATION
89  
90 =over 4
91  
92 =item GeoIPDBFile
93  
94 GeoIPDBFile => '/path/to/GeoIP.dat'
95 GeoIPDBFile => [ '/path/to/GeoIP.dat', '/path/to/GeoIPCity.dat' ]
96  
97 Path to GeoIP2 data files.
98  
99 =item GeoIP2Locales
100  
101 GeoIP2Locales => [ 'en', 'de' ]
102  
103 Array of locale names passed to GeoIP2, to localize GEOIP_COUNTRY_NAME.
104  
105 =back
106  
107 =head1 AUTHOR
108  
109 Zak B. Elep E<lt>zakame@cpan.orgE<gt>
110  
111 =head1 LICENSE
112  
113 This library is free software; you can redistribute it and/or modify
114 it under the same terms as Perl itself.
115  
116 =head1 SEE ALSO
117  
118 L<mod_geoip|http://www.maxmind.com/app/mod_geoip>
119  
120 L<GeoIP2>
121  
122 =cut