Address-PostCode-UK

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.08  2015-03-14T23:25:00+01:00
      - Upgraded to use Address::PostCode::UserAgent 0.04.
      - Added LICENSE file.

0.07  2015-01-16T14:30:00+01:00
      - Removed SIGNATURE file.
      - Updated MANIFEST.SKIP file to ignore folder 'eumm/'.
      - Updated Copyright year information.

0.06  2014-11-03T22:40:45+01:00
      - Checking the US post code format.
      - Added test case for UK post code format.

0.05  2014-11-02T13:14:20+01:00
      - Added more unit test.
      - Validate the response before populating the object.
      - Minor changes to the pod document.

0.04  2014-10-29T17:45:00+01:00
      - Tidied up README file.

0.03  2014-10-29T15:34:20+01:00

MANIFEST  view on Meta::CPAN

lib/Address/PostCode/UK/Place/Council.pm
lib/Address/PostCode/UK/Place/Geo.pm
lib/Address/PostCode/UK/Place/Ward.pm
Makefile.PL
LICENSE
MANIFEST
MANIFEST.SKIP
README
CONTRIBUTING.md
t/00-load.t
t/01-address-postcode-uk.t
t/02-address-postcode-uk-place.t
t/manifest.t
t/pod.t
t/changes.t
t/meta-json.t
t/meta-yml.t
META.yml                                 Module YAML meta-data (added by MakeMaker)
META.json                                Module JSON meta-data (added by MakeMaker)

lib/Address/PostCode/UK.pm  view on Meta::CPAN

use Address::PostCode::UK::Place;
use Address::PostCode::UK::Place::Geo;
use Address::PostCode::UK::Place::Ward;
use Address::PostCode::UK::Place::Council;
use Address::PostCode::UK::Place::Constituency;

use Moo;
use namespace::autoclean;
extends 'Address::PostCode::UserAgent';

our $BASE_URL = 'http://uk-postcodes.com';

=head1 DESCRIPTION

Interface to the API provided by L<UK Postcodes|http://uk-postcodes.com/>.

=head1 NOTE

Data  may  be  used  under the terms of the OS OpenData licence. Northern Ireland
postcode  data may be used under the terms of the ONSPD licence. Currently, there
are no limitations on usage, but they may introduce rate limiting in future.

=head1 METHODS

=head2 details()

It returns an object of type L<Address::PostCode::UK::Place> on success. The only
parameter requires is the post code.

    use strict; use warnings;
    use Address::PostCode::UK;

    my $address   = Address::PostCode::UK->new;
    my $post_code = 'Post Code';
    my $place     = $address->details($post_code);

    print "Latitude : ", $place->geo->lat, "\n";
    print "Longitude: ", $place->geo->lng, "\n";

=cut

sub details {
    my ($self, $post_code) = @_;

    die "ERROR: Missing required param 'post code'.\n" unless defined $post_code;
    die "ERROR: Invalid format for UK post code [$post_code].\n" unless ($post_code =~ /[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/gi);

    $post_code =~ s/\s//g;
    my $url      = sprintf("%s/postcode/%s.json", $BASE_URL, $post_code);
    my $response = $self->get($url);
    my $contents = from_json($response->{'content'});

    my ($geo, $ward, $council, $constituency);
    $geo  = Address::PostCode::UK::Place::Geo->new($contents->{'geo'})
        if (exists $contents->{'geo'});
    $ward = Address::PostCode::UK::Place::Ward->new($contents->{'administrative'}->{'ward'})
        if (exists $contents->{'administrative'}->{'ward'});
    $council = Address::PostCode::UK::Place::Council->new($contents->{'administrative'}->{'council'})
        if (exists $contents->{'administrative'}->{'council'});
    $constituency = Address::PostCode::UK::Place::Constituency->new($contents->{'administrative'}->{'constituency'})
        if (exists $contents->{'administrative'}->{'constituency'});

    return Address::PostCode::UK::Place->new(
        'geo' => $geo, 'ward' => $ward, 'council' => $council, 'constituency' => $constituency);
}

=head2 nearest()

It returns ref to a list of objects of type L<Address::PostCode::UK::Location> on
success. The required parameters are the post code and distance in miles.

    use strict; use warnings;
    use Address::PostCode::UK;

    my $address   = Address::PostCode::UK->new;
    my $post_code = 'Post Code';
    my $distance  = 1;
    my $locations = $address->nearest($post_code, $distance);

    print "Location Latitude : ", $locations->[0]->lat, "\n";
    print "Location Longitude: ", $locations->[0]->lng, "\n";

=cut

sub nearest {
    my ($self, $post_code, $distance) = @_;

    die "ERROR: Missing required param 'post code'.\n" unless defined $post_code;
    die "ERROR: Missing required param 'distance'.\n"  unless defined $distance;

    die "ERROR: Invalid format for UK post code [$post_code].\n" unless ($post_code =~ /[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/gi);
    die "ERROR: Invalid distance [$distance].\n" unless ($distance =~ /^[\d+]$/);

    $post_code =~ s/\s//g;
    my $url      = sprintf("%s/postcode/nearest?postcode=%s&miles=%d&format=json", $BASE_URL, $post_code, $distance);
    my $response = $self->get($url);
    my $contents = from_json($response->{'content'});

    my $locations = [];
    foreach (@$contents) {
        push @$locations, Address::PostCode::UK::Location->new($_);
    }

    return $locations;
}

lib/Address/PostCode/UK.pm  view on Meta::CPAN

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Address-PostCode-UK>

=head1 BUGS

Please report any bugs or feature requests to C<bug-address-postcode-uk at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Address-PostCode-UK>.
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 Address::PostCode::UK

lib/Address/PostCode/UK/Location.pm  view on Meta::CPAN


use 5.006;
use Data::Dumper;

use Moo;
use namespace::autoclean;

has 'lat'      => (is => 'ro');
has 'lng'      => (is => 'ro');
has 'distance' => (is => 'ro');
has 'postcode' => (is => 'ro');
has 'uri'      => (is => 'ro');

=head1 METHODS

=head2 lat()

=head2 lng()

=head2 distance()

=head2 postcode()

=head2 uri()

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Address-PostCode-UK>

=head1 BUGS

Please report any bugs or feature requests to C<bug-address-postcode-uk at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Address-PostCode-UK>.
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 Address::PostCode::UK::Location

lib/Address/PostCode/UK/Place.pm  view on Meta::CPAN

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Address-PostCode-UK>

=head1 BUGS

Please report any bugs or feature requests to C<bug-address-postcode-uk at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Address-PostCode-UK>.
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 Address::PostCode::UK::Place

lib/Address/PostCode/UK/Place/Constituency.pm  view on Meta::CPAN

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Address-PostCode-UK>

=head1 BUGS

Please report any bugs or feature requests to C<bug-address-postcode-uk at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Address-PostCode-UK>.
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 Address::PostCode::UK::Place::Constituency

lib/Address/PostCode/UK/Place/Council.pm  view on Meta::CPAN

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Address-PostCode-UK>

=head1 BUGS

Please report any bugs or feature requests to C<bug-address-postcode-uk at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Address-PostCode-UK>.
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 Address::PostCode::UK::Place::Council

lib/Address/PostCode/UK/Place/Geo.pm  view on Meta::CPAN

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Address-PostCode-UK>

=head1 BUGS

Please report any bugs or feature requests to C<bug-address-postcode-uk at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Address-PostCode-UK>.
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 Address::PostCode::UK::Place::Geo

lib/Address/PostCode/UK/Place/Ward.pm  view on Meta::CPAN

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Address-PostCode-UK>

=head1 BUGS

Please report any bugs or feature requests to C<bug-address-postcode-uk at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Address-PostCode-UK>.
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 Address::PostCode::UK::Place::Ward

t/01-address-postcode-uk.t  view on Meta::CPAN

#!perl -T
use 5.006;
use strict;
use warnings FATAL => 'all';
use Test::More tests => 4;
use Address::PostCode::UK;

my $address = Address::PostCode::UK->new;

eval { $address->details; };
like($@, qr/ERROR: Missing required param 'post code'/);

eval { $address->details(1234); };
like($@, qr/ERROR: Invalid format for UK post code/);

eval { $address->details('XYZ12345'); };
like($@, qr/ERROR: Invalid format for UK post code/);

eval { $address->details('12345XYZ'); };
like($@, qr/ERROR: Invalid format for UK post code/);



( run in 2.240 seconds using v1.01-cache-2.11-cpan-5735350b133 )