| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Geo::Coder::Multimap; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 56124 | use strict; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 75 |  | 
| 4 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 59 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 8 | use Carp qw(croak); | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 134 |  | 
| 7 | 2 |  |  | 2 |  | 1967 | use Encode (); | 
|  | 2 |  |  |  |  | 27396 |  | 
|  | 2 |  |  |  |  | 44 |  | 
| 8 | 2 |  |  | 2 |  | 2360 | use JSON; | 
|  | 2 |  |  |  |  | 38602 |  | 
|  | 2 |  |  |  |  | 13 |  | 
| 9 | 2 |  |  | 2 |  | 2687 | use LWP::UserAgent; | 
|  | 2 |  |  |  |  | 108246 |  | 
|  | 2 |  |  |  |  | 62 |  | 
| 10 | 2 |  |  | 2 |  | 18 | use URI; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 48 |  | 
| 11 | 2 |  |  | 2 |  | 9 | use URI::Escape qw(uri_unescape); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 904 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 14 |  |  |  |  |  |  | $VERSION = eval $VERSION; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub new { | 
| 17 | 1 |  |  | 1 | 1 | 16 | my ($class, %params) = @_; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 | 50 |  |  |  | 5 | my $key = $params{apikey} or croak q('apikey' is required); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 1 |  |  |  |  | 8 | my $self = bless { | 
| 22 |  |  |  |  |  |  | key => uri_unescape($key), | 
| 23 |  |  |  |  |  |  | }, $class; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 1 | 50 |  |  |  | 16 | if ($params{ua}) { | 
| 26 | 0 |  |  |  |  | 0 | $self->ua($params{ua}); | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  | else { | 
| 29 | 1 |  |  |  |  | 30 | $self->{ua} = LWP::UserAgent->new(agent => "$class/$VERSION"); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 1 |  |  |  |  | 3579 | return $self; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub ua { | 
| 36 | 0 |  |  | 0 | 1 |  | my ($self, $ua) = @_; | 
| 37 | 0 | 0 |  |  |  |  | if ($ua) { | 
| 38 | 0 | 0 | 0 |  |  |  | croak q('ua' must be (or derived from) an LWP::UserAgent') | 
| 39 |  |  |  |  |  |  | unless ref $ua and $ua->isa(q(LWP::UserAgent)); | 
| 40 | 0 |  |  |  |  |  | $self->{ua} = $ua; | 
| 41 |  |  |  |  |  |  | } | 
| 42 | 0 |  |  |  |  |  | return $self->{ua}; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub geocode { | 
| 46 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 | 0 |  |  |  |  | my %params   = (@_ % 2) ? (location => shift, @_) : @_; | 
| 49 | 0 | 0 |  |  |  |  | my $location = $params{location} or return; | 
| 50 | 0 |  |  |  |  |  | my $country  = $params{country}; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 |  |  |  |  |  | $location = Encode::encode('utf-8', $location); | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 |  |  |  |  |  | my $uri = URI->new( | 
| 55 |  |  |  |  |  |  | 'http://developer.multimap.com/API/geocode/1.2/' . $self->{key} | 
| 56 |  |  |  |  |  |  | ); | 
| 57 | 0 | 0 |  |  |  |  | $uri->query_form( | 
| 58 |  |  |  |  |  |  | qs     => $params{location}, | 
| 59 |  |  |  |  |  |  | output => 'json', | 
| 60 |  |  |  |  |  |  | $country ? (countryCode => $country) : (), | 
| 61 |  |  |  |  |  |  | ); | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | my $res = $self->ua->get($uri); | 
| 64 | 0 | 0 |  |  |  |  | return unless $res->is_success; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | my $data = eval { from_json($res->decoded_content) }; | 
|  | 0 |  |  |  |  |  |  | 
| 67 | 0 | 0 |  |  |  |  | return unless $data; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 | 0 |  |  |  |  | my @results = @{ $data->{result_set} || [] }; | 
|  | 0 |  |  |  |  |  |  | 
| 70 | 0 | 0 |  |  |  |  | return wantarray ? @results : $results[0]; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | 1; | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | __END__ |