| 1 | package Catalyst::TraitFor::Request::GeoIP; | ||
| 2 | our $VERSION = '0.01'; | ||
| 3 | |||
| 4 | # ABSTRACT: Geo lookups for Catalyst::Requests via Geo::IP | ||
| 5 | |||
| 6 | use Moose::Role; | ||
| 7 | use Geo::IP; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | has geoip_by_addr => (is => 'ro', isa => 'Maybe[Geo::IP::Record]', lazy => 1, builder => '_build_geoip_by_addr'); | ||
| 11 | |||
| 12 | has geoip => ( | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'Geo::IP', | ||
| 15 | lazy => 1, | ||
| 16 | default => sub { Geo::IP->open("/usr/local/share/GeoIP/GeoIPCity.dat", GEOIP_MEMORY_CACHE) }, | ||
| 17 | ); | ||
| 18 | |||
| 19 | requires 'address'; | ||
| 20 | |||
| 21 | sub _build_geoip_by_addr { | ||
| 22 | my ($self) = @_; | ||
| 23 | my $r = $self->geoip->record_by_addr($self->address); | ||
| 24 | return unless $r; | ||
| 25 | return $r; | ||
| 26 | } | ||
| 27 | |||
| 28 | 1; | ||
| 29 | |||
| 30 | |||
| 31 | __END__ |