File Coverage

blib/lib/IP/Info.pm
Criterion Covered Total %
statement 38 50 76.0
branch 4 8 50.0
condition n/a
subroutine 13 14 92.8
pod 2 2 100.0
total 57 74 77.0


line stmt bran cond sub pod time code
1             package IP::Info;
2              
3             $IP::Info::VERSION = '0.17';
4             $IP::Info::AUTHOR = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             IP::Info - Interface to IP geographic and network data.
9              
10             =head1 VERSION
11              
12             Version 0.17
13              
14             =cut
15              
16 6     6   20039 use JSON;
  6         65097  
  6         26  
17 6     6   3837 use Data::Dumper;
  6         31502  
  6         351  
18              
19 6     6   1988 use IP::Info::Response;
  6         21  
  6         176  
20 6     6   2343 use IP::Info::Response::Network;
  6         16  
  6         191  
21 6     6   2253 use IP::Info::Response::Location;
  6         22  
  6         274  
22 6     6   2770 use IP::Info::UserAgent;
  6         19  
  6         175  
23 6     6   37 use IP::Info::UserAgent::Exception;
  6         11  
  6         111  
24              
25 6     6   27 use Digest::MD5 qw(md5_hex);
  6         10  
  6         286  
26 6     6   2857 use Data::Validate::IP qw(is_ipv4);
  6         145369  
  6         424  
27              
28 6     6   45 use Moo;
  6         14  
  6         46  
29 6     6   1988 use namespace::clean;
  6         13  
  6         43  
30             extends 'IP::Info::UserAgent';
31              
32             has 'base_url' => (is => 'ro', default => sub { 'https://api.sec.neustar.biz/ipi/gpp/v1/ipinfo' });
33              
34             =head1 DESCRIPTION
35              
36             Neustar IP Intelligence RESTful API provides the geographic location and network
37             data for any Internet Protocol address in the public address space.
38             The information includes:
39              
40             =over 5
41              
42             =item * Postal code, city, state, region, country, and continent
43              
44             =item * Area code (US and Canada only) and time zone
45              
46             =item * Longitude and latitude
47              
48             =item * DMA (Designated Market Area) and MSA (Metropolitan Statistical Area)
49              
50             =item * Network information, including type, speed, carrier, and registering
51             organization
52              
53             =back
54              
55             To obtain "Free Developer Trial" API key and the shared secret, register your application L.
56              
57             =head1 CONSTRUCTOR
58              
59             The constructor requires the following parameters as listed below:
60              
61             +---------+----------+----------------------------------------+
62             | Key | Required | Description |
63             +---------+----------+----------------------------------------+
64             | api_key | Yes | API Key given by Quova. |
65             | secret | Yes | Allocated share secret given by Quova. |
66             +---------+----------+----------------------------------------+
67              
68             use strict; use warnings;
69             use IP::Info;
70              
71             my $api_key = 'Your_API_Key';
72             my $secret = 'Your_shared_secret';
73             my $info = IP::Info->new({ api_key => $api_key, secret => $secret });
74              
75             =head1 METHODS
76              
77             =head2 ip_address($ip_address)
78              
79             If an IP address is specified in the correct format, then the call returns an
80             object of type L object which can be queried further to look
81             for specific information for that IP. In case it encounters any error it'll throw
82             an exception.
83              
84             =over 2
85              
86             =item * dot-decimal e.g. 4.2.2.2
87              
88             =item * decimal notation e.g. 67240450
89              
90             =back
91              
92             use strict; use warnings;
93             use IP::Info;
94              
95             my $api_key = 'Your_API_Key';
96             my $secret = 'Your_shared_secret';
97             my $info = IP::Info->new({ api_key => $api_key, secret => $secret });
98             my $response = $info->ip_address('4.2.2.2');
99              
100             print "Carrier: ", $response->network->carrier , "\n";
101             print "Country: ", $response->location->country, "\n";
102              
103             =cut
104              
105             sub ip_address {
106 3     3 1 1522 my ($self, $ip) = @_;
107              
108 3 100       19 die ("ERROR: Missing parameter IP Address.") unless defined $ip;
109 2 50       9 die ("ERROR: Invalid IP Address [$ip].") unless is_ipv4($ip);
110              
111 0         0 my $url = sprintf("%s/%s?apikey=%s&sig=%s&format=json", $self->base_url(), $ip, $self->api_key, $self->_sig());
112 0         0 my $response = $self->get($url);
113 0         0 my $content = from_json($response->{content});
114              
115             return IP::Info::Response->new({
116             ip_address => $content->{ipinfo}->{ip_address},
117             ip_type => $content->{ipinfo}->{ip_type},
118             network => IP::Info::Response::Network->new($content->{ipinfo}->{Network}),
119             location => IP::Info::Response::Location->new($content->{ipinfo}->{Location})
120 0         0 });
121             }
122              
123             =head2 schema($file_name)
124              
125             Saves the XML Schema Document in the given file (.xsd file).In case it encounters
126             any error it will throw an exception.
127              
128             use strict; use warnings;
129             use IP::Info;
130              
131             my $api_key = 'Your_API_Key';
132             my $secret = 'Your_shared_secret';
133             my $info = IP::Info->new({ api_key => $api_key, secret => $secret });
134             $info->schema('User_supplied_filename.xsd');
135              
136             =cut
137              
138             sub schema {
139 1     1 1 93 my ($self, $file) = @_;
140              
141 1 50       11 die ("ERROR: Please supply the file name for the schema document.") unless defined $file;
142              
143 0           my $url = sprintf("%s/schema?apikey=%s&sig=%s", $self->base_url(), $self->api_key, $self->_sig());
144 0           my $response = $self->get($url);
145              
146 0 0         open(SCHEMA, ">$file") or die ("ERROR: Couldn't open file [$file] for writing: [$!]");
147 0           print SCHEMA $response->{content};
148 0           close(SCHEMA);
149             }
150              
151             sub _sig {
152 0     0     my ($self) = @_;
153              
154 0           my $time = time;
155 0           return md5_hex($self->api_key . $self->secret . $time);
156             }
157              
158             =head1 AUTHOR
159              
160             Mohammad S Anwar, C<< >>
161              
162             =head1 REPOSITORY
163              
164             L
165              
166             =head1 BUGS
167              
168             Please report any bugs or feature requests to C or
169             through the web interface at L.
170             I will be notified and then you'll automatically be notified of progress on your
171             bug as I make changes.
172              
173             =head1 SUPPORT
174              
175             You can find documentation for this module with the perldoc command.
176              
177             perldoc IP::Info
178              
179             You can also look for information at:
180              
181             =over 4
182              
183             =item * RT: CPAN's request tracker
184              
185             L
186              
187             =item * AnnoCPAN: Annotated CPAN documentation
188              
189             L
190              
191             =item * CPAN Ratings
192              
193             L
194              
195             =item * Search CPAN
196              
197             L
198              
199             =back
200              
201             =head1 LICENSE AND COPYRIGHT
202              
203             Copyright (C) 2011 - 2016 Mohammad S Anwar.
204              
205             This program is free software; you can redistribute it and/or modify it under
206             the terms of the the Artistic License (2.0). You may obtain a copy of the full
207             license at:
208              
209             L
210              
211             Any use, modification, and distribution of the Standard or Modified Versions is
212             governed by this Artistic License.By using, modifying or distributing the Package,
213             you accept this license. Do not use, modify, or distribute the Package, if you do
214             not accept this license.
215              
216             If your Modified Version has been derived from a Modified Version made by someone
217             other than you,you are nevertheless required to ensure that your Modified Version
218             complies with the requirements of this license.
219              
220             This license does not grant you the right to use any trademark, service mark,
221             tradename, or logo of the Copyright Holder.
222              
223             This license includes the non-exclusive, worldwide, free-of-charge patent license
224             to make, have made, use, offer to sell, sell, import and otherwise transfer the
225             Package with respect to any patent claims licensable by the Copyright Holder that
226             are necessarily infringed by the Package. If you institute patent litigation
227             (including a cross-claim or counterclaim) against any party alleging that the
228             Package constitutes direct or contributory patent infringement,then this Artistic
229             License to you shall terminate on the date that such litigation is filed.
230              
231             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
232             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
233             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
234             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
235             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
236             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
237             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
238              
239             =cut
240              
241             1; # End of IP::Info