blib/lib/Geo/Coder/Bing/Bulk.pm

filter: Statementstmt 38.3% 46 / 120 Branchbran 17.7% 11 / 62 Conditioncond 16.6% 3 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 52.6% 10 / 19 Podpod 100% 7 / 7 totaltotal 32.3% 77 / 238 CC 40 SCAR 37.6
CC
40
Cov
30%
_status
110.0
upload
90.0
_download
42.0
CRAP 42.8
↑ Summary
1 package Geo::Coder::Bing::Bulk;
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(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTTP::Request::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.04';
14 2 $VERSION = eval $VERSION;
15  
16 4 ▶sub new {
Subroutine new: called CC=9 SCAR=24.2
Pod documented
17 4 my ($class, @params) = @_;
18 4 ▶ my %params = (@params % 2) ? (key => @params) : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params % 2 ? : 2 2 4
19  
20 4 ▶ croak q('key' is required) 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::Bulk::VERSION")
l!l&&r!l&&!r
040
MC/DC0%0/2
$params{'ua'} || 'LWP::UserAgent'->new('agent', "$class/$Geo::Coder::Bing::Bulk::VERSION")
$params{'ua'} 'LWP::UserAgent'->new('agent', "$class/$Geo::Coder::Bing::Bulk::VERSION")
Truth table33%1/3
A || B
A= $params{'ua'}B= 'LWP::UserAgent'->new('agent', "$class/$Geo::Coder::Bing::Bulk::VERSION")
ABresult
1X1
011
000
26 );
27  
28 4 ▶ if ($self->{debug}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'debug'}) { } 2 2 4
elsif (exists $self->{'compress'} ? $self->{'compress'} : 1) { } 2 0 2
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 }
33 ▶ elsif (exists $self->{compress} ? $self->{compress} : 1) {
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'compress'} ? : 0 2 2
34 2 $self->ua->default_header(accept_encoding => 'gzip,deflate');
35 }
36  
37 croak q('https' requires LWP::Protocol::https)
38 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
39  
40 4 $self->{status} = '';
41  
42 4 return $self;
43 }
44  
45 0 ▶sub response { $_[0]->{response} }
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
46  
47 10 ▶sub ua {
Subroutine ua: called CC=4 SCAR=14.5
Pod documented
48 10 my ($self, $ua) = @_;
49 10 ▶ if ($ua) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua) 4 6 10
50 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
51 unless ref $ua and $ua->isa(q(LWP::UserAgent));
52 4 $self->{ua} = $ua;
53 }
54 10 return $self->{ua};
55 }
56  
57 0 ▶sub upload {
Subroutine upload: not called CC=9 SCAR=45.0
Pod documented
58 0 my $self = shift;
59  
60 0 ▶ my $locs = (1 == @_ and 'ARRAY' eq ref $_[0]) ? $_[0] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
1 == @_ && 'ARRAY' eq ref $_[0] ? : 0 0 0
Condition0%0/3
1 == @_ and "ARRAY" eq ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
1 == @_ and "ARRAY" eq ref $_[0]
1 == @_ "ARRAY" eq ref $_[0]
Truth table0%0/3
A and B
A= 1 == @_B= "ARRAY" eq ref $_[0]
ABresult
0X0
100
111
61 0 ▶ return unless @$locs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$locs 0 0 0
62  
63 0 my $uri = URI->new(
64 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode',
65 );
66 0 ▶ $uri->scheme('https') if $self->{https};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"https"} 0 0 0
67 $uri->query_form(
68 key => $self->{key},
69 0 input => 'pipe',
70 );
71  
72 0 my $req = HTTP::Request::Common::POST(
73 $uri, content_type => 'text/plain',
74 );
75  
76 0 my $id = 0;
77 0 for my $loc (@$locs) {
78 0 (my $str = $loc) =~ tr/|\n\r/ /s;
79 0 $req->add_content_utf8("$id||$str\n");
80 0 $id++;
81 }
82 # Prevents LWP warning about wrong content length.
83 0 $req->content_length(length(${$req->content_ref}));
84  
85 0 my $res = $self->{response} = $self->ua->request($req);
86 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
87  
88 # Change the content type of the response from 'application/json' so
89 # HTTP::Message will decode the character encoding.
90 0 $res->content_type('text/plain');
91  
92 0 my $content = $res->decoded_content;
93 0 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
94  
95 0 my $data = eval { from_json($content) };
96 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
97  
98 0 return $self->{id} = $data->{resourceSets}[0]{resources}[0]{id};
99 }
100  
101 0 ▶sub is_pending {
Subroutine is_pending: not called CC=3 SCAR=24.8
Pod documented
102 0 my ($self) = @_;
103  
104 0 my $status = $self->_status;
105 0 ▶ return 1 if not $status or 'pending' eq $status;
Branch: 1 branch
BranchTrueFalseTotal
if not $status or "pending" eq $status 0 0 0
Condition0%0/3
not $status or "pending" eq $status
l!l&&r!l&&!r
000
MC/DC0%0/2
not $status or "pending" eq $status
not $status "pending" eq $status
Truth table0%0/3
not(A) or B
A= not $statusB= "pending" eq $status
ABresult
1X1
011
000
106 }
107  
108 0 ▶sub _status {
Subroutine _status: not called CC=10 SCAR=47.0
109 0 my ($self) = @_;
110  
111 0 ▶ return unless $self->{content} or $self->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"content"} or $self->{"id"} 0 0 0
Condition0%0/3
$self->{"content"} or $self->{"id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"content"} or $self->{"id"}
$self->{"content"} $self->{"id"}
Truth table0%0/3
A or B
A= $self->{"content"}B= $self->{"id"}
ABresult
1X1
011
000
112  
113 my $uri = URI->new(
114 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/' .
115 $self->{id}
116 0 );
117 0 ▶ $uri->scheme('https') if $self->{https};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"https"} 0 0 0
118 $uri->query_form(
119 key => $self->{key},
120 0 );
121  
122 0 my $res = $self->{response} = $self->ua->get($uri);
123 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
124  
125 # Change the content type of the response from 'application/json' so
126 # HTTP::Message will decode the character encoding.
127 0 $res->content_type('text/plain');
128  
129 0 my $content = $res->decoded_content;
130 0 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
131  
132 0 my $data = eval { from_json($content) };
133 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
134  
135 0 my $resources = $data->{resourceSets}[0]{resources}[0];
136 0 ▶ return unless $resources->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless $resources->{"status"} 0 0 0
137  
138 0 ▶ $self->{failed} = 1 if $resources->{failedEntityCount};
Branch: 1 branch
BranchTrueFalseTotal
if $resources->{"failedEntityCount"} 0 0 0
139 0 ▶ $self->{succeeded} = 1 if $resources->{processedEntityCount};
Branch: 1 branch
BranchTrueFalseTotal
if $resources->{"processedEntityCount"} 0 0 0
140  
141 0 return $self->{status} = lc $resources->{status};
142 }
143  
144 0 ▶sub download { $_[0]->_download('succeeded') }
Subroutine download: not called CC=1 SCAR=6.9
Pod documented
145 0 ▶sub failed { $_[0]->_download('failed') }
Subroutine failed: not called CC=1 SCAR=6.9
Pod documented
146  
147 0 ▶sub _download {
Subroutine _download: not called CC=6 SCAR=37.4
148 0 my ($self, $type) = @_;
149  
150 0 ▶ return unless 'completed' eq $self->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless "completed" eq $self->{"status"} 0 0 0
151 0 ▶ return unless $self->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$type} 0 0 0
152  
153 my $uri = URI->new(
154 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/' .
155 0 $self->{id} . '/output/' . $type
156 );
157 0 ▶ $uri->scheme('https') if $self->{https};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"https"} 0 0 0
158 $uri->query_form(
159 key => $self->{key},
160 0 );
161  
162 0 my $res = $self->{response} = $self->ua->get($uri);
163 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
164  
165 0 my $content_ref = $res->decoded_content(ref => 1);
166 0 ▶ return unless $$content_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $$content_ref 0 0 0
167  
168 0 return $self->_parse_output($content_ref);
169 }
170  
171 # Convert the pipe-delimited output to a data structure conforming
172 # to the data schema described here [1].
173 # [1] http://msdn.microsoft.com/en-us/library/ff701736.aspx
174 2 my %field_mapping = (
175 0 => 'Id',
176 2 => 'Query',
177 12 => [ Address => 'AddressLine' ],
178 13 => [ Address => 'AdminDistrict' ],
179 14 => [ Address => 'CountryRegion' ],
180 15 => [ Address => 'District' ],
181 16 => [ Address => 'FormattedAddress' ],
182 17 => [ Address => 'Locality' ],
183 18 => [ Address => 'PostalCode' ],
184 19 => [ Address => 'PostalTown' ],
185 20 => [ RooftopLocation => 'Latitude' ],
186 21 => [ RooftopLocation => 'Longitude' ],
187 22 => [ InterpolatedLocation => 'Latitude' ],
188 23 => [ InterpolatedLocation => 'Longitude' ],
189 24 => 'Confidence',
190 25 => 'DisplayName',
191 26 => 'EntityType',
192 27 => 'StatusCode',
193 28 => 'FaultReason',
194 );
195  
196 0 ▶sub _parse_output {
Subroutine _parse_output: not called CC=5 SCAR=34.0
197 0 my ($self, $ref) = @_;
198  
199 0 my @data;
200 0 ▶ while ($$ref =~ /([^\n\r]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref =~ /([^\n\r]+)/g) 0 0 0
201 0 my @fields = split '\|', $1, 31;
202 0 my $data = {};
203 0 for my $i (keys %field_mapping) {
204 0 my $val = $fields[$i];
205 0 ▶ if (length $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $val) 0 0 0
206 0 my $key = $field_mapping{$i};
207 0 ▶ if (ref $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key) { } 0 0 0
208 0 $data->{$key->[0]}{$key->[1]} = $val;
209 }
210 else {
211 0 $data->{$key} = $val;
212 }
213 }
214 }
215 0 push @data, $data;
216 }
217  
218 0 return \@data;
219 }
220  
221  
222 2 1;
223  
224 __END__
↑ Summary