File Coverage

blib/lib/WebService/Nestoria/Search/Response.pm
Criterion Covered Total %
statement 69 71 97.1
branch 3 4 75.0
condition 3 5 60.0
subroutine 23 23 100.0
pod 15 18 83.3
total 113 121 93.3


line stmt bran cond sub pod time code
1 9     9   44 use strict;
  9         12  
  9         235  
2 9     9   45 use warnings;
  9         73  
  9         446  
3              
4             package WebService::Nestoria::Search::Response;
5             $WebService::Nestoria::Search::Response::VERSION = '1.022011';
6 9     9   5147 use WebService::Nestoria::Search::Result;
  9         21  
  9         263  
7 9     9   49 use Carp;
  9         14  
  9         599  
8 9     9   46 use URI;
  9         13  
  9         7732  
9              
10             =head1 NAME
11              
12             WebService::Nestoria::Search::Response - Container object for the result set of a query to the Nestoria Search API.
13              
14             =head1 VERSION
15              
16             version 1.022011
17              
18             This package is used by WebService::Nestoria::Search and a WebService::Nestoria::Search::Response object should never need to be explicitly created by the user.
19              
20             =cut
21              
22             sub new {
23 53     53 0 84 my $class = shift;
24 53         85 my $self;
25              
26 53         126 $self->{data} = shift;
27 53         277 $self->{raw} = shift;
28 53         94 $self->{next_iterator} = 0;
29              
30 53   100     208 my $listings = $self->{data}{response}{listings} || [];
31              
32 53         112 $self->{results} = [];
33 53         220 for( my $i = 0; $i < @$listings; $i++ ) {
34 592         963 my $result = {};
35 592         1025 $result->{listing} = $listings->[$i];
36 592         840 $result->{ordinal} = $i;
37 592         982 $result->{response} = $self->{data};
38              
39 592         1700 $self->{results}[$i]
40             = WebService::Nestoria::Search::Result->new($result);
41             }
42              
43 53         517 return bless $self, $class;
44              
45             }
46              
47             =head1 Functions
48              
49             =head2 get_raw
50              
51             Returns the raw data returned by the Nestoria API. By default this will be JSON (JavaScript Object Notation.) C and C are aliases to C.
52              
53             =cut
54              
55             sub get_raw {
56 5     5 1 656 my $self = shift;
57 5         31 return $self->{raw};
58             }
59              
60             sub get_json {
61 2     2 0 1048 my $self = shift;
62 2         8 return $self->get_raw;
63             }
64              
65             sub get_xml {
66 2     2 0 1371 my $self = shift;
67 2         9 return $self->get_raw;
68             }
69              
70             =head2 status_code
71              
72             Returns the HTTP status code of the response. 200 on success, various other numbers on errors.
73              
74             =cut
75              
76             sub status_code {
77 20     20 1 893 my $self = shift;
78 20         127 return $self->{data}{response}{status_code};
79             }
80              
81             =head2 application_response_code
82              
83             Returns the application response code, which is much more useful than the status_code for determining the correctness of the response.
84              
85             Numbers starting 1xx are successes
86              
87             Numbers starting 2xx are location errors
88              
89             Numbers starting 5xx are internal server errors
90              
91             Numbers starting 9xx are invalid request errors
92              
93             For more information read the Nestoria API documentation: http://www.nestoria.co.uk/help/api-return-codes
94              
95             =cut
96              
97             sub application_response_code {
98 13     13 1 86 my $self = shift;
99 13         79 return $self->{data}{response}{application_response_code};
100             }
101              
102             =head2 application_response_text
103              
104             Returns the text description of the application response code. For example if the application response code is 100, the text is "one unambiguous location".
105              
106             =cut
107              
108             sub application_response_text {
109 1     1 1 3 my $self = shift;
110 1         5 return $self->{data}{response}{application_response_text};
111             }
112              
113             =head2 is_success
114              
115             Uses the status_code and application_response_code, and returns true if the request was a success and false otherwise. Concept stolen straight from LWP::UserAgent.
116              
117             if ($response->is_success) {
118             foreach my $result ($response->results) {
119             # do stuff...
120             }
121             }
122             else {
123             die $response->application_response_text;
124             }
125              
126             =cut
127              
128             sub is_success {
129 1     1 1 2 my $self = shift;
130 1   33     5 return ($self->status_code == 200)
131             && ($self->application_response_code =~ m/^1/);
132             }
133              
134             =head2 get_hashref
135              
136             Returns a reference to a hash that contains exactly what the response from the Nestoria API gave, converted from JSON into a hashref with JSON::from_json()
137              
138             =cut
139              
140             sub get_hashref {
141 12     12 1 24 my $self = shift;
142 12         130 return $self->{data};
143             }
144              
145             =head2 count
146              
147             Returns the number of listings found.
148              
149             =cut
150              
151             sub count {
152 19     19 1 35 my $self = shift;
153 19         24 return scalar @{$self->{results}};
  19         99  
154             }
155              
156             =head2 attribution
157              
158             Returns a reference to a hash that contains the 'attribution' data returend by the server. Allows users to link back to Nestoria.
159              
160             =cut
161              
162             sub attribution {
163 1     1 1 2 my $self = shift;
164 1         7 return $self->{data}{response}{attribution};
165             }
166              
167             =head2 attribution_html
168              
169             Returns the attribution formatted in HTML for ease of use on websites.
170              
171             =cut
172              
173             sub attribution_html {
174 1     1 1 4 my $self = shift;
175             return sprintf '',
176             $self->{data}{response}{attribution}{link_to_img},
177             $self->{data}{response}{attribution}{img_height},
178             $self->{data}{response}{attribution}{img_width},
179 1         14 $self->{data}{response}{attribution}{img_url};
180             }
181              
182             =head2 attribution_xhtml
183              
184             Returns the attribution formatted in XHTML for ease of use on websites.
185              
186             =cut
187              
188             sub attribution_xhtml {
189 1     1 1 3 my $self = shift;
190             return sprintf '',
191             $self->{data}{response}{attribution}{link_to_img},
192             $self->{data}{response}{attribution}{img_url},
193             $self->{data}{response}{attribution}{img_height},
194 1         16 $self->{data}{response}{attribution}{img_width};
195             }
196              
197             =head2 nestoria_site_uri
198              
199             Returns a URI object representing the URL for the Nestoria results page for the request.
200              
201             =cut
202              
203             sub nestoria_site_uri {
204 3     3 1 556 my $self = shift;
205              
206 3 50       11 if ($self->{data}{response}{link_to_url}) {
207 3         15 return URI->new($self->{data}{response}{link_to_url});
208             }
209             else {
210 0         0 carp "No 'link_to_url' found in the response";
211             }
212              
213 0         0 return;
214             }
215              
216             =head2 nestoria_site_url
217              
218             Returns a URL for the Nestoria results page for the request.
219              
220             =cut
221              
222             sub nestoria_site_url {
223 1     1 1 620 my $self = shift;
224 1         5 return $self->nestoria_site_uri->as_string;
225             }
226              
227             =head2 results
228              
229             Returns an array of WebService::Nestoria::Search::Result objects, each containing data about a single listing returned by the Nestoria API.
230              
231             =cut
232              
233             sub results {
234 11     11 1 568 my $self = shift;
235 11         18 return @{$self->{results}};
  11         103  
236             }
237              
238             =head2 next_result
239              
240             Returns the next WebService::Nestoria::Search::Result object to be fetched. When out of listings returns C, making it suitable for use in while loops.
241              
242             while ( $listing = $response->next_result ) {
243             # do something;
244             }
245              
246             =cut
247              
248             sub next_result {
249 13     13 1 4834 my $self = shift;
250              
251 13 100       22 if ( $self->{next_iterator} < @{$self->{results}} ) {
  13         39  
252 11         39 return $self->{results}[$self->{next_iterator}++];
253             }
254             else {
255 2         29 return;
256             }
257             }
258              
259             =head2 reset
260              
261             Resets the counter used for next_result.
262              
263             =cut
264              
265             sub reset {
266 1     1 1 3 my $self = shift;
267 1         4 $self->{next_iterator} = 0;
268             }
269              
270             =head1 Copyright
271              
272             Copyright (C) 2009 Lokku Ltd.
273              
274             =head1 Author
275              
276             Alex Balhatchet (alex@lokku.com)
277              
278             Patches supplied by Yoav Felberbaum and Alistair Francis.
279              
280             =cut
281              
282             1;