blib/lib/Geo/Coder/Bing.pm

filter: Statementstmt 50.0% 43 / 86 Branchbran 26.0% 13 / 50 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 64.2% 9 / 14 Podpod 100% 4 / 4 totaltotal 42.4% 73 / 172 CC 31 SCAR 38.5
CC
31
Cov
41%
_geocode_rest
110.0
_geocode_ajax
90.0
new
11.6
CRAP 46.9
↑ 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.12';
13 2 $VERSION = eval $VERSION;
14  
15 sub new {
16 4 ▶ my ($class, @params) = @_;
Subroutine new: called CC=10 SCAR=24.5
Pod documented
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 2 ▶ 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 sub ua {
47 10 ▶ my ($self, $ua) = @_;
Subroutine ua: called CC=4 SCAR=14.5
Pod documented
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 sub geocode {
57 0 ▶ return $_[0]->{key} ? &_geocode_rest : &_geocode_ajax;
Subroutine geocode: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'key'} ? : 0 0 0
58 }
59  
60 sub _geocode_rest {
61 0 ▶ my ($self, @params) = @_;
Subroutine _geocode_rest: not called CC=10 SCAR=47.0
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 my $res = $self->{response} = $self->ua->get($uri);
75 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
76  
77 # Change the content type of the response from 'application/json' so
78 # HTTP::Message will decode the character encoding.
79 0 $res->content_type('text/plain');
80  
81 0 my $content = $res->decoded_content;
82 0 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
83  
84 0 my $data = eval { from_json($content) };
85 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
86  
87 0 ▶ my @results = @{ $data->{resourceSets}[0]{resources} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$data->{"resourceSets"}[0]{"resources"} or [] 0 0 0
88 0 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
89 }
90  
91  
92 # Support AJAX API for backwards compatibility.
93  
94 sub _geocode_ajax {
95 0 ▶ my ($self, @params) = @_;
Subroutine _geocode_ajax: not called CC=9 SCAR=45.0
96 0 ▶ my %params = (@params % 2) ? (location => @params) : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params % 2 ? : 0 0 0
97  
98 0 ▶ my $location = $params{location} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $location = $params{"location"} or return 0 0 0
99 0 $location = Encode::encode('utf-8', $location);
100  
101 0 my $uri = URI->new('http://dev.virtualearth.net/');
102 0 ▶ $uri->scheme('https') if $self->{https};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"https"} 0 0 0
103 0 $uri->path_segments(qw(
104 services v1 geocodeservice geocodeservice.asmx Geocode
105 ));
106 $uri->query_form(
107 format => 'json',
108  
109 # Note: the quotes around the location parameter are required.
110 query => qq("$location"),
111  
112 # These are all required, even if empty.
113 0 map { $_ => '' } qw(
114 addressLine adminDistrict count countryRegion culture
115 curLocAccuracy currentLocation district entityTypes landmark
116 locality mapBounds postalCode postalTown rankBy
117 ),
118 );
119  
120 0 my $res = $self->{response} = $self->ua->get($uri);
121 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
122  
123 # Change the content type of the response from 'application/json' so
124 # HTTP::Message will decode the character encoding.
125 0 $res->content_type('text/plain');
126  
127 0 my $content = $res->decoded_content;
128 0 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
129  
130 # Workaround invalid data.
131 0 $content =~ s[ \}\.d $ ][}]x;
132  
133 0 my $data = eval { from_json($content) };
134 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
135  
136 0 ▶ my @results = @{ $data->{d}{Results} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$data->{"d"}{"Results"} or [] 0 0 0
137 0 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
138 }
139  
140  
141 2 1;
142  
143 __END__
↑ Summary