| 1 | # -*- mode:perl; coding:iso-8859-1 -*- | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Author: Slaven Rezic | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # Copyright (C) 2010,2011,2013,2014,2017 Slaven Rezic. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # This package is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | package Geo::Coder::Googlev3; | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | our $VERSION = '0.15'; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Carp ('croak'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use JSON::XS (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use LWP::UserAgent (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use URI (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use URI::QueryParam (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | my($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 9 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | $self->{ua} = delete $args{ua} || | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 9 | ▶ | LWP::UserAgent->new( | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Googlev3' . "/$Geo::Coder::Googlev3::VERSION libwww-perl/$LWP::VERSION", 'env_proxy', 1, 'timeout', 15)
MC/DC0%0/2
delete $args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Googlev3' . "/$Geo::Coder::Googlev3::VERSION libwww-perl/$LWP::VERSION", 'env_proxy', 1, 'timeout', 15)
delete $args{'ua'}
'LWP::UserAgent'->new('agent', 'Geo::Coder::Googlev3' . "/$Geo::Coder::Googlev3::VERSION libwww-perl/$LWP::VERSION", 'env_proxy', 1, 'timeout', 15)
Truth table33%1/3
A || B
A= delete $args{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::Googlev3' . "/$Geo::Coder::Googlev3::VERSION libwww-perl/$LWP::VERSION", 'env_proxy', 1, 'timeout', 15)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | agent => __PACKAGE__ . "/$VERSION libwww-perl/$LWP::VERSION", | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | env_proxy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | timeout => 15, | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | ▶ | $self->{region} = delete $args{region} || delete $args{gl}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
delete $args{'region'} || delete $args{'gl'}
MC/DC50%1/2
delete $args{'region'} || delete $args{'gl'}
delete $args{'region'}
delete $args{'gl'}
Truth table66%2/3
A || B
A= delete $args{'region'}B= delete $args{'gl'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 9 | $self->{language} = delete $args{language}; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 9 | my $sensor; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | 9 | ▶ | if ($args{sensor}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | $sensor = delete $args{sensor}; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | if ($sensor !~ m{^(false|true)$}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | croak "sensor argument has to be either 'false' or 'true'"; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 6 | $sensor = 'false'; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 8 | $self->{sensor} = $sensor; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | if ($args{bounds}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | $self->bounds(delete $args{bounds}); | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | croak "Unsupported arguments: " . join(" ", %args) if %args; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 8 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | sub ua { | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ua: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 14 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $self->{ua} = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 14 | $self->{ua}; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | sub geocode { | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 14 | ▶ | my($self, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
geocode: called CC=6 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 14 | my $raw = delete $args{raw}; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 14 | my $url = $self->geocode_url(%args); | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 14 | my $ua = $self->ua; | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 14 | my $resp = $ua->get($url); | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | 14 | ▶ | if ($resp->is_success) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 14 | my $content = $resp->decoded_content(charset => "none"); | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 14 | my $res = JSON::XS->new->utf8->decode($content); | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 14 | ▶ | if ($raw) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 13 | ▶ | if ($res->{status} eq 'OK') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return @{ $res->{results} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | return $res->{results}->[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } elsif ($res->{status} eq 'ZERO_RESULTS') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 13 | croak "Fetching $url did not return OK status, but '" . $res->{status} . "'"; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | croak "Fetching $url failed: " . $resp->status_line; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # private! | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | sub geocode_url { | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 17 | ▶ | my($self, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
geocode_url: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 17 | my $loc = $args{location}; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 17 | my $url = URI->new('http://maps.google.com/maps/api/geocode/json'); | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 17 | my %url_params; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 17 | $url_params{address} = $loc; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 17 | $url_params{sensor} = $self->{sensor}; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 17 | ▶ | $url_params{region} = $self->{region} if defined $self->{region}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 17 | ▶ | $url_params{language} = $self->{language} if defined $self->{language}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 17 | ▶ | if (defined $self->{bounds}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | $url_params{bounds} = join '|', map { $_->{lat}.','.$_->{lng} } @{ $self->{bounds} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 17 | ▶ | while(my($k,$v) = each %url_params) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 38 | $url->query_param($k => Encode::encode_utf8($v)); | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 17 | $url = $url->as_string; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 17 | $url; | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | sub region { | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
region: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | $self->{region} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | return $self->{region}; | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | sub language { | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
language: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | $self->{language} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | return $self->{language}; | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | sub sensor { | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sensor: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | ▶ | $self->{sensor} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | return $self->{sensor}; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | use constant _BOUNDS_ERROR_MSG => "bounds must be in the form [{lat=>...,lng=>...}, {lat=>...,lng=>...}]"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | sub bounds { | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bounds: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 7 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 5 | my $bounds = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 5 | ▶ | if (ref $bounds ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | croak _BOUNDS_ERROR_MSG . ', but the supplied parameter is not even an array reference.'; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | if (@$bounds != 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | croak _BOUNDS_ERROR_MSG . ', but the supplied parameter has not exactly two array elements.'; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 3 | ▶ | if ((grep { ref $_ eq 'HASH' && exists $_->{lng} && exists $_->{lat} ? 1 : 0 } @$bounds) != 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
ref $_ eq "HASH" and exists $_->{"lng"} and exists $_->{"lat"}
ref $_ eq "HASH" and exists $_->{"lng"}
MC/DC100%3/3
ref $_ eq "HASH" and exists $_->{"lng"} and exists $_->{"lat"}
ref $_ eq "HASH"
exists $_->{"lng"}
exists $_->{"lat"}
Truth table100%4/4
A and B and C
A= ref $_ eq "HASH"B= exists $_->{"lng"}C= exists $_->{"lat"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | croak _BOUNDS_ERROR_MSG . ', but the supplied elements are not lat/lng hashes.'; | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | $self->{bounds} = $bounds; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 3 | return $self->{bounds}; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||