File Coverage

blib/lib/WebService/Geocodio/Request.pm
Criterion Covered Total %
statement 21 40 52.5
branch 0 4 0.0
condition n/a
subroutine 7 10 70.0
pod 2 2 100.0
total 30 56 53.5


line stmt bran cond sub pod time code
1 6     6   47788 use strict;
  6         12  
  6         142  
2 6     6   29 use warnings;
  6         11  
  6         210  
3              
4             package WebService::Geocodio::Request;
5             $WebService::Geocodio::Request::VERSION = '0.05';
6 6     6   24 use Moo::Role;
  6         17  
  6         28  
7 6     6   1764 use strictures 2;
  6         30  
  6         193  
8 6     6   4129 use HTTP::Tiny;
  6         235429  
  6         208  
9 6     6   39 use Carp qw(confess);
  6         10  
  6         252  
10 6     6   2103 use WebService::Geocodio::Location;
  6         18  
  6         2285  
11              
12             with 'WebService::Geocodio::JSON';
13              
14             # ABSTRACT: A request role for Geocod.io
15              
16              
17             has 'ua' => (
18             is => 'ro',
19             lazy => 1,
20             default => sub { HTTP::Tiny->new(
21             agent => "WebService-Geocodio ",
22             default_headers => { 'Content-Type' => 'application/json' },
23             ) },
24             );
25              
26              
27             has 'base_url' => (
28             is => 'ro',
29             lazy => 1,
30             default => sub { 'http://api.geocod.io/v1.2/' },
31             );
32              
33              
34             sub send_forward {
35 0     0 1   my $self = shift;
36              
37 0           $self->_request('geocode', $self->encode(@_));
38             }
39              
40              
41             sub send_reverse {
42 0     0 1   my $self = shift;
43              
44 0           $self->_request('reverse', $self->encode(@_));
45             }
46              
47             sub _request {
48 0     0     my ($self, $op, $content) = @_;
49              
50 0           my $url;
51 0 0         if ( $self->has_fields ) {
52             $url = $self->base_url
53 0           . "$op?fields=" . join(',', @{ $self->fields })
  0            
54             . "&api_key=" . $self->api_key
55             ;
56             }
57             else {
58 0           $url = $self->base_url . "$op?api_key=" . $self->api_key;
59             }
60              
61 0           my $response = $self->ua->request('POST', $url, { content => $content });
62              
63 0 0         if ( $response->{success} ) {
64 0           my $hr = $self->decode($response->{content});
65 0           return map { WebService::Geocodio::Location->new($_) }
66 0           map {; @{$_->{response}->{results}} } @{$hr->{results}};
  0            
  0            
  0            
67             }
68             else {
69             confess "Request to " . $self->base_url . "$op failed: (" .
70 0           $response->{status} . ") - " . $response->{content};
71             }
72             }
73              
74              
75             1;
76              
77             __END__