File Coverage

blib/lib/Log/Saftpresse/Plugin/GeoIP.pm
Criterion Covered Total %
statement 6 19 31.5
branch 0 6 0.0
condition n/a
subroutine 2 3 66.6
pod 1 1 100.0
total 9 29 31.0


line stmt bran cond sub pod time code
1             package Log::Saftpresse::Plugin::GeoIP;
2              
3 1     1   1023 use Moose;
  1         2  
  1         6  
4              
5             # ABSTRACT: plugin to lookup geoip database
6             our $VERSION = '1.4'; # VERSION
7              
8             extends 'Log::Saftpresse::Plugin';
9              
10             has 'address_fields' => ( is => 'ro', isa => 'Str', default => 'client_ip' );
11              
12             has '_address_fields' => ( is => 'ro', isa => 'ArrayRef', lazy => 1,
13             default => sub {
14             my $self = shift;
15             return( [ split(/\s*,\s*/, $self->address_fields) ] );
16             },
17             );
18              
19 1     1   5206 use Geo::IP;
  1         26698  
  1         267  
20              
21             sub process {
22 0     0 1   my ( $self, $stash ) = @_;
23 0           my $addr;
24              
25 0           foreach my $field ( @{$self->_address_fields} ) {
  0            
26 0 0         if( defined $stash->{$field} ) {
27 0           $addr = $stash->{'client_ip'};
28             }
29             }
30              
31 0 0         if( ! defined $addr ) {
32 0           return;
33             }
34              
35 0           my $cc = $self->_geoip->country_code_by_addr( $addr );
36 0 0         if( defined $cc ) {
37 0           $stash->{'geoip_cc'} = $cc;
38             } else {
39 0           $stash->{'geoip_cc'} = 'unknown';
40             }
41              
42 0           return;
43             }
44              
45             has 'database' => ( is => 'ro', isa => 'Str', default => '/usr/share/GeoIP/GeoIP.dat' );
46              
47             has '_geoip' => (
48             is => 'ro', isa => 'Geo::IP', lazy => 1,
49             default => sub {
50             my $self = shift;
51             return Geo::IP->open( $self->database, GEOIP_STANDARD );
52             },
53             );
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             Log::Saftpresse::Plugin::GeoIP - plugin to lookup geoip database
66              
67             =head1 VERSION
68              
69             version 1.4
70              
71             =head1 AUTHOR
72              
73             Markus Benning <ich@markusbenning.de>
74              
75             =head1 COPYRIGHT AND LICENSE
76              
77             This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
78              
79             This is free software, licensed under:
80              
81             The GNU General Public License, Version 2, June 1991
82              
83             =cut