Skip to content

Commit

Permalink
added info_v6 method and updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
abu-usama committed Jan 9, 2024
1 parent 28e3291 commit c62337d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
55 changes: 40 additions & 15 deletions Geo-IPinfo/lib/Geo/IPinfo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,8 @@ sub new {
my $timeout =
defined $options{timeout} ? $options{timeout} : DEFAULT_TIMEOUT;
$self->{ua}->timeout($timeout);
$self->{ipv6_lookup} = defined $options{ipv6_lookup}
? $options{ipv6_lookup} : 0;

$self->{message} = '';

Expand Down Expand Up @@ -1115,15 +1117,23 @@ sub new {
sub info {
my ( $self, $ip ) = @_;

return $self->_get_info( $ip, '' );
return $self->_get_info( $ip, '', $self->{ipv6_lookup} );
}

#-------------------------------------------------------------------------------

sub info_v6 {
my ( $self, $ip ) = @_;

return $self->_get_info( $ip, '', 1 );
}

#-------------------------------------------------------------------------------

sub geo {
my ( $self, $ip ) = @_;

return $self->_get_info( $ip, 'geo' );
return $self->_get_info( $ip, 'geo', $self->{ipv6_lookup} );
}

#-------------------------------------------------------------------------------
Expand All @@ -1141,7 +1151,7 @@ sub field {
return;
}

return $self->_get_info( $ip, $field );
return $self->_get_info( $ip, $field, $self->{ipv6_lookup} );
}

#-------------------------------------------------------------------------------
Expand All @@ -1156,7 +1166,7 @@ sub error_msg {
#-- private method(s) below, don't call them directly -------------------------

sub _get_info {
my ( $self, $ip, $field ) = @_;
my ( $self, $ip, $field, $ipv6_lookup ) = @_;

$ip = defined $ip ? $ip : '';
$field = defined $field ? $field : '';
Expand All @@ -1169,7 +1179,7 @@ sub _get_info {
}
}

my ( $info, $message ) = $self->_lookup_info( $ip, $field );
my ( $info, $message ) = $self->_lookup_info( $ip, $field, $ipv6_lookup );
$self->{message} = $message;

if ( $field ne '' && ref($info) eq 'HASH' ) {
Expand All @@ -1183,14 +1193,16 @@ sub _get_info {
}

sub _lookup_info {
my ( $self, $ip, $field ) = @_;
my ( $self, $ip, $field, $ipv6_lookup ) = @_;

# checking bogon IP and returning response locally.
if ( _is_bogon($ip) ) {
my $details = {};
$details->{ip} = $ip;
$details->{bogon} = "True";
return ( $details, '' );
if ( $ip ne '' ) {
if ( _is_bogon($ip) ) {
my $details = {};
$details->{ip} = $ip;
$details->{bogon} = "True";
return ( $details, '' );
}
}

my $key = $ip . '/' . $field;
Expand All @@ -1201,7 +1213,7 @@ sub _lookup_info {
}

my $is_ipv6 = 0;
$is_ipv6 = 1 if ( $ip =~ /:/ );
$is_ipv6 = 1 if ( $ip =~ /:/ || $ipv6_lookup);
my ( $source_info, $message ) = $self->_lookup_info_from_source($is_ipv6, $key);
if ( not defined $source_info ) {
return ( $source_info, $message );
Expand Down Expand Up @@ -1258,7 +1270,7 @@ sub _lookup_info_from_cache {

sub _lookup_info_from_source {
my ( $self, $is_ipv6, $key ) = @_;

my $url = '';
if ( $is_ipv6 ) {
$url = $self->{base_url_ipv6} . $key;
Expand Down Expand Up @@ -1398,8 +1410,13 @@ A quick usage example:
$ip_address = '216.239.36.21';
$details = $ipinfo->info($ip_address);
$city = $details->city; # Emeryville
$loc = $details->loc; # 37.8342,-122.2900
$city = $details->city; # Mountain View
$loc = $details->loc; # 37.4056,-122.0775
$ip_address = '2001:4860:4860::8888';
$details = $ipinfo->info($ip_address);
$city = $details->city; # Mountain View
$loc = $details->loc; # 37.4056,-122.0775
=head1 SUBROUTINES/METHODS
Expand All @@ -1422,6 +1439,14 @@ of errors, returns undef, the error message can be retrieved with the function '
The values can be accessed with the named methods: ip, org, domains, privacy, abuse, timezone, hostname, city, country, country_name, country_flag,
country_flag_url, country_currency, continent, is_eu, loc, latitude, longitude, postal, asn, company, meta, carrier, and all.
=head2 info_v6(ip_address)
Returns a reference to a Details object containing all information related to the IP address. In case
of errors, returns undef, the error message can be retrieved with the function 'error_msg()'
The values can be accessed with the named methods: ip, org, domains, privacy, abuse, timezone, hostname, city, country, country_name, country_flag,
country_flag_url, country_currency, continent, is_eu, loc, latitude, longitude, postal, asn, company, meta, carrier, and all.
=head2 geo(ip_address)
Returns a reference to an object containing only the geolocation related data. Returns undef
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ $loc = $details->loc; # 37.8342,-122.2900

#### Usage

The `Geo::IPinfo->info()` method accepts an IP address as an optional, positional argument. If no IP address is specified, the API will return data for the IP address from which it receives the request.
The `Geo::IPinfo->info()` method accepts an IPv4 address as an optional, positional argument. If no IP address is specified, the API will return data for the IP address from which it receives the request. The `Geo::IPinfo->info_v6()` method works in a similar fashion but for IPv6 addresses. Both methods can also be set to always do IPv6 lookups by setting `ipv6_lookup" => 1` while initializing the client.

```perl
use Geo::IPinfo;

$access_token = '123456789abc';
$ipinfo = Geo::IPinfo->new($access_token);
$details = $ipinfo->info($ip_address);
# for IPv6
# $details = $ipinfo->info_v6($ip_address);

if (defined $details) # valid data returned
{
Expand All @@ -90,7 +92,7 @@ $ipinfo = Geo::IPinfo->new($access_token);

#### Details Data

`Geo::IPinfo->info()` will return a `Details` object that contains all fields listed in the [IPinfo developer docs](https://ipinfo.io/developers/responses#full-response) with a few minor additions. Properties can be accessed through methods of the same name.
`Geo::IPinfo->info()` and `Geo::IPinfo->info_v6()` will return a `Details` object that contains all fields listed in the [IPinfo developer docs](https://ipinfo.io/developers/responses#full-response) with a few minor additions. Properties can be accessed through methods of the same name.

```perl
$hostname = $details->hostname; # cpe-104-175-221-247.socal.res.rr.com
Expand Down Expand Up @@ -183,17 +185,18 @@ $ipinfo = Geo::IPinfo->new($token, ("cache" => $my_custom_cache));


### Request options
The request timeout period can be set in the `%options` parameter.
The request lookup type and timeout period can be set in the `%options` parameter.

* Default request timeout: 2 seconds
* Default lookup type: IPv4

```perl
$ipinfo = Geo::IPinfo->new($token, ("timeout" => 5));
$ipinfo = Geo::IPinfo->new($token, ("timeout" => 5, "ipv6_lookup" => 1));
```

#### Internationalization

When looking up an IP address, the `$details` object includes a `$details->country_name` method which includes the country name based on American English, `$details->is_eu` method which returns `true` if the country is a member of the European Union (EU) else `undef`, `$details->country_flag` method which returns a dictionary of emoji and Unicode of the country's flag, `$details->country_flag_url` will return a public link to the country's flag image as an SVG which can be used anywhere and `$details->country_currency` method which returns a dictionary of code, the symbol of a country's currency and `$details->continent` which includes code and name of the continent. It is possible to return the country name in other languages, change the EU countries, countries flags, countries' currencies, and continents by setting the `countries`, `eu_countries`, `countries_flags`, `countries_currencies` and `continents` settings when creating the `IPinfo` object. The `countries`, `countries_flags`, `countries_currencies`, and `continents` are hashes while `eu_countries` is an array.
When looking up an IP address, the `$details` object includes a `$details->country_name` method which includes the country name based on American English, `$details->is_eu` method which returns `true` if the country is a member of the European Union (EU) else `undef`, `$details->country_flag` method which returns a dictionary of emoji and Unicode of the country's flag, `$details->country_flag_url` will return a public link to the country's flag image as an SVG which can be used anywhere and `$details->country_currency` method which returns a dictionary of code, the symbol of a country's currency and `$details->continent` which includes code and name of the continent. It is possible to return the country name in other languages, change the EU countries, countries flags, countries' currencies, and continents by setting the `countries`, `eu_countries`, `countries_flags`, `countries_currencies` and `continents` settings when creating the `IPinfo` object. The `countries`, `countries_flags`, `countries_currencies`, and `continents` are hashes while `eu_countries` is an array.

```perl
my %custom_countries = (
Expand Down
4 changes: 2 additions & 2 deletions example.pl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
print $ipinfo->error_msg . "\n";
}

# you can also retrieve information for IPv6 addresses in a similar fasion
my $ipv6_data = $ipinfo->info('2001:4860:4860::8888');
# you can also retrieve information for IPv6 addresses in a similar fashion
my $ipv6_data = $ipinfo->info_v6('2001:4860:4860::8888');
if ( defined $ipv6_data ) # valid data returned
{
# print JSON string
Expand Down

0 comments on commit c62337d

Please sign in to comment.