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