App-ipinfo
view release on metacpan or search on metacpan
SECURITY.md
t/compile.t
t/default_template.t
t/format.t
t/get_info.t
t/ipv6.t
t/json.t
t/lib/Local/ipinfo.pm
t/live.t
t/load.t
t/looks_like_template.t
t/pod.t
t/pod_coverage.t
t/run.t
t/test_manifest
META.yml Module YAML meta-data (added by MakeMaker)
META.json Module JSON meta-data (added by MakeMaker)
#!perl
use v5.26;
use strict;
use open qw(:std :utf8);
use App::ipinfo;
my $opts = {};
$opts->{template} = shift @ARGV if App::ipinfo->looks_like_template($ARGV[0]);
App::ipinfo->run( $opts, @ARGV );
lib/App/ipinfo.pm view on Meta::CPAN
error_fh => $class->default_error_fh,
template => $class->default_template,
token => $class->get_token,
};
my %args = ( $defaults->%*, %hash );
bless \%args, $class;
}
=item * looks_like_template(STRING)
Returns true if STRING looks like a template. That is, it has a C<%>
followed by a non-whitespace character. This will get more sophisticated
later.
=cut
sub looks_like_template ($either, $string) {
$string =~ m/%\S/;
}
=item * CLASS->run( [TEMPLATE,] IP_ADDRESS [, IP_ADDRESS ... ] )
=item * OBJ->run( [TEMPLATE,] IP_ADDRESS [, IP_ADDRESS ... ] )
Format every IP address according to TEMPLATE and send the result to
the output filehandle.
If the first argument looks like a template (has a C<%>), it is used
to format the output. Otherwise, the first argument is taken as the start
of the list of IP addresses and the default format is used.
If the invocant is not a reference, it's used as the class name to
build the object. If the invocant is a reference, it's used as the
object. These are the same and use all the default settings:
my $obj = App::ipinfo->new;
$obj->run( @ip_addresses );
lib/App/ipinfo.pm view on Meta::CPAN
=cut
sub get_info ($app, $ip ) {
state $ipinfo = do {
my $g = Geo::IPinfo->new( $app->token );
$g->{base_url_ipv6} = $g->{base_url};
$g;
};
my $method = do {
if( $app->looks_like_ipv4($ip) ) {
'info';
}
elsif( $app->looks_like_ipv6($ip) ) {
$ip = _compact_ipv6($ip);
'info_v6'
}
else {
$app->error( "<$ip> does not look like an IP address. Skipping." );
return;
}
};
my $info = $ipinfo->$method($ip);
lib/App/ipinfo.pm view on Meta::CPAN
if( exists $info->{bogon} and $info->{bogon} eq 'True' ) {
$app->error( "<$ip> is a bogon." );
return;
}
$app->decode_info($info);
return $info;
}
=item * looks_like_ipv4(IP)
Returns true if IP looks like an IPv4 address.
=cut
sub looks_like_ipv4 ($app, $ip) {
Net::CIDR::cidrvalidate($ip);
}
=item * looks_like_ipv6(IP)
Returns true if IP looks like an IPv6 address.
=cut
sub looks_like_ipv6 ($app, $ip) {
my $compact = _compact_ipv6($ip);
Net::CIDR::cidrvalidate($compact);
}
=item * get_token
Return the API token. So far, this is just the value in the C<APP_IPINFO_TOKEN>
environment variable.
=cut
t/looks_like_template.t view on Meta::CPAN
use strict;
use warnings;
use open qw(:std :utf8);
use Test::More;
plan skip_all => 'set APP_IPINFO_TOKEN for live tests'
unless defined $ENV{'APP_IPINFO_TOKEN'};
my $class = 'App::ipinfo';
my $method = 'looks_like_template';
my $ip = '151.101.130.132';
subtest 'sanity' => sub {
use_ok $class;
can_ok $class, $method;
};
subtest 'templates' => sub {
my @templates = qw( %% %n %T %j acb%T );
foreach my $t ( @templates ) {
ok $class->$method($t), "<$t> looks like a template";
}
};
subtest 'non templates' => sub {
my @templates = qw( abc 123 % abc% );
foreach my $t ( @templates ) {
ok ! $class->$method($t), "<$t> does not look like a template";
}
};
t/test_manifest view on Meta::CPAN
load.t
compile.t
pod.t
pod_coverage.t
default_template.t
looks_like_template.t
ipv6.t
get_info.t
format.t
json.t
run.t
live.t
( run in 0.485 second using v1.01-cache-2.11-cpan-64827b87656 )