Info.pm

filter: Statementstmt 78.5% 44 / 56 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 2 / 2 totaltotal 79.0% 64 / 81 CC 5 SCAR 8.6
CC
5
Cov
75%
schema
6.5
ip_address
3.8
_sig
2.0
CRAP 2.4
« Info/UserAgent.pm ↑ Summary Info/Response/Location.pm »
1 package IP::Info;
2  
3 6 $IP::Info::VERSION = '0.17';
4 6 $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 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 ▶use IP::Info::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use IP::Info::Response::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use IP::Info::Response::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use IP::Info::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use IP::Info::UserAgent::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 6 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Data::Validate::IP qw(is_ipv4);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 extends 'IP::Info::UserAgent';
31  
32 2 ▶has 'base_url' => (is => 'ro', default => sub { 'https://api.sec.neustar.biz/ipi/gpp/v1/ipinfo' });
Subroutine __ANON__: called CC=1 SCAR=0.0
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<here|https://www.neustar.biz/lp/ip-intelligence/trial.php>.
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<IP::Info::Response> 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 ▶ my ($self, $ip) = @_;
Subroutine ip_address: called CC=3 SCAR=13.5
Pod documented
107  
108 3 ▶ die ("ERROR: Missing parameter IP Address.") unless defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 2 3
109 2 ▶ die ("ERROR: Invalid IP Address [$ip].") unless is_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ipv4($ip) 2 0 2
110  
111 0 my $url = sprintf("%s/%s?apikey=%s&sig=%s&format=json", $self->base_url(), $ip, $self->api_key, $self->_sig());
112 0 my $response = $self->get($url);
113 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 });
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 ▶ my ($self, $file) = @_;
Subroutine schema: called CC=3 SCAR=18.7
Pod documented
140  
141 1 ▶ die ("ERROR: Please supply the file name for the schema document.") unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 1 0 1
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 ▶ open(SCHEMA, ">$file") or die ("ERROR: Couldn't open file [$file] for writing: [$!]");
Branch: 1 branch
BranchTrueFalseTotal
open SCHEMA, ">$file" or die "ERROR: Couldn't open file [$file] for writing: [$!]" 0 0 0
147 0 print SCHEMA $response->{content};
148 0 close(SCHEMA);
149 }
150  
151 sub _sig {
152 0 ▶ my ($self) = @_;
Subroutine _sig: not called CC=1 SCAR=6.9
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<< <mohammad.anwar at yahoo.com> >>
161  
162 =head1 REPOSITORY
163  
164 L<https://github.com/manwar/IP-Info>
165  
166 =head1 BUGS
167  
168 Please report any bugs or feature requests to C<bug-ip-info at rt.cpan.org> or
169 through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IP-Info>.
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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IP-Info>
186  
187 =item * AnnoCPAN: Annotated CPAN documentation
188  
189 L<http://annocpan.org/dist/IP-Info>
190  
191 =item * CPAN Ratings
192  
193 L<http://cpanratings.perl.org/d/IP-Info>
194  
195 =item * Search CPAN
196  
197 L<http://search.cpan.org/dist/IP-Info/>
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<http://www.perlfoundation.org/artistic_license_2_0>
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 6 1; # End of IP::Info
« Info/UserAgent.pm ↑ Summary Info/Response/Location.pm »