File Coverage

blib/lib/IO/IPFinder.pm
Criterion Covered Total %
statement 80 111 72.0
branch 2 8 25.0
condition n/a
subroutine 27 32 84.3
pod 1 11 9.0
total 110 162 67.9


line stmt bran cond sub pod time code
1             package IO::IPFinder;
2              
3 1     1   71323 use 5.026001;
  1         12  
4 1     1   5 use strict;
  1         2  
  1         20  
5 1     1   5 use warnings;
  1         2  
  1         39  
6 1     1   709 use JSON qw(encode_json decode_json);
  1         12381  
  1         6  
7 1     1   578 use URI::Escape; # FOR uri_escape($val)
  1         1312  
  1         59  
8 1     1   653 use LWP::UserAgent; # call
  1         44530  
  1         38  
9 1     1   525 use IO::Info; # FOR print
  1         2  
  1         33  
10 1     1   452 use IO::Validation::Asnvalidation;
  1         4  
  1         30  
11 1     1   412 use IO::Validation::Domainvalidation;
  1         2  
  1         31  
12 1     1   380 use IO::Validation::Firewallvalidation;
  1         3  
  1         32  
13 1     1   396 use IO::Validation::Ipvalidation;
  1         3  
  1         33  
14 1     1   397 use IO::Validation::Tokenvalidation;
  1         3  
  1         42  
15              
16             our $VERSION = '1.0';
17              
18 1     1   5 use constant DEFAULT_BASE_URL => 'https://api.ipfinder.io/v1/';
  1         2  
  1         57  
19              
20 1     1   6 use constant DEFAULT_API_TOKEN => 'free';
  1         35  
  1         55  
21              
22 1     1   6 use constant FORMAT => 'json';
  1         2  
  1         62  
23              
24 1     1   7 use constant STATUS_PATH => 'info';
  1         2  
  1         49  
25              
26 1     1   5 use constant RANGES_PATH => 'ranges/';
  1         1  
  1         63  
27              
28 1     1   6 use constant FIREWALL_PATH => 'firewall/';
  1         2  
  1         54  
29              
30 1     1   6 use constant DOMAIN_PATH => 'domain/';
  1         2  
  1         44  
31              
32 1     1   5 use constant DOMAIN_H_PATH => 'domainhistory/';
  1         2  
  1         51  
33              
34 1     1   6 use constant DOMAIN_BY_PATH => 'domainby/';
  1         2  
  1         792  
35              
36              
37              
38             # Items to export into callers namespace by default. Note: do not export
39             # names by default without a very good reason. Use EXPORT_OK instead.
40             # Do not simply export all your public functions/methods/constants.
41              
42             # This allows declaration use IO::IPFinder ':all';
43             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
44             # will save memory.
45              
46             # Preloaded methods go here.
47              
48              
49             #-------------------------------------------------------------------------------
50             #-------------------------------------------------------------------------------
51             sub new {
52 1     1 0 85 my ($class, $token, $baseUrl) = @_;
53              
54 1         4 my $self = {};
55              
56              
57              
58 1 50       4 if (defined($token)) {
59 0         0 IO::Validation::Tokenvalidation->validate($token);
60 0         0 $self->{token} = $token;
61             }
62             else {
63 1         3 $self->{token} = DEFAULT_API_TOKEN;
64             }
65              
66 1 50       4 if (defined($baseUrl)) {
67 0         0 $self->{baseUrl} = $baseUrl;
68             }
69             else {
70 1         4 $self->{baseUrl} = DEFAULT_BASE_URL;
71             }
72 1         3 bless $self, $class;
73              
74 1         3 return $self;
75             }
76             #-------------------------------------------------------------------------------
77              
78             #-------------------------------------------------------------------------------
79             #-------------------------------------------------------------------------------
80             sub call {
81              
82 0     0 0 0 my ($self, $path,$format) = @_;
83              
84              
85              
86 0 0       0 if (defined($format)) {
87 0         0 $self->{format} = $format;
88             }
89             else {
90 0         0 $self->{format} = FORMAT;
91             }
92              
93 0         0 $self->{ua} = LWP::UserAgent->new;
94 0         0 $self->{ua}->default_headers(HTTP::Headers->new(
95             Accept => 'application/json'
96             ));
97 0         0 $self->{ua}->agent("IPFinder-Client/Perl/$VERSION");
98              
99 0         0 my $url = $self->{baseUrl}.$path;
100             my $data = {
101             token => $self->{token},
102             format => $self->{format},
103 0         0 };
104 0         0 my $fields = encode_json($data);
105              
106 0         0 my $response = $self->{ua}->post($url, Content => $fields);
107 0 0       0 if ($response->is_success)
108             {
109 0         0 $self->{body} = decode_json($response->decoded_content);
110              
111             }
112             else {
113 0         0 $self->{body} = print("ERROR: " . $response->status_line() . $response->decoded_content);
114             }
115              
116 0         0 return IO::Info->new($self->{body});
117             }
118             #-------------------------------------------------------------------------------
119              
120             #-------------------------------------------------------------------------------
121             #-------------------------------------------------------------------------------
122             sub Authentication {
123 0     0 1 0 my ($self) = @_;
124 0         0 return $self->call('', '');
125             }
126             #-------------------------------------------------------------------------------
127              
128             #-------------------------------------------------------------------------------
129             #-------------------------------------------------------------------------------
130             sub getAddressInfo {
131 1     1 0 52 my ($self, $path) = @_;
132 1         8 IO::Validation::Ipvalidation->validate($path);
133 0         0 return $self->call($path);
134             }
135             #-------------------------------------------------------------------------------
136              
137             #-------------------------------------------------------------------------------
138             #-------------------------------------------------------------------------------
139             sub getAsn {
140 1     1 0 497 my ($self, $path) = @_;
141 1         9 IO::Validation::Asnvalidation->validate($path);
142 0         0 return $self->call($path);
143             }
144             #-------------------------------------------------------------------------------
145              
146             #-------------------------------------------------------------------------------
147             #-------------------------------------------------------------------------------
148             sub getStatus {
149 0     0 0 0 my ($self) = @_;
150 0         0 return $self->call(STATUS_PATH);
151             }
152             #-------------------------------------------------------------------------------
153              
154             #-------------------------------------------------------------------------------
155             #-------------------------------------------------------------------------------
156             sub getRanges {
157 0     0 0 0 my ($self, $path) = @_;
158 0         0 return $self->call(RANGES_PATH.uri_escape($path));
159             }
160             #-------------------------------------------------------------------------------
161              
162             #-------------------------------------------------------------------------------
163             #-------------------------------------------------------------------------------
164             sub getFirewall {
165 2     2 0 643 my ($self, $path,$formats) = @_;
166 2         12 IO::Validation::Firewallvalidation->validate($path,$formats);
167 0         0 return $self->call(FIREWALL_PATH.$path,$formats);
168             }
169             #-------------------------------------------------------------------------------
170              
171             #-------------------------------------------------------------------------------
172             #-------------------------------------------------------------------------------
173             sub getDomain {
174 1     1 0 328 my ($self, $path) = @_;
175 1         9 IO::Validation::Domainvalidation->validate($path);
176 0         0 return $self->call(DOMAIN_PATH.$path);
177             }
178             #-------------------------------------------------------------------------------
179              
180             #-------------------------------------------------------------------------------
181             #-------------------------------------------------------------------------------
182             sub getDomainHistory {
183 1     1 0 338 my ($self, $path) = @_;
184 1         6 IO::Validation::Domainvalidation->validate($path);
185 0           return $self->call(DOMAIN_H_PATH.$path);
186             }
187             #-------------------------------------------------------------------------------
188              
189             #-------------------------------------------------------------------------------
190             #-------------------------------------------------------------------------------
191             sub getDomainBy {
192 0     0 0   my ($self, $by) = @_;
193 0           return $self->call(DOMAIN_BY_PATH.$by);
194             }
195             #-------------------------------------------------------------------------------
196              
197              
198              
199              
200             1;
201             __END__