| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::GeoIPify; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 216063 | use namespace::clean; | 
|  | 4 |  |  |  |  | 64470 |  | 
|  | 4 |  |  |  |  | 29 |  | 
| 4 | 4 |  |  | 4 |  | 2944 | use strictures 2; | 
|  | 4 |  |  |  |  | 6399 |  | 
|  | 4 |  |  |  |  | 151 |  | 
| 5 | 4 |  |  | 4 |  | 805 | use utf8; | 
|  | 4 |  |  |  |  | 12 |  | 
|  | 4 |  |  |  |  | 30 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 4 |  |  | 4 |  | 137 | use Carp qw(croak); | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 263 |  | 
| 8 | 4 |  |  | 4 |  | 2131 | use Data::Validate::IP qw(is_ipv4 is_public_ipv4); | 
|  | 4 |  |  |  |  | 153295 |  | 
|  | 4 |  |  |  |  | 344 |  | 
| 9 | 4 |  |  | 4 |  | 2281 | use Moo; | 
|  | 4 |  |  |  |  | 28459 |  | 
|  | 4 |  |  |  |  | 22 |  | 
| 10 | 4 |  |  | 4 |  | 7921 | use Sub::Quote qw(quote_sub); | 
|  | 4 |  |  |  |  | 19396 |  | 
|  | 4 |  |  |  |  | 260 |  | 
| 11 | 4 |  |  | 4 |  | 1882 | use Types::Common::String qw(StrLength); | 
|  | 4 |  |  |  |  | 507278 |  | 
|  | 4 |  |  |  |  | 58 |  | 
| 12 | 4 |  |  | 4 |  | 2486 | use Types::Standard qw(InstanceOf Str); | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 26 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | with 'Role::Cache::LRU'; | 
| 15 |  |  |  |  |  |  | with 'Role::REST::Client'; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | our $VERSION = '0.04'; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | has api_key => ( | 
| 20 |  |  |  |  |  |  | isa => StrLength[32], | 
| 21 |  |  |  |  |  |  | is => 'rw', | 
| 22 |  |  |  |  |  |  | required => 1, | 
| 23 |  |  |  |  |  |  | ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | has api_url => ( | 
| 26 |  |  |  |  |  |  | isa => Str, | 
| 27 |  |  |  |  |  |  | is => 'ro', | 
| 28 |  |  |  |  |  |  | default => quote_sub(q{ 'https://geo.ipify.org/api/v1' }), | 
| 29 |  |  |  |  |  |  | ); | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | has api_ipify_url => ( | 
| 32 |  |  |  |  |  |  | isa => Str, | 
| 33 |  |  |  |  |  |  | is => 'ro', | 
| 34 |  |  |  |  |  |  | default => quote_sub(q{ 'https://api.ipify.org' }), | 
| 35 |  |  |  |  |  |  | ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub lookup { | 
| 38 | 0 |  |  | 0 | 1 |  | my ($self, $ip) = @_; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 | 0 |  |  |  |  | croak "$ip is not a public IPv4 address" if (!is_public_ipv4($ip)); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  |  | my $cached_ip_record = $self->get_cache($ip); | 
| 43 | 0 | 0 |  |  |  |  | return $cached_ip_record if (defined $cached_ip_record); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 |  |  |  |  |  | $self->set_persistent_header( | 
| 46 |  |  |  |  |  |  | 'User-Agent' => __PACKAGE__ . $WebService::GeoIPify::VERSION); | 
| 47 | 0 |  |  |  |  |  | $self->server($self->api_url); | 
| 48 | 0 |  |  |  |  |  | $self->type(q|application/json|); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | my $queries = { | 
| 51 |  |  |  |  |  |  | apiKey => $self->api_key, | 
| 52 |  |  |  |  |  |  | ipAddress => $ip, | 
| 53 |  |  |  |  |  |  | }; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | my $response = $self->get('', $queries); | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 |  |  |  |  |  | my $ip_record = $response->data; | 
| 58 | 0 |  |  |  |  |  | $self->set_cache($ip => $ip_record); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  |  | return $ip_record; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub check { | 
| 64 | 0 |  |  | 0 | 1 |  | my ($self) = @_; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | my $ip = $self->get($self->api_ipify_url)->data; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 | 0 |  |  |  |  | croak q|Cannot obtain client's public IPv4 address| if (!is_ipv4($ip)); | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | return $self->lookup($ip); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | 1; | 
| 74 |  |  |  |  |  |  | __END__ | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =encoding utf-8 | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =for stopwords geoipify geolocation ipify ipv4 | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =head1 NAME | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | WebService::GeoIPify - Perl library for ipify's Geolocation API, | 
| 83 |  |  |  |  |  |  | https://geo.ipify.org and https://ipify.org. | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | use WebService::GeoIPify; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | my $geoipify = WebService::GeoIPify->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx32'); | 
| 90 |  |  |  |  |  |  | print $geoipify->lookup('8.8.8.8'); | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | WebService::GeoIPify is a Perl library for obtaining Geolocation information on | 
| 95 |  |  |  |  |  |  | IPv4 address. | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =head1 DEVELOPMENT | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | Source repository at L<https://github.com/kianmeng/webservice-geoipify|https://github.com/kianmeng/webservice-geoipify>. | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | How to contribute? Follow through the L<CONTRIBUTING.md|https://github.com/kianmeng/webservice-geoipify/blob/master/CONTRIBUTING.md> document to setup your development environment. | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =head1 METHODS | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =head2 new($api_key) | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | Construct a new WebService::GeoIPify instance. | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | my $geoipify = WebService::GeoIPify->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx32'); | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =head3 api_key | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | Compulsory. The API access key used to make request through web service. | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =head3 api_url | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | The default base URL for API calls. | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head3 api_ipify_url | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | The default base URL for ipify API calls to obtain the client public IP. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =head2 lookup($ip_address) | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | Query and get an IP address information. Only accept IPv4 public address. | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | my $geoipify = WebService::GeoIPify->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx32'); | 
| 128 |  |  |  |  |  |  | print $geoipify->lookup('8.8.8.8'); | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head2 check() | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | Look up the public IP address of the client which made the web service call. | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | my $geoipify = WebService::GeoIPify->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx32'); | 
| 135 |  |  |  |  |  |  | print $geoipify->check(); | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =head1 AUTHOR | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | Kian Meng, Ang E<lt>kianmeng@cpan.orgE<gt> | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | This software is Copyright (c) 2019 Kian Meng, Ang. | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | This is free software, licensed under: | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | The Artistic License 2.0 (GPL Compatible) | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =cut |