blib/lib/Geo/Coder/Bing.pm

filter: Statementstmt 42.1% 43 / 102 Branchbran 23.2% 13 / 56 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 56.2% 9 / 16 Podpod 100% 5 / 5 totaltotal 36.4% 74 / 203 CC 37 SCAR 35.2
CC
37
Cov
35%
_geocode_ajax
90.0
_rest_request
56.0
reverse_geocode
42.0
CRAP 33.8
↑ Summary
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
BranchTrueFalseTotal
@params % 2 ? : 2 2 4
18  
19 carp 'Provide a Bing Maps key to use the new REST API'
20 4 ▶ unless $params{key};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"key"} 0 4 4
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")
l!l&&r!l&&!r
040
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")
ABresult
1X1
011
000
26 );
27  
28 4 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 2 2 4
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
l!l
02
MC/DC0%0/1
$self->{"compress"} ||= 0
$self->{"compress"}
Truth table50%1/2
A
A= $self->{"compress"}
Aresult
11
00
33 }
34 4 ▶ if (exists $self->{compress} ? $self->{compress} : 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{"compress"} ? $self->{"compress"} : 1) 2 2 4
exists $self->{'compress'} ? : 2 2 4
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
BranchTrueFalseTotal
if $self->{"https"} and not $self->ua->is_protocol_supported("https") 0 4 4
Condition33%1/3
$self->{"https"} and not $self->ua->is_protocol_supported("https")
!ll&&!rl&&r
400
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($ua) 4 6 10
49 4 ▶ croak q('ua' must be (or derived from) an LWP::UserAgent')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ua and $ua->isa("LWP::UserAgent") 0 4 4
Condition33%1/3
ref $ua and $ua->isa("LWP::UserAgent")
!ll&&!rl&&r
004
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$_[0]{'key'} ? : 0 0 0
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
BranchTrueFalseTotal
@params % 2 ? : 0 0 0
63  
64 0 ▶ $params{query} = delete $params{location} or return;
Branch: 1 branch
BranchTrueFalseTotal
$params{"query"} = delete $params{"location"} or return 0 0 0
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
BranchTrueFalseTotal
if $self->{"https"} 0 0 0
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
BranchTrueFalseTotal
@params % 2 ? : 0 0 0
82  
83 0 ▶ my $location = $params{location} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $location = $params{"location"} or return 0 0 0
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
BranchTrueFalseTotal
if $self->{"https"} 0 0 0
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
BranchTrueFalseTotal
unless $res->is_success 0 0 0
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
BranchTrueFalseTotal
unless $content 0 0 0
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
BranchTrueFalseTotal
unless $data 0 0 0
120  
121 0 ▶ my @results = @{ $data->{d}{Results} || [] };
Condition0%0/2
$data->{'d'}{'Results'} || []
l!l
00
MC/DC0%0/1
$data->{'d'}{'Results'} || []
$data->{'d'}{'Results'}
Truth table0%0/2
A
A= $data->{'d'}{'Results'}
Aresult
11
00
122 0 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
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
BranchTrueFalseTotal
@params % 2 ? : 0 0 0
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
BranchTrueFalseTotal
if (my $latlon = delete $params{'latlng'}) { } 0 0 0
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
BranchTrueFalseTotal
unless 2 == grep(defined($_), $lat, $lon) 0 0 0
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
BranchTrueFalseTotal
if $self->{"https"} 0 0 0
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
BranchTrueFalseTotal
unless $uri 0 0 0
155  
156 0 my $res = $self->{response} = $self->ua->get($uri);
157 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
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
BranchTrueFalseTotal
unless $content 0 0 0
165  
166 0 my $data = eval { from_json($content) };
167 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
168  
169 0 ▶ my @results = @{ $data->{resourceSets}[0]{resources} || [] };
Condition0%0/2
$data->{'resourceSets'}[0]{'resources'} || []
l!l
00
MC/DC0%0/1
$data->{'resourceSets'}[0]{'resources'} || []
$data->{'resourceSets'}[0]{'resources'}
Truth table0%0/2
A
A= $data->{'resourceSets'}[0]{'resources'}
Aresult
11
00
170 0 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
171 }
172  
173  
174 2 1;
175  
176 __END__
↑ Summary