Geo-What3Words
view release on metacpan or search on metacpan
lib/Geo/What3Words.pm view on Meta::CPAN
# ABSTRACT: turn WGS84 coordinates into three word addresses and vice-versa using what3words.com HTTPS API
package Geo::What3Words;
$Geo::What3Words::VERSION = '3.0.4';
use strict;
use warnings;
use Cpanel::JSON::XS;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
use Encode qw( decode_utf8 );
use HTTP::Tiny;
use Net::Ping;
use Net::Ping::External;
use Ref::Util qw( is_hashref is_coderef );
use URI;
use utf8;
# DO NOT TRY TO USE URI::XS IT JUST LEADS TO PROBLEMS
my $JSONXS = Cpanel::JSON::XS->new->allow_nonref(1);
sub new {
my ($class, %params) = @_;
my $self = {};
$self->{api_endpoint} = $params{api_endpoint} || 'https://api.what3words.com/v3/';
$self->{key} = $params{key} || die "API key not set";
$self->{language} = $params{language};
$self->{logging} = $params{logging};
## _ua is used for testing. But could also be used to
## set proxies or such
$self->{ua} = $params{ua} || HTTP::Tiny->new;
my $version = $Geo::What3Words::VERSION || '';
$self->{ua}->agent("Perl Geo::What3Words $version");
return bless($self, $class);
}
sub ping {
my $self = shift;
## http://example.com/some/path => example.com
## also works with IP addresses
my $host = URI->new($self->{api_endpoint})->host;
$self->_log("pinging $host...");
my $netping = Net::Ping->new('external');
my $res = $netping->ping($host);
$self->_log($res ? 'available' : 'unavailable');
return $res;
}
sub words2pos {
my ($self, @params) = @_;
my $res = $self->words_to_position(@params);
if ($res && is_hashref($res) && exists($res->{coordinates})) {
return $res->{coordinates}->{lat} . ',' . $res->{coordinates}->{lng};
}
return;
}
sub pos2words {
( run in 0.905 second using v1.01-cache-2.11-cpan-39bf76dae61 )