File Coverage

blib/lib/Geo/Coder/GoogleMaps/Response.pm
Criterion Covered Total %
statement 15 39 38.4
branch 0 6 0.0
condition 0 15 0.0
subroutine 5 13 38.4
pod 8 8 100.0
total 28 81 34.5


line stmt bran cond sub pod time code
1             package Geo::Coder::GoogleMaps::Response;
2              
3 1     1   24320 use warnings;
  1         2  
  1         34  
4 1     1   6 use strict;
  1         1  
  1         36  
5 1     1   4 use Carp;
  1         2  
  1         79  
6 1     1   2186 use Encode;
  1         13312  
  1         102  
7             use constant {
8 1         484 G_GEO_SUCCESS => 200,
9             G_GEO_BAD_REQUEST => 400,
10             G_GEO_SERVER_ERROR => 500,
11             G_GEO_MISSING_QUERY => 601,
12             G_GEO_MISSING_ADDRESS => 601,
13             G_GEO_UNKNOWN_ADDRESS => 602,
14             G_GEO_UNAVAILABLE_ADDRESS => 603,
15             G_GEO_UNKNOWN_DIRECTIONS => 604,
16             G_GEO_BAD_KEY => 610,
17             G_GEO_TOO_MANY_QUERIES => 620,
18 1     1   9 };
  1         2  
19              
20             =encoding utf-8
21              
22             =head1 NAME
23              
24             Geo::Coder::GoogleMaps::Response - Response object for the L module.
25              
26             =head1 VERSION
27              
28             Version 0.4 (follow L version number)
29              
30             =cut
31              
32             our $VERSION = '0.4';
33              
34             =head1 SYNOPSIS
35              
36             This module provides a convenient way to represent a response for a geocoding request to Google's servers.
37              
38             =head1 CONSTANTS
39              
40             Those constants are giving hints about the status of a geocoding request.
41              
42             G_GEO_SUCCESS: No errors occurred; the address was successfully parsed and its geocode has been returned.
43             G_GEO_BAD_REQUEST: A directions request could not be successfully parsed.
44             G_GEO_SERVER_ERROR: A geocoding, directions or maximum zoom level request could not be successfully processed, yet the exact reason for the failure is not known.
45             G_GEO_MISSING_QUERY: The "location" parameter was either missing or had no value.
46             G_GEO_MISSING_ADDRESS: Synonym for G_GEO_MISSING_QUERY.
47             G_GEO_UNKNOWN_ADDRESS: No corresponding geographic location could be found for the specified address.
48             G_GEO_UNAVAILABLE_ADDRESS: The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.
49             G_GEO_BAD_KEY: The given key is either invalid or does not match the domain for which it was given.
50             G_GEO_TOO_MANY_QUERIES: The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time.
51              
52             =head1 FUNCTIONS
53              
54             =head2 new
55              
56             The object constructor it takes no parameters
57              
58             =cut
59              
60              
61             sub new {
62 0     0 1   my($class, %param) = @_;
63 0 0 0       if( (exists($param{status_code}) && defined($param{status_code}) ) && (exists($param{status_request}) && defined($param{status_request}) ) ){
      0        
      0        
64 0           bless { status => {code=>$param{status_code},request=>$param{status_request}}, placemarks => [] }, $class;
65             }
66             else{
67 0           bless { status => {code=>-1,request=>""}, placemarks => [] }, $class;
68             }
69             }
70              
71             =head2 is_success
72              
73             Return true if the the request was successfull and there is actually some placemarks in the list, false otherwise.
74              
75             If the exact failure reason is needed, please use Geo::Coder::GoogleMaps::Response::status_code() and check with the available constants.
76              
77             unless( $response->is_success() ){
78             print "WARNING: Address is unknown by Google's server !\n" if( $response->status_code() == G_GEO_UNKNOWN_ADDRESS );
79             }
80              
81             =cut
82              
83             sub is_success {
84 0     0 1   my $self = shift;
85 0   0       return ($self->{status}->{code} == G_GEO_SUCCESS && scalar(@{$self->{placemarks}}) >= 1) ;
86             }
87              
88             =head2 status_code
89              
90             Returns the response status code. This code can be tested against the G_GEO_* constants.
91              
92             if( $response->status_code() == Geo::Coder::GoogleMaps::Response::G_GEO_BAD_KEY )
93             print "Please provide a valid Google Maps API key and try again.\n";
94              
95             =cut
96              
97             sub status_code {
98 0     0 1   my $self = shift;
99 0           return $self->{status}->{code};
100             }
101              
102             =head2 status
103              
104             Returns the complete response status. The status is a hashref which looks like that :
105              
106             status => {
107             code => -1,
108             request => ""
109             }
110              
111             =cut
112              
113             sub status {
114 0     0 1   my $self = shift;
115 0           return $self->{status};
116             }
117              
118             =head2 add_placemark
119              
120             Adds the placemark (a L object) given in parameter to the list of placemarks.
121              
122             This methods croak on errors (like if you did not give a proper object in argument).
123              
124             =cut
125              
126             sub add_placemark {
127 0     0 1   my $self = shift;
128 0           my $location = shift;
129 0 0 0       if( defined($location) && UNIVERSAL::isa( $location, "Geo::Coder::GoogleMaps::Location") ){
130 0           push @{$self->{placemarks}},$location;
  0            
131             }
132             else{
133 0           Carp::croak("add_placemark() : takes a Geo::Coder::GoogleMaps::Location as mandatory argument.\n");
134             }
135             }
136              
137             =head2 placemarks
138              
139             Return the complete list of placemarks, or an arrayref depending on the context.
140              
141             In any case the array contains a list of L objects.
142              
143             =cut
144              
145             sub placemarks {
146 0     0 1   my $self = shift;
147 0 0         wantarray ? @{$self->{placemarks}} : [@{$self->{placemarks}}]; # I voluntarly create a new reference to prevent (at last a little...) the deletion of placemarks
  0            
  0            
148             }
149              
150             =head2 clean_placemarks
151              
152             Remove all the placemarks from the response object.
153              
154             =cut
155              
156             sub clean_placemarks {
157 0     0 1   my $self = shift;
158 0           $self->{placemarks} = [];
159             }
160              
161             =head2 clean_status
162              
163             Reset the response object's status to its initial state (undefined).
164              
165             =cut
166              
167             sub clean_status {
168 0     0 1   my $self = shift;
169 0           $self->{status} = {code=>-1,request=>""};
170             }
171              
172             1;
173             __END__