| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Solr::Response; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 34696 | use Moo; | 
|  | 3 |  |  |  |  | 9099 |  | 
|  | 3 |  |  |  |  | 18 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 3 |  |  | 3 |  | 2120 | use Types::Standard qw(Object HashRef Maybe InstanceOf ArrayRef); | 
|  | 3 |  |  |  |  | 78038 |  | 
|  | 3 |  |  |  |  | 53 |  | 
| 6 | 3 |  |  | 3 |  | 4250 | use WebService::Solr::Document; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 115 |  | 
| 7 | 3 |  |  | 3 |  | 1043 | use Data::Page; | 
|  | 3 |  |  |  |  | 12272 |  | 
|  | 3 |  |  |  |  | 19 |  | 
| 8 | 3 |  |  | 3 |  | 1059 | use Data::Pageset; | 
|  | 3 |  |  |  |  | 2317 |  | 
|  | 3 |  |  |  |  | 30 |  | 
| 9 | 3 |  |  | 3 |  | 1595 | use JSON::XS (); | 
|  | 3 |  |  |  |  | 11020 |  | 
|  | 3 |  |  |  |  | 1646 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | has 'raw_response' => ( | 
| 12 |  |  |  |  |  |  | is      => 'ro', | 
| 13 |  |  |  |  |  |  | isa     => Object, | 
| 14 |  |  |  |  |  |  | handles => { | 
| 15 |  |  |  |  |  |  | status_code    => 'code', | 
| 16 |  |  |  |  |  |  | status_message => 'message', | 
| 17 |  |  |  |  |  |  | is_success     => 'is_success', | 
| 18 |  |  |  |  |  |  | is_error       => 'is_error' | 
| 19 |  |  |  |  |  |  | }, | 
| 20 |  |  |  |  |  |  | ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | has 'content' => ( is => 'lazy', isa => HashRef ); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | has 'docs' => | 
| 25 |  |  |  |  |  |  | ( is => 'lazy', isa => ArrayRef ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | around docs => sub { | 
| 28 |  |  |  |  |  |  | my ($orig, $self, @args) = @_; | 
| 29 |  |  |  |  |  |  | my $ret = $self->$orig(@args); | 
| 30 |  |  |  |  |  |  | return wantarray ? @$ret : $ret; | 
| 31 |  |  |  |  |  |  | }; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | has 'pager' => ( is => 'lazy', isa => Maybe[InstanceOf['Data::Page']] ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | has '_pageset_slide' => | 
| 36 |  |  |  |  |  |  | ( is => 'rw', isa => Maybe[InstanceOf['Data::Pageset']], predicate => 1 ); | 
| 37 |  |  |  |  |  |  | has '_pageset_fixed' => | 
| 38 |  |  |  |  |  |  | ( is => 'rw', isa => Maybe[InstanceOf['Data::Pageset']], predicate => 1 ); | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub BUILDARGS { | 
| 41 | 2 |  |  | 2 | 1 | 14331 | my ( $self, $res ) = @_; | 
| 42 | 2 |  |  |  |  | 30 | return { raw_response => $res }; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub _build_content { | 
| 46 | 2 |  |  | 2 |  | 6257 | my $self    = shift; | 
| 47 | 2 |  |  |  |  | 21 | my $content = $self->raw_response->content; | 
| 48 | 2 | 50 |  |  |  | 32 | return {} unless $content; | 
| 49 | 2 |  |  |  |  | 4 | my $rv = eval { JSON::XS::decode_json( $content ) }; | 
|  | 2 |  |  |  |  | 35 |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | ### JSON::XS throw an exception, but kills most of the content | 
| 52 |  |  |  |  |  |  | ### in the diagnostic, making it hard to track down the problem | 
| 53 | 2 | 50 |  |  |  | 7 | die "Could not parse JSON response: $@ $content" if $@; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 2 |  |  |  |  | 29 | return $rv; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub _build_docs { | 
| 59 | 1 |  |  | 1 |  | 12 | my $self   = shift; | 
| 60 | 1 |  |  |  |  | 14 | my $struct = $self->content; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 1 | 50 |  |  |  | 12 | return unless exists $struct->{ response }->{ docs }; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 2 |  |  |  |  | 12 | return [ map { WebService::Solr::Document->new( %$_ ) } | 
| 65 | 1 |  |  |  |  | 3 | @{ $struct->{ response }->{ docs } } ]; | 
|  | 1 |  |  |  |  | 3 |  | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub _build_pager { | 
| 69 | 2 |  |  | 2 |  | 823 | my $self   = shift; | 
| 70 | 2 |  |  |  |  | 28 | my $struct = $self->content; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 2 | 50 |  |  |  | 29 | return unless exists $struct->{ response }->{ numFound }; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 2 |  |  |  |  | 6 | my $rows = $struct->{ responseHeader }->{ params }->{ rows }; | 
| 75 | 2 | 50 |  |  |  | 5 | $rows = 10 unless defined $rows; | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | # do not generate a pager for queries explicitly requesting no rows | 
| 78 | 2 | 100 |  |  |  | 20 | return if $rows == 0; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 1 |  |  |  |  | 8 | my $pager = Data::Page->new; | 
| 81 | 1 |  |  |  |  | 73 | $pager->total_entries( $struct->{ response }->{ numFound } ); | 
| 82 | 1 |  |  |  |  | 13 | $pager->entries_per_page( $rows ); | 
| 83 | 1 |  |  |  |  | 16 | $pager->current_page( $struct->{ response }->{ start } / $rows + 1 ); | 
| 84 | 1 |  |  |  |  | 32 | return $pager; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub pageset { | 
| 88 | 5 |  |  | 5 | 1 | 2988 | my $self = shift; | 
| 89 | 5 |  |  |  |  | 12 | my %args = @_; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 5 |  | 100 |  |  | 23 | my $mode = $args{ 'mode' } || 'fixed'; | 
| 92 | 5 |  |  |  |  | 11 | my $meth = "_pageset_" . $mode; | 
| 93 | 5 |  |  |  |  | 11 | my $pred = "_has" . $meth; | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | ### use a cached version if possible | 
| 96 | 5 | 100 |  |  |  | 56 | return $self->$meth if $self->$pred; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 2 |  |  |  |  | 6 | my $pager = $self->_build_pageset( @_ ); | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | ### store the result | 
| 101 | 2 |  |  |  |  | 39 | return $self->$meth( $pager ); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub _build_pageset { | 
| 105 | 2 |  |  | 2 |  | 4 | my $self   = shift; | 
| 106 | 2 |  |  |  |  | 40 | my $struct = $self->content; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 2 | 50 |  |  |  | 19 | return unless exists $struct->{ response }->{ numFound }; | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 2 |  |  |  |  | 4 | my $rows = $struct->{ responseHeader }->{ params }->{ rows }; | 
| 111 | 2 | 50 |  |  |  | 6 | $rows = 10 unless defined $rows; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | # do not generate a pager for queries explicitly requesting no rows | 
| 114 | 2 | 100 |  |  |  | 7 | return if $rows == 0; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | my $pager = Data::Pageset->new( | 
| 117 |  |  |  |  |  |  | {   total_entries    => $struct->{ response }->{ numFound }, | 
| 118 |  |  |  |  |  |  | entries_per_page => $rows, | 
| 119 | 1 |  |  |  |  | 19 | current_page     => $struct->{ response }->{ start } / $rows + 1, | 
| 120 |  |  |  |  |  |  | pages_per_set    => 10, | 
| 121 |  |  |  |  |  |  | mode => 'fixed',    # default, or 'slide' | 
| 122 |  |  |  |  |  |  | @_, | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  | ); | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 1 |  |  |  |  | 297 | return $pager; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | sub facet_counts { | 
| 130 | 0 |  |  | 0 | 1 |  | return shift->content->{ facet_counts }; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | sub spellcheck { | 
| 134 | 0 |  |  | 0 | 1 |  | return shift->content->{ spellcheck }; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | sub solr_status { | 
| 138 | 0 |  |  | 0 | 1 |  | return shift->content->{ responseHeader }->{ status }; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | sub ok { | 
| 142 | 0 |  |  | 0 | 1 |  | my $status = shift->solr_status; | 
| 143 | 0 |  | 0 |  |  |  | return defined $status && $status == 0; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 3 |  |  | 3 |  | 20 | no Moo; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 23 |  | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | 1; | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | __END__ |