File Coverage

blib/lib/Dezi/Response.pm
Criterion Covered Total %
statement 18 36 50.0
branch 0 8 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 25 52 48.0


line stmt bran cond sub pod time code
1             package Dezi::Response;
2 2     2   2230 use Moo;
  2         38879  
  2         13  
3 2     2   5049 use Types::Standard qw( Str Num Int ArrayRef HashRef InstanceOf Maybe );
  2         143166  
  2         22  
4 2     2   2761 use Carp;
  2         5  
  2         129  
5 2     2   11 use JSON;
  2         4  
  2         26  
6 2     2   1334 use Dezi::Doc;
  2         8  
  2         77  
7 2     2   19 use namespace::autoclean;
  2         4  
  2         23  
8              
9             our $VERSION = '0.003004';
10              
11             has 'http_response' => ( is => 'ro', isa => InstanceOf ['HTTP::Response'] );
12             has 'results' => ( is => 'rw', isa => Maybe [ArrayRef] );
13             has 'total' => ( is => 'rw', isa => Int );
14             has 'search_time' => ( is => 'rw', isa => Num );
15             has 'build_time' => ( is => 'rw', isa => Num );
16             has 'query' => ( is => 'rw', isa => Str );
17             has 'fields' => ( is => 'rw', isa => Maybe [ArrayRef] );
18             has 'facets' => ( is => 'rw', isa => Maybe [HashRef] );
19             has 'suggestions' => ( is => 'rw', isa => Maybe [ArrayRef] );
20              
21             =pod
22              
23             =head1 NAME
24              
25             Dezi::Response - Dezi search server response
26              
27             =head1 SYNOPSIS
28              
29             use Dezi::Client;
30             my $client = Dezi::Client->new('http://localhost:5000');
31            
32             my $response = $client->search( q => 'foo' );
33             # $response isa Dezi::Response
34            
35             # iterate over results
36             for my $result (@{ $response->results }) {
37             printf("--\n uri: %s\n title: %s\n score: %s\n",
38             $result->uri, $result->title, $result->score);
39             }
40            
41             # print stats
42             printf(" hits: %d\n", $response->total);
43             printf("search time: %s\n", $response->search_time);
44             printf(" build time: %s\n", $response->build_time);
45             printf(" query: %s\n", $response->query);
46              
47             =head1 DESCRIPTION
48              
49             Dezi::Response represents a Dezi server response.
50              
51             This class is used internally by Dezi::Client.
52              
53             =head1 METHODS
54              
55             =head2 new( I )
56              
57             Returns a new response. I should be a HTTP::Response
58             object from a Dezi JSON response.
59              
60             =head2 BUILDARGS
61              
62             Allows for single argument I instead of named pair.
63              
64             =head2 BUILD
65              
66             Initializes objects.
67              
68             =cut
69              
70             around BUILDARGS => sub {
71             my $orig = shift;
72             my $class = shift;
73              
74             if ( @_ == 1 ) {
75             return $class->$orig( http_response => $_[0] );
76             }
77             else {
78             return $class->$orig(@_);
79             }
80             };
81              
82             sub BUILD {
83 0     0 1   my $self = shift;
84 0           my $json = from_json( $self->http_response->decoded_content );
85              
86             # inflate json into self, except results
87 0           for my $attr ( keys %$json ) {
88 0 0         next if $attr eq 'results';
89              
90             # set in hash directly if we have no method (yet)
91 0 0         if ( $self->can($attr) ) {
92 0           $self->$attr( $json->{$attr} );
93             }
94             else {
95 0           $self->{$attr} = $json->{$attr};
96             }
97             }
98 0           my @res;
99             my @fields;
100 0           for my $r ( @{ $json->{results} } ) {
  0            
101 0 0         if ( !@fields ) {
102 0           for my $k ( keys %$r ) {
103 0 0         if ( !Dezi::Doc->can($k) ) {
104 0           push @fields, $k;
105             }
106             }
107             }
108 0           my %fields = map { $_ => $r->{$_} } @fields;
  0            
109 0           push @res, Dezi::Doc->new( %$r, _fields => \%fields );
110             }
111              
112             # overwrite with objects
113 0           $self->{results} = \@res;
114             }
115              
116             =head2 results
117              
118             Returns array ref of Dezi::Doc objects.
119              
120             =head2 total
121              
122             Returns integer of hit count.
123              
124             =head2 search_time
125              
126             Returns string of floating point time Dezi server took to search.
127              
128             =head2 build_time
129              
130             Returns string of floating point time Dezi server took to build
131             response.
132              
133             =head2 query
134              
135             Returns the query string.
136              
137             =head2 fields
138              
139             Returns array ref of field names.
140              
141             =head2 facets
142              
143             Returns array ref of facet objects.
144              
145             B
146              
147             =head2 suggestions
148              
149             Returns array ref of query suggestions.
150              
151             =head2 http_response
152              
153             Returns raw HTTP:Response object.
154              
155             =cut
156              
157             1;
158              
159             __END__