File Coverage

blib/lib/Addr/MyIP.pm
Criterion Covered Total %
statement 28 28 100.0
branch 3 4 75.0
condition n/a
subroutine 9 10 90.0
pod 2 2 100.0
total 42 44 95.4


line stmt bran cond sub pod time code
1             package Addr::MyIP;
2              
3 3     3   66026 use strict;
  3         29  
  3         75  
4 3     3   16 use warnings;
  3         5  
  3         105  
5              
6 3     3   1568 use Data::Dumper;
  3         18736  
  3         258  
7 3     3   26 use Exporter qw(import);
  3         8  
  3         88  
8 3     3   1869 use HTTP::Tiny;
  3         138787  
  3         215  
9              
10             our $VERSION = '0.05';
11              
12             our @EXPORT = qw(myip myip6);
13              
14             use constant {
15 3         701 IPV4_URL => 'https://api.ipify.org',
16             IPV6_URL => 'https://api64.ipify.org',
17 3     3   25 };
  3         8  
18              
19             my $client = HTTP::Tiny->new;
20              
21             sub myip {
22 2     2 1 3214 return _get(IPV4_URL);
23             }
24             sub myip6 {
25 2     2 1 3093 my $ip = _get(IPV6_URL);
26              
27 2 50       9 return $ip =~ /\./ ? '' : $ip;
28             }
29              
30             sub _get {
31 4     4   15 my ($url) = @_;
32 4         19 my $response = $client->get($url);
33              
34 4         88 my $status = $response->{status};
35              
36 4 100       16 if ($status != 200) {
37 2         22 warn "Failed to connect to $url to get your address: $response->{content}";
38 2         8 return '';
39             }
40              
41 2         7 return $response->{content};
42             }
43              
44       0     sub __placeholder {}
45              
46             1;
47             __END__