Address-PostCode-UserAgent

 view release on metacpan or  search on metacpan

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

package Address::PostCode::UserAgent;

$Address::PostCode::UserAgent::VERSION = '0.09';

=head1 NAME

Address::PostCode::UserAgent - User agent for Address::PostCode::* family.

=head1 VERSION

Version 0.09

=cut

use 5.006;
use Data::Dumper;

use HTTP::Tiny;
use Address::PostCode::UserAgent::Exception;

use Moo;
use namespace::autoclean;

has 'ua'=> (is => 'rw', default => sub { HTTP::Tiny->new(agent => "Address-PostCode-UserAgent/v1"); } );

=head1 DESCRIPTION

The L<Address::PostCode::UserAgent> module is the core library Address::PostCode::* family.

=head1 METHODS

=head2 get($url, \%headers)

It requires URL and optionally headers. It returns the standard response.On error
throws exception of type L<Address::PostCode::UserAgent::Exception>.

=cut

sub get {
    my ($self, $url, $headers) = @_;

    die "ERROR: Headers have to be hash ref." if (defined $headers && ref($headers) ne 'HASH');
    my $ua = $self->ua;
    my $response = (defined $headers)
                   ?
                   ($ua->request('GET', $url, { headers => $headers }))
                   :
                   ($ua->request('GET', $url));

    my @caller = caller(1);
    @caller = caller(2) if $caller[3] eq '(eval)';

    unless ($response->{success}) {
        Address::PostCode::UserAgent::Exception->throw({
            method      => $caller[3],
            message     => "request to API failed",
            code        => $response->{status},
            reason      => $response->{reason},
            filename    => $caller[1],
            line_number => $caller[2] });
    }

    return $response;
}

=head1 AUTHOR

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

=head1 REPOSITORY

L<https://github.com/Manwar/Address-PostCode-UserAgent>

=head1 BUGS

Please report any bugs or feature requests to C<bug-address-postcode-useragent at

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.219 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )