Permalink
Browse files

initial checkin

  • Loading branch information...
0 parents commit 36cb5022d51dd58def7eaeaa0de741ac8b5f3e82 @gray committed Sep 30, 2009
Showing with 497 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +4 −0 Changes
  3. +16 −0 MANIFEST
  4. +49 −0 Makefile.PL
  5. +29 −0 README
  6. +31 −0 ex/abc.pl
  7. +31 −0 ex/custom-useragent.pl
  8. +205 −0 lib/Geo/Coder/Multimap.pm
  9. +5 −0 t/00_compile.t
  10. +8 −0 t/01_new.t
  11. +8 −0 xt/kwalitee.t
  12. +44 −0 xt/live.t
  13. +11 −0 xt/perlcritic.t
  14. +22 −0 xt/perlcriticrc
  15. +10 −0 xt/pod.t
  16. +9 −0 xt/pod_coverage.t
  17. +10 −0 xt/portability_filenames.t
@@ -0,0 +1,5 @@
+Geo-Coder-Multimap-*
+Makefile
+Makefile.old
+blib/
+pm_to_blib
@@ -0,0 +1,4 @@
+Revision history for Geo-Coder-Multimap
+
+0.01 Wed Sep 30 06:45:38 UTC 2009
+ - Initial distribution.
@@ -0,0 +1,16 @@
+Changes
+ex/abc.pl
+ex/custom-useragent.pl
+lib/Geo/Coder/Multimap.pm
+Makefile.PL
+MANIFEST This list of files
+README
+t/00_compile.t
+t/01_new.t
+xt/kwalitee.t
+xt/live.t
+xt/perlcritic.t
+xt/perlcriticrc
+xt/pod.t
+xt/pod_coverage.t
+xt/portability_filenames.t
@@ -0,0 +1,49 @@
+use strict;
+use warnings;
+use ExtUtils::MakeMaker;
+
+my %conf = (
+ NAME => 'Geo::Coder::Multimap',
+ AUTHOR => 'gray <gray@cpan.org>',
+ LICENSE => 'perl',
+ VERSION_FROM => 'lib/Geo/Coder/Multimap.pm',
+ ABSTRACT_FROM => 'lib/Geo/Coder/Multimap.pm',
+ PREREQ_PM => {
+ 'Encode' => 0,
+ 'JSON' => 2.0,
+ 'LWP::UserAgent' => 0,
+ 'Test::More' => 0,
+ 'URI' => 1.36,
+ },
+ META_MERGE => {
+ resources => {
+ repository => 'http://github.com/gray/geo-coder-multimap',
+ },
+ recommends => {
+ 'JSON::XS' => 2.0,
+ },
+ },
+ dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
+ clean => { FILES => 'Geo-Coder-Multimap-*' },
+);
+
+unless (eval { ExtUtils::MakeMaker->VERSION(6.46) }) {
+ delete $conf{META_MERGE};
+}
+
+WriteMakefile(%conf);
+
+
+sub MY::postamble {
+ return <<" MAKE_FRAG";
+authortest:
+\t\$(MAKE) -e \$(TEST_TYPE) TEST_FILES="xt/*.t"
+ MAKE_FRAG
+}
+
+sub MY::dist_test {
+ my $self = shift;
+ return $self->MM::dist_test . <<" MAKE_FRAG";
+\tcd \$(DISTVNAME) && \$(MAKE) authortest \$(PASTHRU)
+ MAKE_FRAG
+}
@@ -0,0 +1,29 @@
+Geo-Coder-Multimap
+==================
+
+This module provides an interface to the Beta Multimap Geocoding Web Service.
+
+INSTALLATION
+
+To install this module type the following:
+
+ perl Makefile.PL
+ make
+ make test
+ make install
+
+DEPENDENCIES
+
+This module requires these other modules and libraries:
+
+ JSON
+ LWP::UserAgent
+ Test::More
+ URI
+
+COPYRIGHT AND LICENCE
+
+Copyright (C) 2009 by gray <gray@cpan.org>
+
+This library is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use Data::Dumper;
+use Geo::Coder::Multimap;
+
+unless ($ENV{MULTIMAP_APIKEY}) {
+ die "MULTIMAP_APIKEY environment variable must be set";
+}
+my $location = join(' ', @ARGV) || die "Usage: $0 \$location_string";
+
+# Custom useragent identifier.
+my $ua = LWP::UserAgent->new(agent => 'My Geocoder');
+
+# Allow compressed replies.
+if (eval "use Compress::Zlib") {
+ $ua->default_headers(accept_encoding => 'gzip,deflate');
+}
+
+# Load any proxy settings from environment variables.
+$ua->env_proxy;
+
+my $geocoder = Geo::Coder::Multimap->new(
+ apikey => $ENV{MULTIMAP_APIKEY},
+ ua => $ua,
+);
+my @result = $geocoder->geocode(location => $location);
+
+local $Data::Dumper::Indent = 1;
+print Dumper(@result);
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use Data::Dumper;
+use Geo::Coder::Multimap;
+
+unless ($ENV{MULTIMAP_APIKEY}) {
+ die "MULTIMAP_APIKEY environment variable must be set";
+}
+my $location = join(' ', @ARGV) || die "Usage: $0 \$location_string";
+
+# Custom useragent identifier.
+my $ua = LWP::UserAgent->new(agent => 'My Geocoder');
+
+# Allow compressed replies.
+if (eval "use Compress::Zlib") {
+ $ua->default_headers(accept_encoding => 'gzip,deflate');
+}
+
+# Load any proxy settings from environment variables.
+$ua->env_proxy;
+
+my $geocoder = Geo::Coder::Multimap->new(
+ apikey => $ENV{MULTIMAP_APIKEY},
+ ua => $ua,
+);
+my $result = $geocoder->geocode(location => $location);
+
+local $Data::Dumper::Indent = 1;
+print Dumper($result);
@@ -0,0 +1,205 @@
+package Geo::Coder::Multimap;
+
+use strict;
+use warnings;
+
+use Carp qw(croak);
+use Encode ();
+use JSON;
+use LWP::UserAgent;
+use URI;
+use URI::Escape qw(uri_unescape);
+
+our $VERSION = '0.01';
+$VERSION = eval $VERSION;
+
+sub new {
+ my ($class, %params) = @_;
+
+ my $key = $params{apikey} or croak q('apikey' is required);
+
+ my $self = bless {
+ key => uri_unescape($key),
+ }, $class;
+
+ if ($params{ua}) {
+ $self->ua($params{ua});
+ }
+ else {
+ $self->{ua} = LWP::UserAgent->new(agent => "$class/$VERSION");
+ }
+
+ return $self;
+}
+
+sub ua {
+ my ($self, $ua) = @_;
+ if ($ua) {
+ croak q('ua' must be (or derived from) an LWP::UserAgent')
+ unless ref $ua and $ua->isa(q(LWP::UserAgent));
+ $self->{ua} = $ua;
+ }
+ return $self->{ua};
+}
+
+sub geocode {
+ my $self = shift;
+
+ 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://developer.multimap.com/API/geocode/1.2/' . $self->{key}
+ );
+ $uri->query_form(
+ qs => $params{location},
+ output => 'json',
+ $country ? (countryCode => $country) : (),
+ );
+
+ my $res = $self->ua->get($uri);
+ return unless $res->is_success;
+
+ my $data = eval { from_json($res->decoded_content) };
+ return unless $data;
+
+ my @results = @{ $data->{result_set} || [] };
+ return wantarray ? @results : $results[0];
+}
+
+
+1;
+
+__END__
+
+=head1 NAME
+
+Geo::Coder::Multimap - Geocode addresses with the Multimap Open API
+
+=head1 SYNOPSIS
+
+ use Geo::Coder::Multimap;
+
+ my $geocoder = Geo::Coder::Multimap->new(apikey => 'Your API Key');
+ my $location = $geocoder->geocode(
+ location => 'Hollywood and Highland, Los Angeles, CA, US'
+ );
+
+=head1 DESCRIPTION
+
+The C<Geo::Coder::Multimap> module provides an interface to the geocoding
+functionality of the Multimap Open API.
+
+=head1 METHODS
+
+=head2 new
+
+ $geocoder = Geo::Coder::Multimap->new(apikey => 'Your API Key')
+
+Creates a new geocoding object.
+
+An API key can be obtained here:
+L<https://www.multimap.com/openapi/signup/>.
+
+Accepts an optional B<ua> parameter for passing in a custom LWP::UserAgent
+object.
+
+=head2 geocode
+
+ $location = $geocoder->geocode(location => $loc)
+ $location = $geocoder->geocode(location => $loc, country => $code)
+ @locations = $geocoder->geocode(location => $loc)
+
+The C<location> string should either include the country or the C<country>
+paramter should be given. Note, the C<country> parameter will produce
+better results in most cases.
+
+In scalar context, this method returns the first location result; and in
+list context it returns all locations results.
+
+Each location result is a hashref; a typical example looks like:
+
+ {
+ 'geocode_quality' => '3a',
+ 'point' => {
+ 'lat' => '34.10156',
+ 'lon' => '-118.33872'
+ },
+ 'zoom_factor' => 14,
+ 'address' => {
+ 'postal_code' => '90028',
+ 'country_code' => 'US',
+ 'areas' => [ 'HOLLYWOOD', 'CA' ],
+ 'display_name' => 'HOLLYWOOD, CA, 90028'
+ },
+ 'geocode_score' => '0.409'
+ }
+
+=head2 ua
+
+ $ua = $geocoder->ua()
+ $ua = $geocoder->ua($ua)
+
+Accessor for the UserAgent object.
+
+=head1 SEE ALSO
+
+L<http://www.multimap.com/openapidocs/1.2/web_service/ws_geocoding.htm>
+
+L<Geo::Coder::Bing>, L<Geo::Coder::Google>, L<Geo::Coder::Mapquest>,
+L<Geo::Coder::Yahoo>
+
+=head1 REQUESTS AND BUGS
+
+Please report any bugs or feature requests to
+L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Geo-Coder-Multimap>.
+I will be notified, and then you'll automatically be notified of progress on
+your bug as I make changes.
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+ perldoc Geo::Coder::Multimap
+
+You can also look for information at:
+
+=over
+
+=item * GitHub Source Repository
+
+L<http://github.com/gray/geo-coder-multimap>
+
+=item * AnnoCPAN: Annotated CPAN documentation
+
+L<http://annocpan.org/dist/Geo-Coder-Multimap>
+
+=item * CPAN Ratings
+
+L<http://cpanratings.perl.org/d/Geo-Coder-Multimap>
+
+=item * RT: CPAN's request tracker
+
+L<http://rt.cpan.org/Public/Dist/Display.html?Name=Geo-Coder-Multimap>
+
+=item * Search CPAN
+
+L<http://search.cpan.org/dist/Geo-Coder-Multimap>
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2009 gray <gray at cpan.org>, all rights reserved.
+
+This library is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=head1 AUTHOR
+
+gray, <gray at cpan.org>
+
+=cut
@@ -0,0 +1,5 @@
+use strict;
+use warnings;
+use Test::More tests => 1;
+
+BEGIN { use_ok 'Geo::Coder::Multimap' }
@@ -0,0 +1,8 @@
+use strict;
+use warnings;
+use Test::More tests => 2;
+use Geo::Coder::Multimap;
+
+my $geo = Geo::Coder::Multimap->new(apikey => 'placeholder');
+isa_ok($geo, 'Geo::Coder::Multimap', 'new');
+can_ok('Geo::Coder::Multimap', qw(geocode ua));
Oops, something went wrong.

0 comments on commit 36cb502

Please sign in to comment.