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