line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Geo::Coder::TomTom; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
32338
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
51
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
8
|
use Carp qw(croak); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
169
|
|
7
|
2
|
|
|
2
|
|
1179
|
use Encode (); |
|
2
|
|
|
|
|
16461
|
|
|
2
|
|
|
|
|
41
|
|
8
|
2
|
|
|
2
|
|
1339
|
use JSON; |
|
2
|
|
|
|
|
22802
|
|
|
2
|
|
|
|
|
8
|
|
9
|
2
|
|
|
2
|
|
1535
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
71346
|
|
|
2
|
|
|
|
|
64
|
|
10
|
2
|
|
|
2
|
|
14
|
use URI; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
938
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
13
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
5
|
|
|
5
|
1
|
1303
|
my ($class, @params) = @_; |
17
|
5
|
100
|
|
|
|
19
|
my %params = (@params % 2) ? (apikey => @params) : @params; |
18
|
|
|
|
|
|
|
|
19
|
5
|
100
|
|
|
|
166
|
croak q('apikey' is required) unless exists $params{apikey}; |
20
|
|
|
|
|
|
|
|
21
|
4
|
|
|
|
|
8
|
my $self = bless \ %params, $class; |
22
|
|
|
|
|
|
|
|
23
|
4
|
|
33
|
|
|
51
|
$self->ua( |
24
|
|
|
|
|
|
|
$params{ua} || LWP::UserAgent->new(agent => "$class/$VERSION") |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
4
|
100
|
|
|
|
10
|
if ($self->{debug}) { |
28
|
2
|
|
|
0
|
|
6
|
my $dump_sub = sub { $_[0]->dump(maxlength => 0); return }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
29
|
2
|
|
|
|
|
5
|
$self->ua->set_my_handler(request_send => $dump_sub); |
30
|
2
|
|
|
|
|
54
|
$self->ua->set_my_handler(response_done => $dump_sub); |
31
|
2
|
|
50
|
|
|
53
|
$self->{compress} ||= 0; |
32
|
|
|
|
|
|
|
} |
33
|
4
|
100
|
|
|
|
12
|
if (exists $self->{compress} ? $self->{compress} : 1) { |
|
|
100
|
|
|
|
|
|
34
|
2
|
|
|
|
|
3
|
$self->ua->default_header(accept_encoding => 'gzip,deflate'); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
4
|
50
|
33
|
|
|
74
|
croak q('https' requires LWP::Protocol::https) |
38
|
|
|
|
|
|
|
if $self->{https} and not $self->ua->is_protocol_supported('https'); |
39
|
|
|
|
|
|
|
|
40
|
4
|
|
|
|
|
9
|
return $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
0
|
1
|
0
|
sub response { $_[0]->{response} } |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub ua { |
46
|
10
|
|
|
10
|
1
|
2555
|
my ($self, $ua) = @_; |
47
|
10
|
100
|
|
|
|
19
|
if ($ua) { |
48
|
4
|
50
|
33
|
|
|
26
|
croak q('ua' must be (or derived from) an LWP::UserAgent') |
49
|
|
|
|
|
|
|
unless ref $ua and $ua->isa(q(LWP::UserAgent)); |
50
|
4
|
|
|
|
|
7
|
$self->{ua} = $ua; |
51
|
|
|
|
|
|
|
} |
52
|
10
|
|
|
|
|
18
|
return $self->{ua}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub geocode { |
56
|
0
|
|
|
0
|
1
|
|
my ($self, @params) = @_; |
57
|
0
|
0
|
|
|
|
|
my %params = (@params % 2) ? (location => @params) : @params; |
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
$params{query} = delete $params{location} or return; |
60
|
0
|
|
|
|
|
|
$_ = Encode::encode('utf-8', $_) for values %params; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my $uri = URI->new('https://api.tomtom.com/lbs/services/geocode/4/geocode'); |
63
|
0
|
0
|
|
|
|
|
$uri->scheme('https') if $self->{https}; |
64
|
0
|
|
|
|
|
|
$uri->query_form( |
65
|
|
|
|
|
|
|
key => $self->{apikey}, |
66
|
|
|
|
|
|
|
format => 'json', |
67
|
|
|
|
|
|
|
%params, |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $res = $self->{response} = $self->ua->get($uri); |
71
|
0
|
0
|
|
|
|
|
return unless $res->is_success; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Change the content type of the response from 'application/json' so |
74
|
|
|
|
|
|
|
# HTTP::Message will decode the character encoding. |
75
|
0
|
|
|
|
|
|
$res->content_type('text/plain'); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $content = $res->decoded_content; |
78
|
0
|
0
|
|
|
|
|
return unless $content; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $data = eval { from_json($content) }; |
|
0
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
return unless $data; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Result is a list only if there is more than one item. |
84
|
0
|
|
|
|
|
|
my $results = $data->{geoResponse}{geoResult}; |
85
|
0
|
0
|
|
|
|
|
my @results = 'ARRAY' eq ref $results ? @$results : ($results); |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
return wantarray ? @results : $results[0]; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |