File Coverage

blib/lib/WebService/Solr/Tiny.pm
Criterion Covered Total %
statement 34 37 91.8
branch 8 10 80.0
condition 2 4 50.0
subroutine 8 9 88.8
pod 1 2 50.0
total 53 62 85.4


line stmt bran cond sub pod time code
1             package WebService::Solr::Tiny 0.001;
2              
3 3     3   84783 use strict;
  3         6  
  3         75  
4 3     3   15 use warnings;
  3         5  
  3         86  
5              
6 3     3   2018 use URI::Query::FromHash 0.003;
  3         2951  
  3         15  
7              
8             sub import {
9 3     3   97 no strict 'refs';
  3         4  
  3         1157  
10              
11 1         48 *{ caller . '::solr_escape' }
12 0     0   0 = sub { $_[0] =~ s/([\Q+-&|!(){}[]^"~*?:\\\E])/\\$1/gr }
13 5 100   5   5014 if grep $_ eq 'solr_escape', @_;
14             }
15              
16             sub new {
17 2     2 0 207 my $class = shift;
18 2         8 my $self = bless {@_}, $class;
19              
20 2 50       15 unless ( exists $self->{agent} ) {
21 0         0 require HTTP::Tiny;
22              
23 0         0 $self->{agent} = HTTP::Tiny->new( keep_alive => 1 );
24             }
25              
26 2 50       12 unless ( exists $self->{decoder} ) {
27 2         15 require JSON::PP;
28              
29 2         7 $self->{decoder} = \&JSON::PP::decode_json;
30             }
31              
32 2   50     14 $self->{default_args} //= {};
33 2   50     11 $self->{url} //= 'http://localhost:8983/solr/select';
34              
35 2         4 $self;
36             }
37              
38             sub search {
39 4     4 1 1051 my $self = shift;
40 4 100       7 my %args = ( %{ $self->{default_args} }, 'q' => @_ ? @_ : '' );
  4         27  
41              
42 4         21 my $reply = $self->{agent}->get( $self->{url} . '?' . hash2query %args );
43              
44 4 100       489 unless ( $reply->{success} ) {
45 1         5 require Carp;
46              
47 1         170 Carp::croak("Solr request failed - $reply->{content}");
48             }
49              
50 3         11 $self->{decoder}( $reply->{content} );
51             }
52              
53 3     3   15 no URI::Query::FromHash;
  3         4  
  3         10  
54              
55             1;