File Coverage

blib/lib/WebService/IPStack.pm
Criterion Covered Total %
statement 21 42 50.0
branch 0 10 0.0
condition 0 6 0.0
subroutine 7 11 63.6
pod 3 3 100.0
total 31 72 43.0


line stmt bran cond sub pod time code
1             package WebService::IPStack;
2              
3             our $VERSION = '0.01';
4              
5 5     5   271733 use namespace::clean;
  5         76108  
  5         35  
6 5     5   3201 use strictures 2;
  5         7121  
  5         173  
7 5     5   885 use utf8;
  5         12  
  5         35  
8              
9 5     5   167 use Carp qw(confess);
  5         9  
  5         237  
10 5     5   2386 use Moo;
  5         30222  
  5         25  
11 5     5   8592 use MooX::Enumeration;
  5         9489  
  5         23  
12 5     5   2949 use Types::Standard qw(Str Enum);
  5         339266  
  5         61  
13              
14             with 'Role::REST::Client';
15              
16             has api_key => (
17             isa => sub {
18             confess "API key must be length of 32 characters!" if (length $_[0] != 32);
19             },
20             is => 'rw',
21             required => 1
22             );
23              
24             has api_plan => (
25             is => 'rw',
26             isa => Enum[qw(free basic pro pro_plus)],
27             handles => [qw/ is_free is_basic is_pro is_pro_plus /],
28             default => sub { 'free' },
29             );
30              
31             has api_url => (
32             isa => Str,
33             is => 'ro',
34             default => sub {
35             my $protocol = (shift->is_free) ? 'http' : 'https';
36             return qq|$protocol://api.ipstack.com/|
37             },
38             );
39              
40             sub lookup {
41 0     0 1   my ($self, $ip, $params) = @_;
42              
43 0           return $self->_request($ip, $params);
44             }
45              
46             sub bulk_lookup {
47 0     0 1   my ($self, $ips, $params) = @_;
48              
49 0 0         confess "Expect an array of IP address" if (ref $ips ne 'ARRAY');
50              
51 0 0 0       if (!$self->is_pro || !$self->is_pro_plus) {
52 0           confess "Bulk IP lookup only for Business or Business Pro subscription plan"
53             }
54              
55 0           my $endpoint = join(",", $ips);
56 0           return $self->_request($endpoint, $params);
57             }
58              
59             sub check {
60 0     0 1   my ($self, $params) = @_;
61              
62 0           return $self->_request('check', $params);
63             }
64              
65             sub _request {
66 0     0     my ($self, $endpoint, $params) = @_;
67              
68 0 0 0       if (exists($params->{security}) && !$self->is_business_pro) {
69 0           confess "Security data only for Business Pro subscription plan"
70             }
71              
72 0 0         my $format = exists($params->{output}) ? $params->{output} : 'json';
73              
74 0           $self->set_persistent_header('User-Agent' => __PACKAGE__ . $WebService::IPStack::VERSION);
75 0           $self->server($self->api_url);
76 0           $self->type(qq|application/$format|);
77              
78 0           my $queries = {
79             access_key => $self->api_key
80             };
81 0 0         $queries = {%$queries, %$params} if (defined $params);
82              
83 0           my $response = $self->get($endpoint, $queries);
84              
85 0           return $response->data;
86             }
87              
88             1;
89             __END__
90              
91             =encoding utf-8
92              
93             =head1 NAME
94              
95             WebService::IPStack - Perl library for using IPStack, https://ipstack.com.
96              
97             =head1 SYNOPSIS
98              
99             use WebService::IPStack;
100              
101             my $ipstack = WebService::IPStack->new(api_key => 'foobar');
102             $ipstack->query('8.8.8.8');
103              
104             # Only for Pro plan.
105             $ipstack->query(['8.8.8.8', '8.8.4.4']);
106              
107             =head1 DESCRIPTION
108              
109             WebService::IPStack is a Perl library for obtaining information on IPv4 or IPv6
110             address.
111              
112             =head1 DEVELOPMENT
113              
114             Source repo at L<https://github.com/kianmeng/webservice-ipstack|https://github.com/kianmeng/webservice-ipstack>.
115              
116             How to contribute? Follow through the L<CONTRIBUTING.md|https://github.com/kianmeng/webservice-ipstack/blob/master/CONTRIBUTING.md> document to setup your development environment.
117              
118             =head1 METHODS
119              
120             =head2 new($api_key, [$api_plan])
121              
122             Construct a new WebService::IPStack instance.
123              
124             =head3 api_key
125              
126             Compulsory. The API access key used to make request through web service.
127              
128             =head3 api_plan
129              
130             Optional. The API subscription plan used when accessing the API. There are four
131             subscription plans: free, standard, pro, and pro_plus. By default, the
132             subscription plan is 'free'. The main difference between free and non-free
133             subscription plans are HTTPS encryption protocol support and additional
134             information.
135              
136             # The API request URL is http://api.ipstack.com/
137             my $ipstack = WebService::IPStack->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx32');
138             print $ipstack->api_url;
139              
140             # The API request URL is https://api.ipstack.com/
141             my $ipstack = WebService::IPStack->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx32', api_plan => 'paid');
142             print $ipstack->api_url;
143              
144             =head3 api_url
145              
146             The default API hostname and path. The protocol depends on the subscription plan.
147              
148             =head2 lookup($ip_address, [%params])
149              
150             Query and get an IP address information. Optionally you can add more settings
151             to adjust the output.
152              
153             my $ipstack = WebService::IPStack->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx32');
154             $ipstack->query('8.8.8.8');
155              
156             # With optional parameters.
157             $ipstack->query('8.8.8.8', {hostname => 1, security => 1, output => 'xml'});
158              
159             =head2 bulk_lookup($ip_address, [%params])
160              
161             Only for Paid subscription plan. Query and get multiple IP addresses
162             information. Optionally you can add more settings to adjust the output.
163              
164             my $ipstack = WebService::IPStack->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx32', api_plan => 'paid');
165             $ipstack->query(['8.8.8.8', '8.8.4.4']);
166              
167             # With optional parameters.
168             $ipstack->query(['8.8.8.8', '8.8.4.4'], {language => 'zh'});
169              
170             =head2 check([%params])
171              
172             Look up the IP address details of the client which made the web service call.
173             Optionally you can add more settings to adjust the output.
174              
175             my $ipstack = WebService::IPStack->new(api_key => '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx32');
176             $ipstack->check();
177              
178             # With optional parameters.
179             $ipstack->check({hostname => 1, security => 1, output => xml});
180              
181             =head1 COPYRIGHT AND LICENSE
182              
183             This software is Copyright (c) 2019 Kian Meng, Ang.
184              
185             This is free software, licensed under:
186              
187             The Artistic License 2.0 (GPL Compatible)
188              
189             =head1 AUTHOR
190              
191             Kian Meng, Ang E<lt>kianmeng@users.noreply.github.comE<gt>
192              
193             =cut