Dezi-Client
view release on metacpan or search on metacpan
lib/Dezi/Response.pm view on Meta::CPAN
Dezi::Response - Dezi search server response
=head1 SYNOPSIS
use Dezi::Client;
my $client = Dezi::Client->new('http://localhost:5000');
my $response = $client->search( q => 'foo' );
# $response isa Dezi::Response
# iterate over results
for my $result (@{ $response->results }) {
printf("--\n uri: %s\n title: %s\n score: %s\n",
$result->uri, $result->title, $result->score);
}
# print stats
printf(" hits: %d\n", $response->total);
printf("search time: %s\n", $response->search_time);
printf(" build time: %s\n", $response->build_time);
printf(" query: %s\n", $response->query);
=head1 DESCRIPTION
Dezi::Response represents a Dezi server response.
This class is used internally by Dezi::Client.
=head1 METHODS
=head2 new( I<http_response> )
Returns a new response. I<http_response> should be a HTTP::Response
object from a Dezi JSON response.
=head2 BUILDARGS
Allows for single argument I<http_response> instead of named pair.
=head2 BUILD
Initializes objects.
=cut
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
if ( @_ == 1 ) {
return $class->$orig( http_response => $_[0] );
}
else {
return $class->$orig(@_);
}
};
sub BUILD {
my $self = shift;
my $json = from_json( $self->http_response->decoded_content );
# inflate json into self, except results
for my $attr ( keys %$json ) {
next if $attr eq 'results';
# set in hash directly if we have no method (yet)
if ( $self->can($attr) ) {
$self->$attr( $json->{$attr} );
}
else {
$self->{$attr} = $json->{$attr};
}
}
my @res;
my @fields;
for my $r ( @{ $json->{results} } ) {
if ( !@fields ) {
for my $k ( keys %$r ) {
if ( !Dezi::Doc->can($k) ) {
push @fields, $k;
}
}
}
my %fields = map { $_ => $r->{$_} } @fields;
push @res, Dezi::Doc->new( %$r, _fields => \%fields );
}
# overwrite with objects
$self->{results} = \@res;
}
=head2 results
Returns array ref of Dezi::Doc objects.
=head2 total
Returns integer of hit count.
=head2 search_time
Returns string of floating point time Dezi server took to search.
=head2 build_time
Returns string of floating point time Dezi server took to build
response.
=head2 query
Returns the query string.
=head2 fields
Returns array ref of field names.
=head2 facets
Returns array ref of facet objects.
( run in 1.619 second using v1.01-cache-2.11-cpan-39bf76dae61 )