File Coverage

blib/lib/Address/PostCode/UK.pm
Criterion Covered Total %
statement 38 61 62.3
branch 3 20 15.0
condition n/a
subroutine 13 14 92.8
pod 2 2 100.0
total 56 97 57.7


line stmt bran cond sub pod time code
1             package Address::PostCode::UK;
2              
3             $Address::PostCode::UK::VERSION = '0.18';
4             $Address::PostCode::UK::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Address::PostCode::UK - Interface to the UK PostCode.
9              
10             =head1 VERSION
11              
12             Version 0.18
13              
14             =cut
15              
16 4     4   135311 use 5.006;
  4         35  
17 4     4   2575 use JSON;
  4         49157  
  4         29  
18 4     4   2900 use Data::Dumper;
  4         28523  
  4         268  
19 4     4   1804 use Address::PostCode::UserAgent;
  4         463730  
  4         158  
20 4     4   1961 use Address::PostCode::UK::Location;
  4         15  
  4         137  
21 4     4   1827 use Address::PostCode::UK::Place;
  4         11  
  4         131  
22 4     4   1866 use Address::PostCode::UK::Place::Geo;
  4         12  
  4         137  
23 4     4   1812 use Address::PostCode::UK::Place::Ward;
  4         14  
  4         126  
24 4     4   1841 use Address::PostCode::UK::Place::Council;
  4         12  
  4         131  
25 4     4   1848 use Address::PostCode::UK::Place::Constituency;
  4         12  
  4         121  
26              
27 4     4   26 use Moo;
  4         9  
  4         16  
28 4     4   1305 use namespace::autoclean;
  4         19  
  4         20  
29             extends 'Address::PostCode::UserAgent';
30              
31             our $BASE_URL = 'http://uk-postcodes.com';
32              
33             =head1 DESCRIPTION
34              
35             Interface to the API provided by L.
36              
37             =head1 NOTE
38              
39             Data may be used under the terms of the OS OpenData licence. Northern Ireland
40             postcode data may be used under the terms of the ONSPD licence. Currently, there
41             are no limitations on usage, but they may introduce rate limiting in future.
42              
43             =head1 METHODS
44              
45             =head2 details()
46              
47             It returns an object of type L on success. The only
48             parameter requires is the post code.
49              
50             use strict; use warnings;
51             use Address::PostCode::UK;
52              
53             my $address = Address::PostCode::UK->new;
54             my $post_code = 'Post Code';
55             my $place = $address->details($post_code);
56              
57             print "Latitude : ", $place->geo->lat, "\n";
58             print "Longitude: ", $place->geo->lng, "\n";
59              
60             =cut
61              
62             sub details {
63 4     4 1 5464 my ($self, $post_code) = @_;
64              
65 4 100       17 die "ERROR: Missing required param 'post code'.\n" unless defined $post_code;
66 3 50       30 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);
67              
68 0           $post_code =~ s/\s//g;
69 0           my $url = sprintf("%s/postcode/%s.json", $BASE_URL, $post_code);
70 0           my $response = $self->get($url);
71 0           my $contents = from_json($response->{'content'});
72              
73 0           my ($geo, $ward, $council, $constituency);
74             $geo = Address::PostCode::UK::Place::Geo->new($contents->{'geo'})
75 0 0         if (exists $contents->{'geo'});
76             $ward = Address::PostCode::UK::Place::Ward->new($contents->{'administrative'}->{'ward'})
77 0 0         if (exists $contents->{'administrative'}->{'ward'});
78             $council = Address::PostCode::UK::Place::Council->new($contents->{'administrative'}->{'council'})
79 0 0         if (exists $contents->{'administrative'}->{'council'});
80             $constituency = Address::PostCode::UK::Place::Constituency->new($contents->{'administrative'}->{'constituency'})
81 0 0         if (exists $contents->{'administrative'}->{'constituency'});
82              
83 0           return Address::PostCode::UK::Place->new(
84             'geo' => $geo, 'ward' => $ward, 'council' => $council, 'constituency' => $constituency);
85             }
86              
87             =head2 nearest()
88              
89             It returns ref to a list of objects of type L on
90             success. The required parameters are the post code and distance in miles.
91              
92             use strict; use warnings;
93             use Address::PostCode::UK;
94              
95             my $address = Address::PostCode::UK->new;
96             my $post_code = 'Post Code';
97             my $distance = 1;
98             my $locations = $address->nearest($post_code, $distance);
99              
100             print "Location Latitude : ", $locations->[0]->lat, "\n";
101             print "Location Longitude: ", $locations->[0]->lng, "\n";
102              
103             =cut
104              
105             sub nearest {
106 0     0 1   my ($self, $post_code, $distance) = @_;
107              
108 0 0         die "ERROR: Missing required param 'post code'.\n" unless defined $post_code;
109 0 0         die "ERROR: Missing required param 'distance'.\n" unless defined $distance;
110              
111 0 0         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);
112 0 0         die "ERROR: Invalid distance [$distance].\n" unless ($distance =~ /^[\d+]$/);
113              
114 0           $post_code =~ s/\s//g;
115 0           my $url = sprintf("%s/postcode/nearest?postcode=%s&miles=%d&format=json", $BASE_URL, $post_code, $distance);
116 0           my $response = $self->get($url);
117 0           my $contents = from_json($response->{'content'});
118              
119 0           my $locations = [];
120 0           foreach (@$contents) {
121 0           push @$locations, Address::PostCode::UK::Location->new($_);
122             }
123              
124 0           return $locations;
125             }
126              
127             =head1 AUTHOR
128              
129             Mohammad S Anwar, C<< >>
130              
131             =head1 REPOSITORY
132              
133             L
134              
135             =head1 BUGS
136              
137             Please report any bugs or feature requests to C,
138             or through the web interface at L.
139             I will be notified, and then you'll automatically be notified of progress on your
140             bug as I make changes.
141              
142             =head1 SUPPORT
143              
144             You can find documentation for this module with the perldoc command.
145              
146             perldoc Address::PostCode::UK
147              
148             You can also look for information at:
149              
150             =over 4
151              
152             =item * RT: CPAN's request tracker (report bugs here)
153              
154             L
155              
156             =item * AnnoCPAN: Annotated CPAN documentation
157              
158             L
159              
160             =item * CPAN Ratings
161              
162             L
163              
164             =item * Search CPAN
165              
166             L
167              
168             =back
169              
170             =head1 LICENSE AND COPYRIGHT
171              
172             Copyright (C) 2014 - 2015 Mohammad S Anwar.
173              
174             This program is free software; you can redistribute it and/or modify it under
175             the terms of the the Artistic License (2.0). You may obtain a copy of the full
176             license at:
177              
178             L
179              
180             Any use, modification, and distribution of the Standard or Modified Versions is
181             governed by this Artistic License.By using, modifying or distributing the Package,
182             you accept this license. Do not use, modify, or distribute the Package, if you do
183             not accept this license.
184              
185             If your Modified Version has been derived from a Modified Version made by someone
186             other than you,you are nevertheless required to ensure that your Modified Version
187             complies with the requirements of this license.
188              
189             This license does not grant you the right to use any trademark, service mark,
190             tradename, or logo of the Copyright Holder.
191              
192             This license includes the non-exclusive, worldwide, free-of-charge patent license
193             to make, have made, use, offer to sell, sell, import and otherwise transfer the
194             Package with respect to any patent claims licensable by the Copyright Holder that
195             are necessarily infringed by the Package. If you institute patent litigation
196             (including a cross-claim or counterclaim) against any party alleging that the
197             Package constitutes direct or contributory patent infringement,then this Artistic
198             License to you shall terminate on the date that such litigation is filed.
199              
200             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
201             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
202             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
203             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
204             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
205             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
206             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
207              
208             =cut
209              
210             1; # End of Address::PostCode::UK