| 1 | package Net::IPInfoDB; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # ---------------------------------------------------------------------- | ||||||||||||||
| 4 | # Net::IPInfoDB - fetch ip info form ipinfodb.com | ||||||||||||||
| 5 | # ---------------------------------------------------------------------- | ||||||||||||||
| 6 | |||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use vars qw($VERSION $DEBUG @FIELDS); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use vars qw($API_URI $API_VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use vars qw($WEB_URI); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | ▶ | use LWP::Simple qw(get); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | $VERSION = '3.0'; | |||||||||||||
| 16 | 1 | ▶ | $DEBUG = 0 unless defined $DEBUG; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | $API_URI = 'http://api.ipinfodb.com'; | |||||||||||||
| 19 | 1 | $API_VERSION = 'v3'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | $WEB_URI = 'http://ipinfodb.com/ip_locator.php?ip='; | |||||||||||||
| 22 | 1 | @FIELDS = qw(status_code status_message ip_address country_code country_name | |||||||||||||
| 23 | region_name city_name zip_code latitude longitude timezone); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 26 | 0 | my $class = shift; | |||||||||||||
| 27 | |||||||||||||||
| 28 | 0 | my $self = bless { | |||||||||||||
| 29 | _KEY => '', | ||||||||||||||
| 30 | _ERROR => '', | ||||||||||||||
| 31 | } => $class; | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | ▶ | if (@_) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 34 | 0 | $self->key($_[0]); | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | elsif (my $k = $ENV{'IPINFODB_TOKEN'}) { | ||||||||||||||
| 37 | 0 | $self->key($k); | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 0 | return $self; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 0 | ▶ | sub key { | ||||||||||||
|
Subroutine
key: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 44 | 0 | my $self = shift; | |||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 0 | $self->{ _KEY } = $_[0]; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | return $self->{ _KEY }; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | ▶ | sub error { | ||||||||||||
|
Subroutine
error: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 54 | 0 | my $self = shift; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 0 | $self->{'_ERROR'} = "@_"; | |||||||||||||
| 58 | 0 | return; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | return $self->{'_ERROR'}; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 0 | ▶ | sub get_city { | ||||||||||||
|
Subroutine
get_city: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 65 | 0 | my $self = shift; | |||||||||||||
| 66 | 0 | my $host = shift; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | return $self->_get('ip-city', $host); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | ▶ | sub get_country { | ||||||||||||
|
Subroutine
get_country: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||||
| 73 | 0 | my $host = shift; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | return $self->_get('ip-country', $host); | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 0 | ▶ | sub _get { | ||||||||||||
|
Subroutine
_get: not called CC=4 SCAR=30.0
| |||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||
| 80 | 0 | my $meth = shift; | |||||||||||||
| 81 | 0 | my $addr = shift; | |||||||||||||
| 82 | 0 | my $raw; | |||||||||||||
| 83 | 0 | my $res = Net::IPInfoDB::Result->new; | |||||||||||||
| 84 | |||||||||||||||
| 85 | 0 | my $uri = URI->new("$API_URI/$API_VERSION/$meth"); | |||||||||||||
| 86 | 0 | $uri->query_form( | |||||||||||||
| 87 | format => "raw", | ||||||||||||||
| 88 | key => $self->key, | ||||||||||||||
| 89 | ip => $addr, | ||||||||||||||
| 90 | ); | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | ▶ | if ($raw = get($uri)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | my @f = split /;/, $raw; | |||||||||||||
| 94 | 0 | ▶ | for (my $i = 0; $i < @f; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 0 | my $m = $FIELDS[ $i ]; | |||||||||||||
| 96 | 0 | $res->$m($f[ $i ]); | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | ▶ | if ($res->status_code ne "OK") { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | 0 | return $self->error($res->status_message); | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 0 | $res->web_uri("$WEB_URI$addr"); | |||||||||||||
| 106 | 0 | $res->_raw($raw); | |||||||||||||
| 107 | |||||||||||||||
| 108 | 0 | return $res; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | package Net::IPInfoDB::Result; | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 1 | ▶ | use Class::Struct; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 114 | |||||||||||||||
| 115 | struct( | ||||||||||||||
| 116 | 1 | (map { $_ => '$' } @Net::IPInfoDB::FIELDS), | |||||||||||||
| 117 | 'web_uri' => '$', | ||||||||||||||
| 118 | '_raw' => '$', | ||||||||||||||
| 119 | ); | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | ▶ | sub fields { | ||||||||||||
|
Subroutine
fields: not called CC=1 SCAR=6.9
| |||||||||||||||
| 122 | 0 | return @Net::IPInfoDB::FIELDS; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 1 | 1; | |||||||||||||
| 126 | |||||||||||||||
| 127 | __END__ | ||||||||||||||