| 1 | package HTTP::ClientDetect::Location; | ||||||||||||||
| 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 FATAL => 'all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use Module::Load; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | =head1 NAME | ||||||||||||||
| 10 | |||||||||||||||
| 11 | HTTP::ClientDetect::Location - Lookup the country of the client using Geo::IP | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||
| 14 | |||||||||||||||
| 15 | Version 0.01 | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =cut | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 1 | our $VERSION = '0.01'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||
| 23 | |||||||||||||||
| 24 | use HTTP::ClientDetect::Location; | ||||||||||||||
| 25 | my $geo = HTTP::ClientDetect::Location->new(db => "/path/to/geo-ip"); | ||||||||||||||
| 26 | # inside a Dancer route | ||||||||||||||
| 27 | get '/detect' => sub { | ||||||||||||||
| 28 | my $req = request; | ||||||||||||||
| 29 | my $country_code = $geo->country_code($req); | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 ACCESSORS | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =head2 db | ||||||||||||||
| 36 | |||||||||||||||
| 37 | Path to the geoip database, which can be retrieved from | ||||||||||||||
| 38 | http://dev.maxmind.com/geoip/legacy/geolite/ | ||||||||||||||
| 39 | |||||||||||||||
| 40 | C<db> must be passed to the constructor. | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =cut | ||||||||||||||
| 43 | |||||||||||||||
| 44 | has db => (is => 'ro', | ||||||||||||||
| 45 | required => 1, | ||||||||||||||
| 46 | isa => sub { | ||||||||||||||
| 47 | 0 | ▶ | die "db is not a file" unless -f $_[0]; | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||
| 48 | 1 | }); | |||||||||||||
| 49 | |||||||||||||||
| 50 | |||||||||||||||
| 51 | =head2 geo | ||||||||||||||
| 52 | |||||||||||||||
| 53 | This accessor wraps L<Geo::IP> or L<Geo::IP::PurePerl>. Calling | ||||||||||||||
| 54 | $geo->geo will return an instantiated object, and you can call, e.g. | ||||||||||||||
| 55 | C<$geo->geo->country_code_by_addr("128.31.0.51")> or | ||||||||||||||
| 56 | C<$geo->geo->country_code_by_name("linuxia.de")>. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =cut | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 1 | has geo => (is => 'rwp', | |||||||||||||
| 61 | builder => 1); | ||||||||||||||
| 62 | |||||||||||||||
| 63 | sub _build_geo { | ||||||||||||||
| 64 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_geo: not called CC=2 SCAR=17.9
| |||||||||||||||
| 65 | 0 | my $module = "Geo::IP"; | |||||||||||||
| 66 | 0 | eval { | |||||||||||||
| 67 | 0 | load $module; | |||||||||||||
| 68 | }; | ||||||||||||||
| 69 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 0 | $module .= "::PurePerl"; | |||||||||||||
| 71 | # if we die here too bad | ||||||||||||||
| 72 | 0 | load $module; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | 0 | my $gi = $module->open($self->db); | |||||||||||||
| 75 | 0 | return $gi; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | =head1 SUBROUTINES/METHODS | ||||||||||||||
| 79 | |||||||||||||||
| 80 | =head2 request_country($request_obj) | ||||||||||||||
| 81 | |||||||||||||||
| 82 | Return the country of the request. The request should be a string with | ||||||||||||||
| 83 | the ip or L<Dancer::Request> object (or something that respond to the | ||||||||||||||
| 84 | method "remote_address"). | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =cut | ||||||||||||||
| 87 | |||||||||||||||
| 88 | sub request_country { | ||||||||||||||
| 89 | 0 | ▶ | my ($self, $arg) = @_; | ||||||||||||
|
Subroutine
request_country: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 90 | 0 | my $ip; | |||||||||||||
| 91 | 0 | ▶ | if (ref($arg)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | # Dancer | ||||||||||||||
| 93 | 0 | ▶ | if ($arg->can("remote_address")) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 94 | 0 | $ip = $arg->remote_address; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | # Catalyst | ||||||||||||||
| 97 | elsif ($arg->can("address")) { | ||||||||||||||
| 98 | 0 | $ip = $arg->address; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | } | ||||||||||||||
| 101 | else { | ||||||||||||||
| 102 | 0 | $ip = $arg; | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | 0 | ▶ | return unless $ip; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | ▶ | if ($ip =~ m/(([0-9]{1,3}\.){3}([0-9]{1,3}))/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | $ip = $1; | |||||||||||||
| 107 | 0 | return $self->geo->country_code_by_addr($ip); | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | else { | ||||||||||||||
| 110 | 0 | return; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 AUTHOR | ||||||||||||||
| 115 | |||||||||||||||
| 116 | Marco Pessotto, C<< <melmothx at gmail.com> >> | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =head1 BUGS | ||||||||||||||
| 119 | |||||||||||||||
| 120 | Please report any bugs or feature requests to C<bug-interchange6-plugin-autodetect at rt.cpan.org>, or through | ||||||||||||||
| 121 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTTP-ClientDetect>. I will be notified, and then you'll | ||||||||||||||
| 122 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||
| 123 | |||||||||||||||
| 124 | |||||||||||||||
| 125 | |||||||||||||||
| 126 | |||||||||||||||
| 127 | =head1 SUPPORT | ||||||||||||||
| 128 | |||||||||||||||
| 129 | You can find documentation for this module with the perldoc command. | ||||||||||||||
| 130 | |||||||||||||||
| 131 | perldoc HTTP::ClientDetect::Location | ||||||||||||||
| 132 | |||||||||||||||
| 133 | |||||||||||||||
| 134 | You can also look for information at: | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =over 4 | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||
| 139 | |||||||||||||||
| 140 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTTP-ClientDetect> | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 143 | |||||||||||||||
| 144 | L<http://annocpan.org/dist/HTTP-ClientDetect> | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =item * CPAN Ratings | ||||||||||||||
| 147 | |||||||||||||||
| 148 | L<http://cpanratings.perl.org/d/HTTP-ClientDetect> | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =item * Search CPAN | ||||||||||||||
| 151 | |||||||||||||||
| 152 | L<http://search.cpan.org/dist/HTTP-ClientDetect/> | ||||||||||||||
| 153 | |||||||||||||||
| 154 | =back | ||||||||||||||
| 155 | |||||||||||||||
| 156 | |||||||||||||||
| 157 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||
| 158 | |||||||||||||||
| 159 | |||||||||||||||
| 160 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 161 | |||||||||||||||
| 162 | Copyright 2013 Marco Pessotto. | ||||||||||||||
| 163 | |||||||||||||||
| 164 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 165 | under the terms of either: the GNU General Public License as published | ||||||||||||||
| 166 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||
| 167 | |||||||||||||||
| 168 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||
| 169 | |||||||||||||||
| 170 | |||||||||||||||
| 171 | =cut | ||||||||||||||
| 172 | |||||||||||||||
| 173 | 1 | 1; # End of HTTP::ClientDetect::Location | |||||||||||||