Permalink
Browse files

don't limit location string to unicode-flagged string or utf-8 bytes

  • Loading branch information...
1 parent 16c98cc commit 5e72e4a78bfcabc8372a4bf3308fcce538a18654 @gray committed Sep 30, 2009
Showing with 43 additions and 8 deletions.
  1. +5 −0 Changes
  2. +2 −0 Makefile.PL
  3. +13 −4 lib/Geo/Coder/Mapquest.pm
  4. +23 −4 xt/live.t
View
@@ -1,5 +1,10 @@
Revision history for Geo-Coder-Mapquest
+0.03
+ - Location is no longer limited to only unicode-flagged strings or
+ UTF-8 bytes.
+ - Updated example script to use apikey.
+
0.02 Sat Sep 19 08:52:40 UTC 2009
- Added missing example of location data format.
- Added file portability author test.
View
@@ -9,10 +9,12 @@ my %conf = (
VERSION_FROM => 'lib/Geo/Coder/Mapquest.pm',
ABSTRACT_FROM => 'lib/Geo/Coder/Mapquest.pm',
PREREQ_PM => {
+ 'Encode' => 0,
'JSON' => 2.0,
'LWP::UserAgent' => 0,
'Test::More' => 0,
'URI' => 1.36,
+ 'URI::Escape' => 0,
},
META_MERGE => {
resources => {
View
@@ -4,6 +4,7 @@ use strict;
use warnings;
use Carp qw(croak);
+use Encode ();
use JSON;
use LWP::UserAgent;
use URI;
@@ -44,18 +45,26 @@ sub ua {
sub geocode {
my $self = shift;
- my $location = @_ % 2 ? $_[0] : $_[0] eq 'location' ? $_[1] : '';
- return unless $location;
+ my %params = (@_ % 2) ? (location => shift, @_) : @_;
+ my $location = $params{location} or return;
+ my $country = $params{country};
+
+ $location = Encode::encode('utf-8', $location);
my $uri = URI->new(
'http://platform.beta.mapquest.com/geocoding/v1/address'
);
- $uri->query_form(key => $self->{key}, location => $location);
+ $uri->query_form(
+ key => $self->{key},
+ location => $location,
+ $country ? (adminArea1 => $country) : (),
+
+ );
my $res = $self->ua->get($uri);
return unless $res->is_success;
- my $data = eval { decode_json($res->decoded_content) };
+ my $data = eval { from_json($res->decoded_content) };
return unless $data;
my @results = @{ $data->{locations} || [] };
View
@@ -1,13 +1,14 @@
use strict;
use warnings;
+use Encode qw(decode encode);
use Geo::Coder::Mapquest;
use Test::More;
unless ($ENV{MAPQUEST_APIKEY}) {
plan skip_all => 'MAPQUEST_APIKEY environment variable must be set';
}
else {
- plan tests => 2;
+ plan tests => 8;
}
my $geocoder = Geo::Coder::Mapquest->new(apikey => $ENV{MAPQUEST_APIKEY});
@@ -16,8 +17,26 @@ my $geocoder = Geo::Coder::Mapquest->new(apikey => $ENV{MAPQUEST_APIKEY});
my $location = $geocoder->geocode($address);
is($location->{postalCode}, 90028, "correct zip code for $address");
}
+{
+ my @locations = $geocoder->geocode('Main Street, Los Angeles, CA');
+ ok(@locations > 1, 'there are many Main Streets in Los Angeles, CA');
+}
TODO: {
- local $TODO = 'Multiple locations';
- my @locations = $geocoder->geocode('Main Street');
- ok(@locations > 1, 'there are many Main Streets');
+ local $TODO = 'International locations';
+ my $address = qq(Ch\xE2teau d Uss\xE9, 37420);
+
+ my $location = $geocoder->geocode($address, country => 'FR');
+ ok($location, 'latin1 bytes');
+ is($location->{adminArea1}, 'FR', 'latin1 bytes');
+
+ $location = $geocoder->geocode(decode('latin1', $address), country => 'FR');
+ ok($location, 'UTF-8 characters');
+ is($location->{adminArea1}, 'FR', 'UTF-8 characters');
+
+ $location = $geocoder->geocode(
+ encode('utf-8', decode('latin1', $address)), country => 'FR',
+ );
+ ok($location, 'UTF-8 bytes');
+ is($location->{adminArea1}, 'FR', 'UTF-8 bytes');
}
+

0 comments on commit 5e72e4a

Please sign in to comment.