Addr-MyIP

 view release on metacpan or  search on metacpan

lib/Addr/MyIP.pm  view on Meta::CPAN

package Addr::MyIP;

use strict;
use warnings;

use Data::Dumper;
use Exporter qw(import);
use HTTP::Tiny;

our $VERSION = '0.05';

our @EXPORT = qw(myip myip6);

use constant {
    IPV4_URL    => 'https://api.ipify.org',
    IPV6_URL    => 'https://api64.ipify.org',
};

my $client = HTTP::Tiny->new;

sub myip {
    return _get(IPV4_URL);
}
sub myip6 {
    my $ip = _get(IPV6_URL);

    return $ip =~ /\./ ? '' : $ip;
}

sub _get {
    my ($url) = @_;
    my $response = $client->get($url);

    my $status = $response->{status};

    if ($status != 200) {
        warn "Failed to connect to $url to get your address: $response->{content}";
        return '';
    }

    return $response->{content};
}

sub __placeholder {}

1;
__END__

=head1 NAME

Addr::MyIP - Get your public facing IPv4 or IPv6 address

=for html
<a href="https://github.com/stevieb9/addr-myip/actions"><img src="https://github.com/stevieb9/addr-myip/workflows/CI/badge.svg"/></a>
<a href='https://coveralls.io/github/stevieb9/addr-myip?branch=main'><img src='https://coveralls.io/repos/stevieb9/addr-myip/badge.svg?branch=main&service=github' alt='Coverage Status' /></a>

=head1 SYNOPSIS

    use Addr::MyIP;

    my $ipv4_addr = myip();
    my $ipv6_addr = myip6();

=head1 DESCRIPTION

For end-users, please review the

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.113 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )