Solr.pm

filter: Statementstmt 96.0% 123 / 128 Branchbran 54.5% 12 / 22 Conditioncond 64.2% 9 / 14 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 96.9% 32 / 33 Podpod 100% 13 / 13 totaltotal 88.0% 191 / 217 CC 20 SCAR 10.0
CC
20
Cov
88%
BUILDARGS
7.1
add
5.3
_send_update
5.1
CRAP 2.7
« Solr/Document.pm ↑ Summary Solr/Query.pm »
1 package WebService::Solr;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 ▶use Types::Standard qw(InstanceOf Object Bool HashRef Maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use WebService::Solr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use XML::Easy::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use XML::Easy::Content;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use XML::Easy::Text ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 has 'url' => (
19 is => 'ro',
20 isa => InstanceOf['URI'],
21 7 ▶ default => sub { URI->new( 'http://localhost:8983/solr' ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
22 10 );
23  
24 has 'agent' =>
25 7 ▶ ( is => 'ro', isa => Object, default => sub { LWP::UserAgent->new } );
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 10 has 'autocommit' => ( is => 'ro', isa => Bool, default => 1 );
28  
29 has 'default_params' => (
30 is => 'ro',
31 isa => HashRef,
32 7 ▶ default => sub { { wt => 'json' } }
Subroutine __ANON__: called CC=1 SCAR=0.0
33 10 );
34  
35 18 ▶around default_params => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
36 18 my ($orig, $self, @args) = @_;
37 18 my $ret = $self->$orig(@args);
38 18 ▶ return wantarray ? %$ret : $ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 0 18
39 10 };
40  
41 10 has 'last_response' => (
42 is => 'rw',
43 isa => Maybe[InstanceOf['WebService::Solr::Response']],
44 );
45  
46 10 our $VERSION = '0.44';
47  
48 7 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=5 SCAR=19.6
Pod documented
49 7 my ( $self, $url, $options ) = @_;
50 7 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
25
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
51  
52 7 ▶ if ( $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 0 7 7
53 0 ▶ $options->{ url } = ref $url ? $url : URI->new( $url );
Branch: 1 branch
BranchTrueFalseTotal
ref $url ? : 0 0 0
54 }
55  
56 7 ▶ if ( exists $options->{ default_params } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"default_params"}) 0 7 7
57 $options->{ default_params }
58 0 = { %{ $options->{ default_params } }, wt => 'json', };
59 }
60  
61 7 return $options;
62 }
63  
64 2 ▶sub add {
Subroutine add: called CC=5 SCAR=16.7
Pod documented
65 2 my ( $self, $doc, $params ) = @_;
66 2 ▶ my @docs = ref $doc eq 'ARRAY' ? @$doc : ( $doc );
Branch: 1 branch
BranchTrueFalseTotal
ref $doc eq 'ARRAY' ? : 0 2 2
67  
68 my @elements = map {
69 2 ▶ ( '',
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($_) ? : 0 2 2
ref $_ eq 'HASH' ? : 2 0 2
70 blessed $_
71 ? $_->to_element
72 : WebService::Solr::Document->new(
73 ref $_ eq 'HASH' ? %$_ : @$_
74 )->to_element
75 )
76 } @docs;
77  
78 2 ▶ $params ||= {};
Condition50%1/2
$params ||= {}
l!l
02
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
79 2 my $e
80 = XML::Easy::Element->new( 'add', $params,
81 XML::Easy::Content->new( [ @elements, '' ] ),
82 );
83 2 my $xml = XML::Easy::Text::xml10_write_element( $e );
84  
85 2 my $response = $self->_send_update( $xml );
86 2 return $response->ok;
87 }
88  
89 1 ▶sub update {
Subroutine update: called CC=1 SCAR=0.0
Pod documented
90 1 return shift->add( @_ );
91 }
92  
93 5 ▶sub commit {
Subroutine commit: called CC=2 SCAR=7.0
Pod documented
94 5 my ( $self, $params ) = @_;
95 5 ▶ $params ||= {};
Condition50%1/2
$params ||= {}
l!l
50
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
96 5 my $e = XML::Easy::Element->new( 'commit', $params, [ '' ] );
97 5 my $xml = XML::Easy::Text::xml10_write_element( $e );
98 5 my $response = $self->_send_update( $xml, {}, 0 );
99 5 return $response->ok;
100 }
101  
102 1 ▶sub rollback {
Subroutine rollback: called CC=1 SCAR=0.0
Pod documented
103 1 my ( $self ) = @_;
104 1 my $response = $self->_send_update( '<rollback/>', {}, 0 );
105 1 return $response->ok;
106 }
107  
108 5 ▶sub optimize {
Subroutine optimize: called CC=2 SCAR=7.0
Pod documented
109 5 my ( $self, $params ) = @_;
110 5 ▶ $params ||= {};
Condition50%1/2
$params ||= {}
l!l
50
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
111 5 my $e = XML::Easy::Element->new( 'optimize', $params, [ '' ] );
112 5 my $xml = XML::Easy::Text::xml10_write_element( $e );
113 5 my $response = $self->_send_update( $xml, {}, 0 );
114 5 return $response->ok;
115 }
116  
117 4 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.0
Pod documented
118 4 my ( $self, $options ) = @_;
119  
120 4 my $xml = '';
121 4 for my $k ( keys %$options ) {
122 6 my $v = $options->{ $k };
123 $xml .= join(
124 '',
125 map {
126 6 ▶ XML::Easy::Text::xml10_write_element(
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 2 4 6
127 XML::Easy::Element->new( $k, {}, [ $_ ] ) )
128 } ref $v ? @$v : $v
129 );
130 }
131  
132 4 my $response = $self->_send_update( "<delete>${xml}</delete>" );
133 4 return $response->ok;
134 }
135  
136 1 ▶sub delete_by_id {
Subroutine delete_by_id: called CC=1 SCAR=0.0
Pod documented
137 1 my ( $self, $id ) = @_;
138 1 return $self->delete( { id => $id } );
139 }
140  
141 1 ▶sub delete_by_query {
Subroutine delete_by_query: called CC=1 SCAR=0.0
Pod documented
142 1 my ( $self, $query ) = @_;
143 1 return $self->delete( { query => $query } );
144 }
145  
146 1 ▶sub ping {
Subroutine ping: called CC=1 SCAR=0.0
Pod documented
147 1 my ( $self ) = @_;
148 1 $self->last_response( WebService::Solr::Response->new(
149 $self->agent->get( $self->_gen_url( 'admin/ping' ) ) ) );
150 1 return $self->last_response->is_success;
151 }
152  
153 1 ▶sub search {
Subroutine search: called CC=2 SCAR=7.0
Pod documented
154 1 my ( $self, $query, $params ) = @_;
155 1 ▶ $params ||= {};
Condition50%1/2
$params ||= {}
l!l
01
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
156 1 $params->{ 'q' } = $query;
157 1 return $self->generic_solr_request( 'select', $params );
158 }
159  
160 0 ▶sub auto_suggest {
Subroutine auto_suggest: not called CC=1 SCAR=6.9
Pod documented
161 0 shift->generic_solr_request( 'autoSuggest', @_ );
162 }
163  
164 1 ▶sub generic_solr_request {
Subroutine generic_solr_request: called CC=2 SCAR=7.1
Pod documented
165 1 my ( $self, $path, $params ) = @_;
166 1 ▶ $params ||= {};
Condition50%1/2
$params ||= {}
l!l
10
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
167 1 return $self->last_response(
168 WebService::Solr::Response->new(
169 $self->agent->post(
170 $self->_gen_url( $path ),
171 Content_Type => 'application/x-www-form-urlencoded; charset=utf-8',
172 Content => { $self->default_params, %$params } ) ) );
173 }
174  
175 19 ▶sub _gen_url {
Subroutine _gen_url: called CC=1 SCAR=0.0
176 19 my ( $self, $handler ) = @_;
177  
178 19 my $url = $self->url->clone;
179 19 $url->path( $url->path . "/$handler" );
180 19 return $url;
181 }
182  
183 17 ▶sub _send_update {
Subroutine _send_update: called CC=5 SCAR=16.3
184 17 my ( $self, $xml, $params, $autocommit ) = @_;
185 17 ▶ $autocommit = $self->autocommit unless defined $autocommit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $autocommit 6 11 17
186  
187 17 ▶ $params ||= {};
Condition100%2/2
$params ||= {}
l!l
116
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
188 17 my $url = $self->_gen_url( 'update' );
189 17 $url->query_form( { $self->default_params, %$params } );
190 17 my $req = HTTP::Request->new(
191 POST => $url,
192 HTTP::Headers->new( Content_Type => 'text/xml; charset=utf-8' ),
193 '<?xml version="1.0" encoding="UTF-8"?>' . encode( 'utf8', "$xml" )
194 );
195  
196 17 my $http_response = $self->agent->request( $req );
197 17 ▶ if ( $http_response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_response->is_error) 0 17 17
198 0 confess($http_response->status_line . ': ' . $http_response->content);
199 }
200  
201 17 $self->last_response( WebService::Solr::Response->new( $http_response ) );
202  
203 17 ▶ $self->commit if $autocommit;
Branch: 1 branch
BranchTrueFalseTotal
if $autocommit 0 17 17
204  
205 17 return $self->last_response;
206 }
207  
208 10 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
209  
210 10 1;
211  
212 __END__
« Solr/Document.pm ↑ Summary Solr/Query.pm »