| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Google::Search::Response; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 29 | use Any::Moose; | 
|  | 5 |  |  |  |  | 12 |  | 
|  | 5 |  |  |  |  | 52 |  | 
| 4 | 5 |  |  | 5 |  | 3397 | use Google::Search::Carp; | 
|  | 5 |  |  |  |  | 52 |  | 
|  | 5 |  |  |  |  | 37 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 5 |  |  | 5 |  | 3541 | use Google::Search::Error; | 
|  | 5 |  |  |  |  | 16 |  | 
|  | 5 |  |  |  |  | 152 |  | 
| 7 | 5 |  |  | 5 |  | 5927 | use JSON; my $json = JSON->new; | 
|  | 5 |  |  |  |  | 71656 |  | 
|  | 5 |  |  |  |  | 29 |  | 
| 8 | 5 |  |  | 5 |  | 6132 | use Try::Tiny; | 
|  | 5 |  |  |  |  | 7452 |  | 
|  | 5 |  |  |  |  | 2960 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has http_response => qw/ is ro required 1 isa HTTP::Response /; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | has content => qw/ is rw lazy_build 1 /; | 
| 13 |  |  |  |  |  |  | sub _build_content { | 
| 14 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 15 | 0 |  |  |  |  |  | $self->parse; | 
| 16 | 0 |  |  |  |  |  | return $self->content; | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | has error => qw/ is rw lazy_build 1 /; | 
| 20 |  |  |  |  |  |  | sub _build_error { | 
| 21 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 22 | 0 |  |  |  |  |  | $self->parse; | 
| 23 | 0 |  |  |  |  |  | return $self->error; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | has responseData => qw/ is ro lazy_build 1 /; | 
| 27 |  |  |  |  |  |  | sub _build_responseData { | 
| 28 | 0 |  | 0 | 0 |  |  | return shift->content->{responseData} || {}; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | has results => qw/ is ro lazy_build 1 /; | 
| 32 |  |  |  |  |  |  | sub _build_results { | 
| 33 | 0 |  |  | 0 |  |  | return shift->responseData->{results}; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | has parsed => qw/ is rw /, default => 0; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub success { | 
| 39 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 40 | 0 | 0 |  |  |  |  | return $self->error ? 0 : 1; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  | 0 | 0 |  | sub is_success { return shift->success( @_ ) }; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub parse { | 
| 46 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 | 0 |  |  |  |  | return if $self->parsed; | 
| 49 | 0 |  |  |  |  |  | $self->parsed( 1 ); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 |  |  |  |  |  | my $response = $self->http_response; | 
| 52 | 0 |  |  | 0 |  |  | my $fail = sub { $self->_parse_error( @_ ) }; | 
|  | 0 |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 |  |  |  |  |  | my ( @error, $content ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 | 0 |  |  |  |  | return $fail->( $response->code, $response->message ) unless $response->is_success; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 |  |  |  |  |  | $content = $response->content; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | try { | 
| 61 | 0 |  |  | 0 |  |  | $content = $json->decode( $content ); | 
| 62 |  |  |  |  |  |  | } catch { | 
| 63 | 0 |  |  | 0 |  |  | @error = ( -1, "Unable to JSON parse content: $@" ); | 
| 64 | 0 |  |  |  |  |  | }; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 | 0 |  |  |  |  | return $fail->( @error ) if @error; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 | 0 |  |  |  |  | return $fail->( -1, "Unable to JSON parse content" ) unless $content ; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 | 0 |  |  |  |  | unless ( $content->{responseStatus} eq 200 ) { | 
| 71 | 0 |  |  |  |  |  | return $fail->( @$content{qw/ responseStatus responseDetails /} ); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 | 0 |  |  |  |  | return $fail->( -1, "responseData is missing" ) unless $content->{responseData}; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 | 0 |  |  |  |  | return $fail->( -1, "responseData.results is missing" ) unless | 
| 77 |  |  |  |  |  |  | $content->{responseData}->{results}; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 0 |  |  |  |  |  | $self->content( $content ); | 
| 80 | 0 |  |  |  |  |  | $self->error( undef ); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub _parse_error { | 
| 84 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 85 | 0 |  |  |  |  |  | my ( $code, $message ) = ( shift, shift ); | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 0 |  |  |  |  |  | $self->content( { responseData => { results => undef } } ); | 
| 88 | 0 |  |  |  |  |  | $self->error( Google::Search::Error->new( | 
| 89 |  |  |  |  |  |  | http_response => $self->http_response, code => $code, message => $message, @_ ) ); | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | 1; |