/usr/share/perl5/WebService/Solr/Response.pm is in libwebservice-solr-perl 0.42-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | package WebService::Solr::Response;
use Moo;
use Types::Standard qw(Object HashRef Maybe InstanceOf ArrayRef);
use WebService::Solr::Document;
use Data::Page;
use Data::Pageset;
use JSON::XS ();
has 'raw_response' => (
is => 'ro',
isa => Object,
handles => {
status_code => 'code',
status_message => 'message',
is_success => 'is_success',
is_error => 'is_error'
},
);
has 'content' => ( is => 'lazy', isa => HashRef );
has 'docs' =>
( is => 'lazy', isa => ArrayRef );
around docs => sub {
my ($orig, $self, @args) = @_;
my $ret = $self->$orig(@args);
return wantarray ? @$ret : $ret;
};
has 'pager' => ( is => 'lazy', isa => Maybe[InstanceOf['Data::Page']] );
has '_pageset_slide' =>
( is => 'rw', isa => Maybe[InstanceOf['Data::Pageset']], predicate => 1 );
has '_pageset_fixed' =>
( is => 'rw', isa => Maybe[InstanceOf['Data::Pageset']], predicate => 1 );
sub BUILDARGS {
my ( $self, $res ) = @_;
return { raw_response => $res };
}
sub _build_content {
my $self = shift;
my $content = $self->raw_response->content;
return {} unless $content;
my $rv = eval { JSON::XS::decode_json( $content ) };
### JSON::XS throw an exception, but kills most of the content
### in the diagnostic, making it hard to track down the problem
die "Could not parse JSON response: $@ $content" if $@;
return $rv;
}
sub _build_docs {
my $self = shift;
my $struct = $self->content;
return unless exists $struct->{ response }->{ docs };
return [ map { WebService::Solr::Document->new( %$_ ) }
@{ $struct->{ response }->{ docs } } ];
}
sub _build_pager {
my $self = shift;
my $struct = $self->content;
return unless exists $struct->{ response }->{ numFound };
my $rows = $struct->{ responseHeader }->{ params }->{ rows };
$rows = 10 unless defined $rows;
# do not generate a pager for queries explicitly requesting no rows
return if $rows == 0;
my $pager = Data::Page->new;
$pager->total_entries( $struct->{ response }->{ numFound } );
$pager->entries_per_page( $rows );
$pager->current_page( $struct->{ response }->{ start } / $rows + 1 );
return $pager;
}
sub pageset {
my $self = shift;
my %args = @_;
my $mode = $args{ 'mode' } || 'fixed';
my $meth = "_pageset_" . $mode;
my $pred = "_has" . $meth;
### use a cached version if possible
return $self->$meth if $self->$pred;
my $pager = $self->_build_pageset( @_ );
### store the result
return $self->$meth( $pager );
}
sub _build_pageset {
my $self = shift;
my $struct = $self->content;
return unless exists $struct->{ response }->{ numFound };
my $rows = $struct->{ responseHeader }->{ params }->{ rows };
$rows = 10 unless defined $rows;
# do not generate a pager for queries explicitly requesting no rows
return if $rows == 0;
my $pager = Data::Pageset->new(
{ total_entries => $struct->{ response }->{ numFound },
entries_per_page => $rows,
current_page => $struct->{ response }->{ start } / $rows + 1,
pages_per_set => 10,
mode => 'fixed', # default, or 'slide'
@_,
}
);
return $pager;
}
sub facet_counts {
return shift->content->{ facet_counts };
}
sub spellcheck {
return shift->content->{ spellcheck };
}
sub solr_status {
return shift->content->{ responseHeader }->{ status };
}
sub ok {
my $status = shift->solr_status;
return defined $status && $status == 0;
}
no Moo;
1;
__END__
=head1 NAME
WebService::Solr::Response - Parse responses from Solr
=head1 SYNOPSIS
my $res = WebService::Solr::Response->new( $http_res );
for my $doc( $res->docs ) {
print $doc->value_for( 'id'), "\n";
}
my $pager = $res->pager;
=head1 DESCRIPTION
This class encapsulates responses from the Solr Web Service. Typically it is
used when documents are returned from a search query, though it will accept
all responses from the service.
=head1 ACCESSORS
=over 4
=item * raw_response - the raw L<HTTP::Response> object.
=item * content - a hashref of deserialized JSON data from the response.
=item * docs - an array of L<WebService::Solr::Document> objects.
=item * pager - a L<Data::Page> object for the search results.
=item * pageset - a L<Data::Pageset> object for the search results. Takes the same arguments as C<< Data::Pageset->new >> does. All arguments optional.
=back
=head1 METHODS
=head2 new( $response )
Given an L<HTTP::Response> object, it will parse the returned data as
required.
=head2 BUILDARGS( @args )
A Moo override to allow our custom constructor.
=head2 facet_counts( )
A shortcut to the C<facet_counts> key in the response data.
=head2 spellcheck( )
A shortcut to the C<spellcheck> key in the response data.
=head2 solr_status( )
Looks for the status value in the response data.
=head2 ok( )
Calls C<solr_status()> and check that it is equal to 0.
=head1 AUTHORS
Brian Cassidy E<lt>bricas@cpan.orgE<gt>
Kirk Beers
=head1 COPYRIGHT AND LICENSE
Copyright 2008-2016 National Adult Literacy Database
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|