File Coverage

blib/lib/Address/PostCode/Australia.pm
Criterion Covered Total %
statement 31 45 68.8
branch 2 8 25.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 44 64 68.7


line stmt bran cond sub pod time code
1             package Address::PostCode::Australia;
2              
3             $Address::PostCode::Australia::VERSION = '0.12';
4             $Address::PostCode::Australia::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Address::PostCode::Australia - Interface to the Australia PostCode.
9              
10             =head1 VERSION
11              
12             Version 0.12
13              
14             =cut
15              
16 4     4   112680 use 5.006;
  4         26  
17 4     4   2094 use JSON;
  4         39822  
  4         19  
18 4     4   2451 use Data::Dumper;
  4         23947  
  4         241  
19 4     4   1500 use Address::PostCode::UserAgent;
  4         385056  
  4         129  
20 4     4   1743 use Address::PostCode::Australia::Place;
  4         11  
  4         121  
21 4     4   1567 use Address::PostCode::Australia::Params qw(validate);
  4         9  
  4         213  
22              
23 4     4   25 use Moo;
  4         8  
  4         32  
24 4     4   1122 use namespace::autoclean;
  4         31  
  4         38  
25             extends 'Address::PostCode::UserAgent';
26              
27             our $BASE_URL = 'https://auspost.com.au/api/postcode/search.json';
28             has 'auth_key' => (is => 'ro', required => 1);
29              
30             =head1 DESCRIPTION
31              
32             Interface to the API provided by L.
33              
34             To use the API, you would need auth key, which you can get it L.
35              
36             More details can be found L.
37              
38             =head1 SYNOPSIS
39              
40             use strict; use warnings;
41             use Address::PostCode::Australia;
42              
43             my $auth_key = 'Your Auth Key';
44             my $postcode = 3002;
45             my $address = Address::PostCode::Australia->new({ auth_key => $auth_key });
46             my $places = $address->details({ postcode => $postcode });
47              
48             print "Location: ", $places->[0]->location, "\n";
49             print "State : ", $places->[0]->state, "\n";
50              
51             =head1 CONSTRUCTOR
52              
53             The only parameter requires is the auth key.
54              
55             use strict; use warnings;
56             use Address::PostCode::Australia;
57              
58             my $auth_key = 'Your Auth Key';
59             my $address = Address::PostCode::Australia->new({ auth_key => $auth_key });
60              
61             =head2 details(\%params)
62              
63             It returns ref to list of objects of type L
64             on success. The parameters requires are list below:
65              
66             +----------+----------------------------------------------------------------+
67             | Name | Description |
68             +----------+----------------------------------------------------------------+
69             | postcode | Mandatory parameter unless location is passed. |
70             | | |
71             | location | Mandatory paramerer unless postcode is passed. |
72             | | |
73             | state | Optional parameter. |
74             +----------+----------------------------------------------------------------+
75              
76             =cut
77              
78             sub details {
79 5     5 1 3795 my ($self, $params) = @_;
80              
81 5         13 my $keys = { postcode => 0, location => 0, state => 0 };
82 5         10 my $url = _get_url($keys, $params);
83 0         0 my $response = $self->get($url, { 'auth-key' => $self->auth_key });
84 0         0 my $contents = from_json($response->{'content'});
85              
86 0         0 my $places = [];
87 0 0       0 if (ref($contents->{'localities'}->{'locality'}) eq 'ARRAY') {
88 0         0 foreach my $location (@{$contents->{'localities'}->{'locality'}}) {
  0         0  
89 0         0 push @$places, Address::PostCode::Australia::Place->new($location);
90             }
91             }
92             else {
93 0         0 push @$places, Address::PostCode::Australia::Place->new($contents->{'localities'}->{'locality'});
94             }
95              
96 0         0 return $places;
97             }
98              
99             #
100             #
101             # PRIVATE METHODS
102              
103             sub _get_url {
104 5     5   10 my ($keys, $values) = @_;
105              
106 5         15 validate($keys, $values);
107              
108 1         2 my $url = $BASE_URL;
109 1 50       5 if (exists $values->{'postcode'}) {
    50          
110 0         0 $url .= sprintf("?q=%d", $values->{'postcode'});
111             }
112             elsif (exists $values->{'location'}) {
113 0         0 $url .= sprintf("?q=%s", $values->{'location'});
114             }
115             else {
116 1         4 die "ERROR: Missing required key postcode/location.\n";
117             }
118              
119 0 0         if (exists $values->{'state'}) {
120 0           $url .= sprintf("&state=%s", $values->{'state'});
121             }
122              
123 0           return $url;
124             }
125              
126             =head1 AUTHOR
127              
128             Mohammad S Anwar, C<< >>
129              
130             =head1 REPOSITORY
131              
132             L
133              
134             =head1 BUGS
135              
136             Please report any bugs or feature requests to C,
137             or through the web interface at L.
138             I will be notified, and then you'll automatically be notified of progress on your
139             bug as I make changes.
140              
141             =head1 SUPPORT
142              
143             You can find documentation for this module with the perldoc command.
144              
145             perldoc Address::PostCode::Australia
146              
147             You can also look for information at:
148              
149             =over 4
150              
151             =item * RT: CPAN's request tracker (report bugs here)
152              
153             L
154              
155             =item * AnnoCPAN: Annotated CPAN documentation
156              
157             L
158              
159             =item * CPAN Ratings
160              
161             L
162              
163             =item * Search CPAN
164              
165             L
166              
167             =back
168              
169             =head1 LICENSE AND COPYRIGHT
170              
171             Copyright (C) 2014 - 2015 Mohammad S Anwar.
172              
173             This program is free software; you can redistribute it and / or modify it under
174             the terms of the the Artistic License (2.0). You may obtain a copy of the full
175             license at:
176              
177             L
178              
179             Any use, modification, and distribution of the Standard or Modified Versions is
180             governed by this Artistic License.By using, modifying or distributing the Package,
181             you accept this license. Do not use, modify, or distribute the Package, if you do
182             not accept this license.
183              
184             If your Modified Version has been derived from a Modified Version made by someone
185             other than you,you are nevertheless required to ensure that your Modified Version
186             complies with the requirements of this license.
187              
188             This license does not grant you the right to use any trademark, service mark,
189             tradename, or logo of the Copyright Holder.
190              
191             This license includes the non-exclusive, worldwide, free-of-charge patent license
192             to make, have made, use, offer to sell, sell, import and otherwise transfer the
193             Package with respect to any patent claims licensable by the Copyright Holder that
194             are necessarily infringed by the Package. If you institute patent litigation
195             (including a cross-claim or counterclaim) against any party alleging that the
196             Package constitutes direct or contributory patent infringement,then this Artistic
197             License to you shall terminate on the date that such litigation is filed.
198              
199             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
200             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
201             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
202             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
203             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
204             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
205             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
206              
207             =cut
208              
209             1; # End of Address::PostCode::Australia