| 1 | package Geo::Coder::Bing; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.13'; | |||||||||||||||||||||||||||
| 13 | 2 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=24.5
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 4 | my ($class, @params) = @_; | |||||||||||||||||||||||||||
| 17 | 4 | ▶ | my %params = (@params % 2) ? (key => @params) : @params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | carp 'Provide a Bing Maps key to use the new REST API' | ||||||||||||||||||||||||||||
| 20 | 4 | ▶ | unless $params{key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | my $self = bless \ %params, $class; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $self->ua( | ||||||||||||||||||||||||||||
| 25 | 4 | ▶ | $params{ua} || LWP::UserAgent->new(agent => "$class/$VERSION") | ||||||||||||||||||||||||||
|
Condition33%1/3
$params{'ua'} || 'LWP::UserAgent'->new('agent', "$class/$Geo::Coder::Bing::VERSION")
MC/DC0%0/2
$params{'ua'} || 'LWP::UserAgent'->new('agent', "$class/$Geo::Coder::Bing::VERSION")
$params{'ua'}
'LWP::UserAgent'->new('agent', "$class/$Geo::Coder::Bing::VERSION")
Truth table33%1/3
A || B
A= $params{'ua'}B= 'LWP::UserAgent'->new('agent', "$class/$Geo::Coder::Bing::VERSION")
| |||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | if ($self->{debug}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my $dump_sub = sub { $_[0]->dump(maxlength => 0); return }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 30 | 2 | $self->ua->set_my_handler(request_send => $dump_sub); | |||||||||||||||||||||||||||
| 31 | 2 | $self->ua->set_my_handler(response_done => $dump_sub); | |||||||||||||||||||||||||||
| 32 | 2 | ▶ | $self->{compress} ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"compress"} ||= 0
MC/DC0%0/1
$self->{"compress"} ||= 0
$self->{"compress"}
Truth table50%1/2
A
A= $self->{"compress"}
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 4 | ▶ | if (exists $self->{compress} ? $self->{compress} : 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | 2 | $self->ua->default_header(accept_encoding => 'gzip,deflate'); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | croak q('https' requires LWP::Protocol::https) | ||||||||||||||||||||||||||||
| 39 | 4 | ▶ | if $self->{https} and not $self->ua->is_protocol_supported('https'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"https"} and not $self->ua->is_protocol_supported("https")
MC/DC0%0/2
$self->{"https"} and not $self->ua->is_protocol_supported("https")
$self->{"https"}
not $self->ua->is_protocol_supported("https")
Truth table33%1/3
A and not(B)
A= $self->{"https"}B= not $self->ua->is_protocol_supported("https")
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | return $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub response { $_[0]->{response} } | ||||||||||||||||||||||||||
|
Subroutine
response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 10 | ▶ | sub ua { | ||||||||||||||||||||||||||
|
Subroutine
ua: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 10 | my ($self, $ua) = @_; | |||||||||||||||||||||||||||
| 48 | 10 | ▶ | if ($ua) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 4 | ▶ | croak q('ua' must be (or derived from) an LWP::UserAgent') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $ua and $ua->isa("LWP::UserAgent")
MC/DC0%0/2
ref $ua and $ua->isa("LWP::UserAgent")
ref $ua
$ua->isa("LWP::UserAgent")
Truth table33%1/3
A and B
A= ref $uaB= $ua->isa("LWP::UserAgent")
| |||||||||||||||||||||||||||||
| 50 | unless ref $ua and $ua->isa(q(LWP::UserAgent)); | ||||||||||||||||||||||||||||
| 51 | 4 | $self->{ua} = $ua; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 10 | return $self->{ua}; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub geocode { | ||||||||||||||||||||||||||
|
Subroutine
geocode: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return $_[0]->{key} ? &_geocode_rest : &_geocode_ajax; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub _geocode_rest { | ||||||||||||||||||||||||||
|
Subroutine
_geocode_rest: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 61 | 0 | my ($self, @params) = @_; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | my %params = (@params % 2) ? (location => @params) : @params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $params{query} = delete $params{location} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | $_ = Encode::encode('utf-8', $_) for values %params; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $uri = URI->new('http://dev.virtualearth.net/REST/v1/Locations'); | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | $uri->scheme('https') if $self->{https}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | $uri->query_form( | ||||||||||||||||||||||||||||
| 70 | key => $self->{key}, | ||||||||||||||||||||||||||||
| 71 | 0 | %params, | |||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return $self->_rest_request($uri); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Support AJAX API for backwards compatibility. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub _geocode_ajax { | ||||||||||||||||||||||||||
|
Subroutine
_geocode_ajax: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 80 | 0 | my ($self, @params) = @_; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | my %params = (@params % 2) ? (location => @params) : @params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $location = $params{location} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | $location = Encode::encode('utf-8', $location); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my $uri = URI->new('http://dev.virtualearth.net/'); | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | $uri->scheme('https') if $self->{https}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $uri->path_segments(qw( | |||||||||||||||||||||||||||
| 89 | services v1 geocodeservice geocodeservice.asmx Geocode | ||||||||||||||||||||||||||||
| 90 | )); | ||||||||||||||||||||||||||||
| 91 | $uri->query_form( | ||||||||||||||||||||||||||||
| 92 | format => 'json', | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Note: the quotes around the location parameter are required. | ||||||||||||||||||||||||||||
| 95 | query => qq("$location"), | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # These are all required, even if empty. | ||||||||||||||||||||||||||||
| 98 | 0 | map { $_ => '' } qw( | |||||||||||||||||||||||||||
| 99 | addressLine adminDistrict count countryRegion culture | ||||||||||||||||||||||||||||
| 100 | curLocAccuracy currentLocation district entityTypes landmark | ||||||||||||||||||||||||||||
| 101 | locality mapBounds postalCode postalTown rankBy | ||||||||||||||||||||||||||||
| 102 | ), | ||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $res = $self->{response} = $self->ua->get($uri); | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | return unless $res->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Change the content type of the response from 'application/json' so | ||||||||||||||||||||||||||||
| 109 | # HTTP::Message will decode the character encoding. | ||||||||||||||||||||||||||||
| 110 | 0 | $res->content_type('text/plain'); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $content = $res->decoded_content; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | return unless $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Workaround invalid data. | ||||||||||||||||||||||||||||
| 116 | 0 | $content =~ s[ \}\.d $ ][}]x; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $data = eval { from_json($content) }; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | return unless $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my @results = @{ $data->{d}{Results} || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$data->{'d'}{'Results'} || []
MC/DC0%0/1
$data->{'d'}{'Results'} || []
$data->{'d'}{'Results'}
Truth table0%0/2
A
A= $data->{'d'}{'Results'}
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | return wantarray ? @results : $results[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub reverse_geocode { | ||||||||||||||||||||||||||
|
Subroutine
reverse_geocode: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 0 | my ($self, @params) = @_; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | my %params = (@params % 2) ? (latlng => @params) : @params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | $_ = Encode::encode('utf-8', $_) for values %params; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Maintain api compatibility with other geocoders. | ||||||||||||||||||||||||||||
| 132 | 0 | my ($lat, $lon); | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (my $latlon = delete $params{latlng}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ($lat, $lon) = split '\s*,\s*', $latlon; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 0 | $lat = delete $params{lat}; | |||||||||||||||||||||||||||
| 138 | 0 | ($lon) = grep defined, delete @params{qw(lon lng long)}; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | return unless 2 == grep defined, $lat, $lon; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | my $uri = URI->new("http://dev.virtualearth.net/REST/v1/Locations/$lat,$lon"); | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | $uri->scheme('https') if $self->{https}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | $uri->query_form( | ||||||||||||||||||||||||||||
| 145 | key => $self->{key}, | ||||||||||||||||||||||||||||
| 146 | 0 | %params, | |||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | return $self->_rest_request($uri); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub _rest_request { | ||||||||||||||||||||||||||
|
Subroutine
_rest_request: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 153 | 0 | my ($self, $uri) = @_; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | return unless $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | my $res = $self->{response} = $self->ua->get($uri); | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | return unless $res->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Change the content type of the response from 'application/json' so | ||||||||||||||||||||||||||||
| 160 | # HTTP::Message will decode the character encoding. | ||||||||||||||||||||||||||||
| 161 | 0 | $res->content_type('text/plain'); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | my $content = $res->decoded_content; | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | return unless $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | my $data = eval { from_json($content) }; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | return unless $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | my @results = @{ $data->{resourceSets}[0]{resources} || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$data->{'resourceSets'}[0]{'resources'} || []
MC/DC0%0/1
$data->{'resourceSets'}[0]{'resources'} || []
$data->{'resourceSets'}[0]{'resources'}
Truth table0%0/2
A
A= $data->{'resourceSets'}[0]{'resources'}
| |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | return wantarray ? @results : $results[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 2 | 1; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||