File Coverage

blib/lib/WebService/Geograph/API.pm
Criterion Covered Total %
statement 35 54 64.8
branch 6 18 33.3
condition 4 6 66.6
subroutine 8 10 80.0
pod 4 4 100.0
total 57 92 61.9


line stmt bran cond sub pod time code
1             package WebService::Geograph::API;
2              
3 2     2   50372 use strict;
  2         7  
  2         264  
4 2     2   12 use warnings ;
  2         6  
  2         65  
5              
6 2     2   1247 use WebService::Geograph::Request ;
  2         21  
  2         61  
7 2     2   1312 use WebService::Geograph::Response ;
  2         7  
  2         59  
8              
9 2     2   2549 use LWP::UserAgent ;
  2         37704  
  2         87  
10 2     2   20 use Data::Dumper ;
  2         3  
  2         1193  
11              
12             our @ISA = qw ( LWP::UserAgent ) ;
13             our $VERSION = '0.05' ;
14              
15              
16             sub new {
17 2     2 1 966 my $class = shift ;
18 2         24 my $rh_options = shift ;
19            
20 2 100 66     14 unless (defined ($rh_options->{key}) and ($rh_options->{key})) {
21 1         112 warn "You must obtain a valid key before using the Geograph API service.\n" .
22             "Visit http://www.geograph.org.uk/help/api for more information.\n" ;
23 1         6 return undef ;
24             }
25            
26             # Please do not change the following parameter.
27             # It does not provide geograph.co.uk with any personal information
28             # but helps them track usage of this module.
29            
30 1         4 my %options = ( 'agent' => 'WebService::Geograph::API' ) ;
31 1         11 my $self = new LWP::UserAgent ( %options );
32 1         3230 $self->{key} = $rh_options->{key} ;
33            
34 1         4 bless $self, $class ;
35 1         5 return $self ;
36              
37             }
38              
39             sub lookup {
40 2     2 1 777 my ($self, $mode, $args) = (@_) ;
41            
42 2 100 66     15 return unless ((defined $mode) && (defined $args)) ;
43 1 50       5 return unless ref $args eq 'HASH' ;
44            
45 1         9 $args->{key} = $self->{key} ;
46            
47 1         10 my $request = new WebService::Geograph::Request ( $mode , $args ) ;
48 1 50       4 if (defined $request) {
49 0         0 $self->execute_request($request) ;
50             } else {
51 1         3 return ;
52             }
53             }
54              
55             sub execute_request {
56 0     0 1   my ($self, $request) = (@_) ;
57 0           my $url = $request->encode_args() ;
58            
59 0           my $response = $self->get($url) ;
60 0           bless $response, 'WebService::Geograph::Response' ;
61            
62 0 0         unless ($response->{_rc} = 200) {
63 0           $response->set_fail(0, "API returned a non-200 status code: ($response->{_rc})") ;
64 0           return $response ;
65             }
66            
67 0           $self->create_results_node($request, $response) ;
68            
69             }
70              
71             sub create_results_node {
72 0     0 1   my ($self, $request, $response) = (@_) ;
73            
74 0 0         if ($request->{mode} eq 'csv') {
    0          
75 0 0         if (defined $response->{_content}) {
76 0           my $csv_data = $response->{_content} ;
77 0           $response->set_success($csv_data) ;
78 0           return $response ;
79             }
80             }
81            
82             elsif ($request->{mode} eq 'search') {
83 0 0         if (defined $response->{_previous}->{_headers}->{location}) {
84 0           my $location = $response->{_previous}->{_headers}->{location} ;
85 0           $response->set_success($location) ;
86 0           return $response ;
87             }
88             }
89            
90            
91            
92            
93            
94             }
95              
96             =head1 NAME
97              
98             WebService::Geograph::API - Perl interface to the Geograph.co.uk API
99              
100             =head1 SYNOPSIS
101              
102             use WebService::Geograph::API;
103            
104             my $api = new WebService::Geograph::API ( { 'key' => 'your_api_key_here'} ) ;
105              
106             my $rv = $api->lookup ( 'csv', { 'i' => 12345,
107             'll' => 1,
108             'thumb' => 1,
109             }) ;
110              
111             my $data = $rd->{results} ;
112              
113             =head1 DESCRIPTION
114              
115             This module provides a simple interface to using the geograph.co.uk API service.
116              
117             The actual core class, C is a subclass of C so
118             all the usual parameters apply.
119              
120             =head2 METHODS
121              
122             =over 4
123              
124             =item C
125              
126             The following constructing method creates a new C object and returns it.
127             It accepts a single parameter, I, which is the API key for the service. You B obtain
128             a valid key otherwise you will not be able to use the API.
129              
130             Obtaining a key is free. See : http://www.geograph.org.uk/help/api#api for more information.
131              
132             my $api = new WebService::Geograph::API ( { 'key' => 'your_api_key_here'} ) ;
133              
134             =item C
135              
136             Creates a new C object and executes it.
137              
138             my $rv = $api->lookup ( 'csv', { 'i' => 12345, 'll' => 1, } ) ;
139            
140             or
141            
142             my $rv = $api->lookup ( 'search ', { q = 'W12 8JL' } ) ;
143              
144             Valid modes at the moment include I for exporting CSV and I for creating custom searches
145             and obtaining their 'i' number. A very good and detailed overview of the various parameters they
146             support can be find on the API page located at : http://www.geograph.org.uk/help/api#api
147              
148             This method creates and returns a new C object. The object is
149             a standard C object with some additional fields. If no errors occur, the
150             results of the query will be located inside I ;
151              
152             my $data = $rv->{results} ;
153              
154             =item C
155              
156             Internal method that executes a request and blesses the response into a
157             C object.
158              
159             =item C
160              
161             Intenal method which assigns the actual data returned from the API within the
162             response objects 'results' key.
163              
164             =back
165              
166             =head1 SUPPORT
167              
168             Please feel free to send any bug reports and suggestions to my email listed below.
169              
170             For more information and useless facts on my life, you can also check my blog:
171              
172             http://idaru.blogspot.com/
173              
174             =head1 AUTHOR
175              
176             Spiros Denaxas
177             CPAN ID: SDEN
178             Lokku Ltd ( http://www.nestoria.co.uk )
179             s [dot] denaxas [@] gmail [dot]com
180              
181             =head1 COPYRIGHT
182              
183             This program is free software; you can redistribute
184             it and/or modify it under the same terms as Perl itself.
185              
186             The full text of the license can be found in the
187             LICENSE file included with this module.
188              
189             =head1 SEE ALSO
190              
191             L, L, L, L
192              
193             =cut
194              
195             #################### main pod documentation end ###################
196              
197             1;
198