line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Geo::Coder::TomTom; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
49692
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
80
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
64
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use Carp qw(croak); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
152
|
|
7
|
2
|
|
|
2
|
|
2250
|
use JSON; |
|
2
|
|
|
|
|
38049
|
|
|
2
|
|
|
|
|
11
|
|
8
|
2
|
|
|
2
|
|
2517
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
145604
|
|
|
2
|
|
|
|
|
75
|
|
9
|
2
|
|
|
2
|
|
26
|
use URI; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
49
|
|
10
|
2
|
|
|
2
|
|
11
|
use URI::Escape qw(uri_escape_utf8); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1280
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
13
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
2
|
|
|
2
|
1
|
648
|
my ($class, @params) = @_; |
17
|
2
|
50
|
|
|
|
12
|
my %params = (@params % 2) ? (apikey => @params) : @params; |
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
|
|
7
|
my $self = bless \ %params, $class; |
20
|
|
|
|
|
|
|
|
21
|
2
|
|
50
|
|
|
20
|
$self->{apikey} ||= '1e2099c7-eea9-476b-aac9-b20dc7100af1'; |
22
|
|
|
|
|
|
|
|
23
|
2
|
50
|
|
|
|
6
|
if ($params{ua}) { |
24
|
0
|
|
|
|
|
0
|
$self->ua($params{ua}); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
else { |
27
|
2
|
|
|
|
|
46
|
$self->{ua} = LWP::UserAgent->new(agent => "$class/$VERSION"); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
2
|
100
|
|
|
|
4341
|
if ($self->{debug}) { |
31
|
1
|
|
|
0
|
|
5
|
my $dump_sub = sub { $_[0]->dump(maxlength => 0); return }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
32
|
1
|
|
|
|
|
4
|
$self->ua->set_my_handler(request_send => $dump_sub); |
33
|
1
|
|
|
|
|
45
|
$self->ua->set_my_handler(response_done => $dump_sub); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
2
|
50
|
|
|
|
52
|
$self->{compress} = 1 unless exists $self->{compress}; |
37
|
2
|
50
|
|
|
|
11
|
$self->ua->default_header(accept_encoding => 'gzip,deflate') |
38
|
|
|
|
|
|
|
if $self->{compress}; |
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
|
|
112
|
return $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
0
|
1
|
0
|
sub response { $_[0]->{response} } |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub ua { |
46
|
4
|
|
|
4
|
1
|
7
|
my ($self, $ua) = @_; |
47
|
4
|
50
|
|
|
|
10
|
if ($ua) { |
48
|
0
|
0
|
0
|
|
|
0
|
croak q('ua' must be (or derived from) an LWP::UserAgent') |
49
|
|
|
|
|
|
|
unless ref $ua and $ua->isa(q(LWP::UserAgent)); |
50
|
0
|
|
|
|
|
0
|
$self->{ua} = $ua; |
51
|
|
|
|
|
|
|
} |
52
|
4
|
|
|
|
|
19
|
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
|
|
|
|
|
my $location = $params{location} or return; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my $uri = URI->new('http://routes.tomtom.com'); |
62
|
0
|
|
|
|
|
|
$uri->path( |
63
|
|
|
|
|
|
|
'/lbs/services/geocode/1/query/' . uri_escape_utf8($location) . |
64
|
|
|
|
|
|
|
'/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
|
0
|
|
|
|
|
return unless $res->is_success; |
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
|
0
|
|
|
|
|
return unless $content; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
my $data = eval { from_json($content) }; |
|
0
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
return unless $data; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Result is a list only if there is more than one item. |
83
|
0
|
|
|
|
|
|
my $results = $data->{geoResponse}{geoResult}; |
84
|
0
|
0
|
|
|
|
|
my @results = 'ARRAY' eq ref $results ? @$results : ($results); |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
return wantarray ? @results : $results[0]; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
__END__ |