summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2013-09-30 19:03:51 -0700
committerMark Wells <mark@freeside.biz>2013-09-30 19:03:51 -0700
commit49fabe633e3a1f1b8d6b164929a498d9e48dcc38 (patch)
tree80afc964a7a75e7fcab493bb405a0d064e595298
start
-rw-r--r--.gitignore6
-rw-r--r--Changes5
-rw-r--r--Geocoding.pm145
-rw-r--r--MANIFEST7
-rw-r--r--Makefile.PL20
-rw-r--r--README46
-rw-r--r--ignore.txt12
-rw-r--r--t/00-load.t9
-rw-r--r--t/01-geocode.t24
9 files changed, 274 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9788afa
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+blib/
+*.sw?
+Makefile
+Makefile.old
+MYMETA.yml
+pm_to_blib
diff --git a/Changes b/Changes
new file mode 100644
index 0000000..28878c8
--- /dev/null
+++ b/Changes
@@ -0,0 +1,5 @@
+Revision history for Geo-TomTom-Geocoding
+
+0.01 Sep 30 2013
+ initial commit
+
diff --git a/Geocoding.pm b/Geocoding.pm
new file mode 100644
index 0000000..7bc0e57
--- /dev/null
+++ b/Geocoding.pm
@@ -0,0 +1,145 @@
+package Geo::TomTom::Geocoding;
+
+use 5.006;
+use strict;
+use warnings;
+
+use LWP::UserAgent;
+use base 'HTTP::Response';
+use JSON 'decode_json';
+
+=head1 NAME
+
+Geo::TomTom::Geocoding - Interface to the TomTom Geocoding service
+
+=head1 VERSION
+
+Version 0.01
+
+=cut
+
+our $VERSION = '0.01';
+our $DEBUG = 1;
+
+=head1 SYNOPSIS
+
+ use Geo::TomTom::Geocoding;
+
+ my $request = {
+ key => '12345678abcdef',# API authorization
+ ST => '123', # address number
+ T => 'Main Street', # street name
+ L => 'San Francisco', # locality
+ AA => 'CA', # administrative area (state/province/district)
+ PC => '93102', # postal code
+ CC => 'USA', # country code (ISO three-letter)
+ IS => 'Market Street', # intersecting street
+ # other options specified in the API docs
+ };
+ $result = Geo::TomTom::Geocoding->query($request);
+
+ if ($result->is_success) {
+ foreach ($result->locations) {
+ print $_->{formattedAddress};
+ }
+ }
+
+=head1 CLASS METHODS
+
+=head2 query HASHREF
+
+Send a request to the web service. See
+L<http://developer.tomtom.com/docs/read/geocoding/Request> for fields that
+can be included in HASHREF. C<key> is your API key.
+
+Returns an object of class Geo::TomTom::Geocoding.
+
+=cut
+
+my $ua = LWP::UserAgent->new;
+my $base_uri = 'https://api.tomtom.com/lbs/geocoding/geocode';
+
+sub query {
+ my $class = shift;
+ my $opt;
+ if (ref($_[0]) eq 'HASH') {
+ $opt = [ %{ $_[0] } ];
+ } elsif (ref($_[0]) eq 'ARRAY') {
+ $opt = shift;
+ } else {
+ $opt = [ @_ ];
+ }
+ push @$opt, format => 'json';
+ my $uri = URI->new($base_uri);
+ $uri->query_form($opt);
+ warn "Geo::TomTom::Geocoding->request\n$uri\n\n" if $DEBUG;
+ my $http_req = HTTP::Request->new(GET => $uri->as_string);
+ my $self = $ua->request($http_req);
+ bless $self, $class;
+ if ( $self->is_success ) {
+ my $data = decode_json($self->content);
+ if ( $data->{geoResponse}->{count} == 0 ) {
+ $self->message("Location not found");
+ } else {
+ $self->locations( @{$data->{geoResponse}->{geoResult}} );
+ }
+ }
+
+ $self;
+}
+
+=head1 METHODS
+
+=head2 locations
+
+Returns a list of all locations matched by the address query. Each one
+will be a hashref containing any of the elements listed in the documentation:
+L<http://developer.tomtom.com/docs/read/geocoding/Response>
+
+=cut
+
+sub locations {
+ my $self = shift;
+ if ( @_ ) {
+ if ( ref($_[0]) eq 'ARRAY' ) {
+ $self->{_locations} = shift;
+ } elsif ( ref($_[0]) eq 'HASH' ) {
+ $self->{_locations} = [ @_ ];
+ }
+ }
+ @{ $self->{_locations} || [] };
+}
+
+=head1 AUTHOR
+
+Mark Wells, C<< <mark at freeside.biz> >>
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+ perldoc Geo::TomTom::Geocoding
+
+For the request and response attributes, see the TomTom API documentation:
+
+ L<developer.tomtom.com/docs/read/geocoding>
+
+=back
+
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright 2013 Mark Wells.
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of either: the GNU General Public License as published
+by the Free Software Foundation; or the Artistic License.
+
+See http://dev.perl.org/licenses/ for more information.
+
+The data from the TomTom geocoding service is NOT free software. Consult
+TomTom's terms of service for details.
+
+=cut
+
+1; # End of Geo::TomTom::Geocoding
diff --git a/MANIFEST b/MANIFEST
new file mode 100644
index 0000000..a36a283
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,7 @@
+Changes
+Geocoding.pm
+Makefile.PL
+MANIFEST This list of files
+README
+t/00-load.t
+t/01-geocode.t
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..2a68cb4
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,20 @@
+use 5.006;
+use strict;
+use warnings;
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ NAME => 'Geo::TomTom::Geocoding',
+ AUTHOR => q{Mark Wells <mark@freeside.biz>},
+ VERSION_FROM => 'Geocoding.pm',
+ ABSTRACT_FROM => 'Geocoding.pm',
+ ($ExtUtils::MakeMaker::VERSION >= 6.3002
+ ? ('LICENSE'=> 'perl')
+ : ()),
+ PL_FILES => {},
+ PREREQ_PM => {
+ 'Test::More' => 0,
+ },
+ dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
+ clean => { FILES => 'Geo-TomTom-Geocoding-*' },
+);
diff --git a/README b/README
new file mode 100644
index 0000000..1aabf7c
--- /dev/null
+++ b/README
@@ -0,0 +1,46 @@
+Geo-TomTom-Geocoding
+
+Interface to the TomTom geocoding service.
+
+INSTALLATION
+
+To install this module, run the following commands:
+
+ export TOMTOM_API_KEY=<your API key> # recommended for testing
+ perl Makefile.PL
+ make
+ make test
+ make install
+
+SUPPORT AND DOCUMENTATION
+
+After installing, you can find documentation for this module with the
+perldoc command.
+
+ perldoc Geo::TomTom::Geocoding
+
+You can also look for information at:
+
+ RT, CPAN's request tracker (report bugs here)
+ http://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-TomTom-Geocoding
+
+ AnnoCPAN, Annotated CPAN documentation
+ http://annocpan.org/dist/Geo-TomTom-Geocoding
+
+ CPAN Ratings
+ http://cpanratings.perl.org/d/Geo-TomTom-Geocoding
+
+ Search CPAN
+ http://search.cpan.org/dist/Geo-TomTom-Geocoding/
+
+
+LICENSE AND COPYRIGHT
+
+Copyright (C) 2013 Mark Wells
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of either: the GNU General Public License as published
+by the Free Software Foundation; or the Artistic License.
+
+See http://dev.perl.org/licenses/ for more information.
+
diff --git a/ignore.txt b/ignore.txt
new file mode 100644
index 0000000..be821e6
--- /dev/null
+++ b/ignore.txt
@@ -0,0 +1,12 @@
+blib*
+Makefile
+Makefile.old
+Build
+Build.bat
+_build*
+pm_to_blib*
+*.tar.gz
+.lwpcookies
+cover_db
+pod2htm*.tmp
+Geo-TomTom-Geocoding-*
diff --git a/t/00-load.t b/t/00-load.t
new file mode 100644
index 0000000..89be297
--- /dev/null
+++ b/t/00-load.t
@@ -0,0 +1,9 @@
+#!perl -T
+
+use Test::More tests => 1;
+
+BEGIN {
+ use_ok( 'Geo::TomTom::Geocoding' ) || print "Bail out!\n";
+}
+
+diag( "Testing Geo::TomTom::Geocoding $Geo::TomTom::Geocoding::VERSION, Perl $], $^X" );
diff --git a/t/01-geocode.t b/t/01-geocode.t
new file mode 100644
index 0000000..a53e145
--- /dev/null
+++ b/t/01-geocode.t
@@ -0,0 +1,24 @@
+#!perl
+
+use Test::More tests => 1;
+use Geo::TomTom::Geocoding;
+
+SKIP: {
+ my $key = $ENV{TOMTOM_API_KEY};
+ skip("TOMTOM_API_KEY not set", 1) unless $key;
+
+ my $test_loc = [
+ key => $key,
+ ST => 55,
+ T => 'Music Concourse Drive',
+ L => 'San Francisco',
+ AA => 'CA',
+ CC => 'USA',
+ ];
+ my $response = Geo::TomTom::Geocoding->query($test_loc);
+ my ($first_match) = $response->locations;
+ note "Test results:\n".explain $first_match;
+ ok( $first_match->{postcode} =~ /^94118/ );
+
+}
+