blib/lib/Geo/Coder/TomTom.pm

filter: Statementstmt 64.5% 40 / 62 Branchbran 25.0% 7 / 28 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 75.0% 9 / 12 Podpod 100% 4 / 4 totaltotal 53.5% 61 / 114 CC 17 SCAR 31.9
CC
17
Cov
51%
geocode
72.0
ua
8.7
new
8.1
CRAP 24.2
↑ Summary
1 package Geo::Coder::TomTom;
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 JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.03';
13 2 $VERSION = eval $VERSION;
14  
15 sub new {
16 2 ▶ my ($class, @params) = @_;
Subroutine new: called CC=7 SCAR=21.0
Pod documented
17 2 ▶ my %params = (@params % 2) ? (apikey => @params) : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params % 2 ? : 0 2 2
18  
19 2 my $self = bless \ %params, $class;
20  
21 2 ▶ $self->{apikey} ||= '1e2099c7-eea9-476b-aac9-b20dc7100af1';
Condition50%1/2
$self->{"apikey"} ||= "1e2099c7-eea9-476b-aac9-b20dc7100af1"
l!l
02
MC/DC0%0/1
$self->{"apikey"} ||= "1e2099c7-eea9-476b-aac9-b20dc7100af1"
$self->{"apikey"}
Truth table50%1/2
A
A= $self->{"apikey"}
Aresult
11
00
22  
23 2 ▶ if ($params{ua}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'ua'}) { } 0 2 2
24 0 $self->ua($params{ua});
25 }
26 else {
27 2 $self->{ua} = LWP::UserAgent->new(agent => "$class/$VERSION");
28 }
29  
30 2 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 1 1 2
31 1 ▶ my $dump_sub = sub { $_[0]->dump(maxlength => 0); return };
Subroutine __ANON__: not called CC=1 SCAR=2.6
32 1 $self->ua->set_my_handler(request_send => $dump_sub);
33 1 $self->ua->set_my_handler(response_done => $dump_sub);
34 }
35  
36 2 ▶ $self->{compress} = 1 unless exists $self->{compress};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"compress"} 2 0 2
37 $self->ua->default_header(accept_encoding => 'gzip,deflate')
38 2 ▶ if $self->{compress};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"compress"} 2 0 2
39  
40 2 return $self;
41 }
42  
43 0 ▶sub response { $_[0]->{response} }
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
44  
45 sub ua {
46 4 ▶ my ($self, $ua) = @_;
Subroutine ua: called CC=4 SCAR=21.7
Pod documented
47 4 ▶ if ($ua) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua) 0 4 4
48 0 ▶ croak q('ua' must be (or derived from) an LWP::UserAgent')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ua and $ua->isa("LWP::UserAgent") 0 0 0
Condition0%0/3
ref $ua and $ua->isa("LWP::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ua and $ua->isa("LWP::UserAgent")
ref $ua $ua->isa("LWP::UserAgent")
Truth table0%0/3
A and B
A= ref $uaB= $ua->isa("LWP::UserAgent")
ABresult
0X0
100
111
49 unless ref $ua and $ua->isa(q(LWP::UserAgent));
50 0 $self->{ua} = $ua;
51 }
52 4 return $self->{ua};
53 }
54  
55 sub geocode {
56 0 ▶ my ($self, @params) = @_;
Subroutine geocode: not called CC=8 SCAR=42.8
Pod documented
57 0 ▶ my %params = (@params % 2) ? (location => @params) : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params % 2 ? : 0 0 0
58  
59 0 ▶ my $location = $params{location} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $location = $params{"location"} or return 0 0 0
60  
61 0 my $uri = URI->new('http://routes.tomtom.com');
62 $uri->path(
63 '/lbs/services/geocode/1/query/' . uri_escape_utf8($location) .
64 0 '/json/' . $self->{apikey} . ';language=en;map=basic'
65 );
66  
67 0 my $res = $self->{response} = $self->ua->get(
68 $uri, referer => 'http://routes.tomtom.com/'
69 );
70 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
71  
72 # Change the content type of the response from 'application/json' so
73 # HTTP::Message will decode the character encoding.
74 0 $res->content_type('text/plain');
75  
76 0 my $content = $res->decoded_content;
77 0 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
78  
79 0 my $data = eval { from_json($content) };
80 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
81  
82 # Result is a list only if there is more than one item.
83 0 my $results = $data->{geoResponse}{geoResult};
84 0 ▶ my @results = 'ARRAY' eq ref $results ? @$results : ($results);
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $results ? : 0 0 0
85  
86 0 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
87 }
88  
89  
90 2 1;
91  
92 __END__
↑ Summary