File Coverage

blib/lib/Address/PostCode/UserAgent.pm
Criterion Covered Total %
statement 17 26 65.3
branch 0 8 0.0
condition 0 3 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 24 45 53.3


line stmt bran cond sub pod time code
1             package Address::PostCode::UserAgent;
2              
3             $Address::PostCode::UserAgent::VERSION = '0.09';
4              
5             =head1 NAME
6              
7             Address::PostCode::UserAgent - User agent for Address::PostCode::* family.
8              
9             =head1 VERSION
10              
11             Version 0.09
12              
13             =cut
14              
15 3     3   69969 use 5.006;
  3         22  
16 3     3   1822 use Data::Dumper;
  3         20561  
  3         206  
17              
18 3     3   2043 use HTTP::Tiny;
  3         152156  
  3         186  
19 3     3   1487 use Address::PostCode::UserAgent::Exception;
  3         16  
  3         121  
20              
21 3     3   28 use Moo;
  3         6  
  3         17  
22 3     3   931 use namespace::autoclean;
  3         8  
  3         28  
23              
24             has 'ua'=> (is => 'rw', default => sub { HTTP::Tiny->new(agent => "Address-PostCode-UserAgent/v1"); } );
25              
26             =head1 DESCRIPTION
27              
28             The L module is the core library Address::PostCode::* family.
29              
30             =head1 METHODS
31              
32             =head2 get($url, \%headers)
33              
34             It requires URL and optionally headers. It returns the standard response.On error
35             throws exception of type L.
36              
37             =cut
38              
39             sub get {
40 0     0 1   my ($self, $url, $headers) = @_;
41              
42 0 0 0       die "ERROR: Headers have to be hash ref." if (defined $headers && ref($headers) ne 'HASH');
43 0           my $ua = $self->ua;
44 0 0         my $response = (defined $headers)
45             ?
46             ($ua->request('GET', $url, { headers => $headers }))
47             :
48             ($ua->request('GET', $url));
49              
50 0           my @caller = caller(1);
51 0 0         @caller = caller(2) if $caller[3] eq '(eval)';
52              
53 0 0         unless ($response->{success}) {
54             Address::PostCode::UserAgent::Exception->throw({
55             method => $caller[3],
56             message => "request to API failed",
57             code => $response->{status},
58             reason => $response->{reason},
59 0           filename => $caller[1],
60             line_number => $caller[2] });
61             }
62              
63 0           return $response;
64             }
65              
66             =head1 AUTHOR
67              
68             Mohammad S Anwar, C<< >>
69              
70             =head1 REPOSITORY
71              
72             L
73              
74             =head1 BUGS
75              
76             Please report any bugs or feature requests to C
77             rt.cpan.org>, or through the web interface at L.
78             I will be notified, and then you'll automatically be notified of progress on your
79             bug as I make changes.
80              
81             =head1 SUPPORT
82              
83             You can find documentation for this module with the perldoc command.
84              
85             perldoc Address::PostCode::UserAgent
86              
87             You can also look for information at:
88              
89             =over 4
90              
91             =item * RT: CPAN's request tracker (report bugs here)
92              
93             L
94              
95             =item * AnnoCPAN: Annotated CPAN documentation
96              
97             L
98              
99             =item * CPAN Ratings
100              
101             L
102              
103             =item * Search CPAN
104              
105             L
106              
107             =back
108              
109             =head1 LICENSE AND COPYRIGHT
110              
111             Copyright (C) 2014 - 2015 Mohammad S Anwar.
112              
113             This program is free software; you can redistribute it and / or modify it under
114             the terms of the the Artistic License (2.0). You may obtain a copy of the full
115             license at:
116              
117             L
118              
119             Any use, modification, and distribution of the Standard or Modified Versions is
120             governed by this Artistic License.By using, modifying or distributing the Package,
121             you accept this license. Do not use, modify, or distribute the Package, if you do
122             not accept this license.
123              
124             If your Modified Version has been derived from a Modified Version made by someone
125             other than you,you are nevertheless required to ensure that your Modified Version
126             complies with the requirements of this license.
127              
128             This license does not grant you the right to use any trademark, service mark,
129             tradename, or logo of the Copyright Holder.
130              
131             This license includes the non-exclusive, worldwide, free-of-charge patent license
132             to make, have made, use, offer to sell, sell, import and otherwise transfer the
133             Package with respect to any patent claims licensable by the Copyright Holder that
134             are necessarily infringed by the Package. If you institute patent litigation
135             (including a cross-claim or counterclaim) against any party alleging that the
136             Package constitutes direct or contributory patent infringement,then this Artistic
137             License to you shall terminate on the date that such litigation is filed.
138              
139             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
140             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
141             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
142             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
143             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
144             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
145             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146              
147             =cut
148              
149             1; # End of Address::PostCode::UserAgent