| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 9 |  |  | 9 |  | 44 | use strict; | 
|  | 9 |  |  |  |  | 15 |  | 
|  | 9 |  |  |  |  | 304 |  | 
| 2 | 9 |  |  | 9 |  | 44 | use warnings; | 
|  | 9 |  |  |  |  | 76 |  | 
|  | 9 |  |  |  |  | 462 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | package WebService::Nestoria::Search::Response; | 
| 5 |  |  |  |  |  |  | $WebService::Nestoria::Search::Response::VERSION = '1.022010'; | 
| 6 | 9 |  |  | 9 |  | 5320 | use WebService::Nestoria::Search::Result; | 
|  | 9 |  |  |  |  | 21 |  | 
|  | 9 |  |  |  |  | 232 |  | 
| 7 | 9 |  |  | 9 |  | 48 | use Carp; | 
|  | 9 |  |  |  |  | 18 |  | 
|  | 9 |  |  |  |  | 615 |  | 
| 8 | 9 |  |  | 9 |  | 49 | use URI; | 
|  | 9 |  |  |  |  | 14 |  | 
|  | 9 |  |  |  |  | 8044 |  | 
| 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.022010 | 
| 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 | 45 |  |  | 45 | 0 | 149 | my $class = shift; | 
| 24 | 45 |  |  |  |  | 139 | my $self; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 45 |  |  |  |  | 220 | $self->{data} = shift; | 
| 27 | 45 |  |  |  |  | 333 | $self->{raw} = shift; | 
| 28 | 45 |  |  |  |  | 225 | $self->{next_iterator} = 0; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 45 |  | 100 |  |  | 383 | my $listings = $self->{data}{response}{listings} || []; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 45 |  |  |  |  | 181 | $self->{results} = []; | 
| 33 | 45 |  |  |  |  | 373 | for( my $i = 0; $i < @$listings; $i++ ) { | 
| 34 | 512 |  |  |  |  | 1300 | my $result = {}; | 
| 35 | 512 |  |  |  |  | 1416 | $result->{listing} = $listings->[$i]; | 
| 36 | 512 |  |  |  |  | 1170 | $result->{ordinal} = $i; | 
| 37 | 512 |  |  |  |  | 1202 | $result->{response} = $self->{data}; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 512 |  |  |  |  | 2402 | $self->{results}[$i] | 
| 40 |  |  |  |  |  |  | = WebService::Nestoria::Search::Result->new($result); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 45 |  |  |  |  | 1855 | 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 | 1084 | my $self = shift; | 
| 57 | 5 |  |  |  |  | 44 | return $self->{raw}; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub get_json { | 
| 61 | 2 |  |  | 2 | 0 | 2197 | my $self = shift; | 
| 62 | 2 |  |  |  |  | 11 | return $self->get_raw; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub get_xml { | 
| 66 | 2 |  |  | 2 | 0 | 1870 | my $self = shift; | 
| 67 | 2 |  |  |  |  | 11 | 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 | 26 |  |  | 26 | 1 | 1940 | my $self = shift; | 
| 78 | 26 |  |  |  |  | 372 | 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 | 175 | my $self = shift; | 
| 99 | 13 |  |  |  |  | 208 | 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 | 4 | my $self = shift; | 
| 110 | 1 |  |  |  |  | 9 | 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 | 3 | my $self = shift; | 
| 130 | 1 |  | 33 |  |  | 19 | 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 | 5 |  |  | 5 | 1 | 19 | my $self = shift; | 
| 142 | 5 |  |  |  |  | 249 | 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 | 58 | my $self = shift; | 
| 153 | 19 |  |  |  |  | 53 | return scalar @{$self->{results}}; | 
|  | 19 |  |  |  |  | 165 |  | 
| 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 | 20 | my $self = shift; | 
| 164 | 1 |  |  |  |  | 10 | 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 |  |  |  |  | 22 | $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 | 4 | 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 |  |  |  |  | 22 | $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 | 602 | my $self = shift; | 
| 205 |  |  |  |  |  |  |  | 
| 206 | 3 | 50 |  |  |  | 13 | if ($self->{data}{response}{link_to_url}) { | 
| 207 | 3 |  |  |  |  | 16 | 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 | 559 | 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 | 4 |  |  | 4 | 1 | 628 | my $self = shift; | 
| 235 | 4 |  |  |  |  | 10 | return @{$self->{results}}; | 
|  | 4 |  |  |  |  | 53 |  | 
| 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 | 4960 | my $self = shift; | 
| 250 |  |  |  |  |  |  |  | 
| 251 | 13 | 100 |  |  |  | 19 | if ( $self->{next_iterator} < @{$self->{results}} ) { | 
|  | 13 |  |  |  |  | 44 |  | 
| 252 | 11 |  |  |  |  | 35 | return $self->{results}[$self->{next_iterator}++]; | 
| 253 |  |  |  |  |  |  | } | 
| 254 |  |  |  |  |  |  | else { | 
| 255 | 2 |  |  |  |  | 8 | 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; |