| 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
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$self->{"apikey"} ||= "1e2099c7-eea9-476b-aac9-b20dc7100af1"
$self->{"apikey"}
Truth table50%1/2
A
A= $self->{"apikey"}
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | if ($params{ua}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 37 | $self->ua->default_header(accept_encoding => 'gzip,deflate') | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if $self->{compress}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | croak q('ua' must be (or derived from) an LWP::UserAgent') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $ua and $ua->isa("LWP::UserAgent")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my $location = $params{location} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | my $data = eval { from_json($content) }; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | return unless $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return wantarray ? @results : $results[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||