blib/lib/DBIx/Class/Indexer/WebService/Dezi.pm

filter: Statementstmt 87.5% 98 / 112 Branchbran 59.3% 19 / 32 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.6% 22 / 24 Podpod 100% 8 / 8 totaltotal 80.1% 149 / 186 CC 23 SCAR 17.0
CC
23
Cov
79%
value_for_field
15.8
update
6.0
as_document
5.0
CRAP 5.5
↑ Summary
1 package DBIx::Class::Indexer::WebService::Dezi;
2  
3 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Dezi::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Media::Type::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 DBIx::Class::Indexer::WebService::Dezi - An indexer for Dezi/Lucy.
16  
17 =head1 VERSION
18  
19 Version 0.08
20  
21 =cut
22  
23 4 our $VERSION = '0.08';
24  
25 2 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 my ( $orig, $class, $connect_info, $source ) = @_;
27 2 return $class->$orig( connect_info => $connect_info, source => $source );
28 4 };
29  
30 =head1 SYNOPSIS
31  
32 This module was inspired (and borrowed some) by DBIx::Class::Indexer::WebService::Solr.
33 In fact it uses DBIx::Class::Indexer as its abstract class. This indexer allows one to
34 use a Dezi::Client to update the index on "insert", "update", or "delete".
35  
36 package MyApp::Schema::Foo;
37 use base 'DBIx::Class';
38  
39 __PACKAGE__->load_components( qw[ Indexed ] );
40 __PACKAGE__->set_indexer( 'WebService::Dezi', { server => 'http://localhost:5000', content_type => 'application/json' } );
41
42 __PACKAGE__->table('person');
43
44 __PACKAGE__->add_columns(
45 person_id => {
46 data_type => 'varchar',
47 size => '36',
48 is_nullable => 0,
49 },
50 name => {
51 data_type => 'varchar',
52 is_nullable => 0,
53 indexed => 1
54 },
55 age => {
56 data_type => 'integer',
57 is_nullable => 0,
58 },
59 image_path => {
60 data_type => 'varchar',
61 size => '128',
62 indexed => { is_binary => 1, base64_encode => 1 },
63 },
64 email => {
65 data_type => 'varchar',
66 size => '128',
67 },
68 created => {
69 data_type => 'timestamp',
70 set_on_create => 1,
71 is_nullable => 0,
72 },
73 );
74  
75 =head1 CONFIG
76  
77 =head2 indexed
78  
79 Can be set to 1 or contain a hashref.
80  
81 =head2 is_binary
82  
83 Flags an indexied field as a binary pointer. Will attempt
84 to slurp the contents for indexing.
85  
86 =head2 base64_encode
87  
88 A flag that will make a is_binary indexed field converted
89 to base64. It is worth noting that highlighting needs to be
90 turned off in the dezi config for this to properly index.
91  
92 =head1 ATTRIBUTES
93  
94 =head2 connect_info
95  
96 Connect info parameters.
97  
98 =cut
99 4 has connect_info => (
100 is => 'rw',
101 required => 1,
102 );
103  
104 =head2 content_type
105  
106 Connect info parameters.
107  
108 =cut
109 has content_type => (
110 is => 'rw',
111 lazy => 1,
112 1 ▶ default => sub { (shift)->connect_info->{content_type} || 'application/xml' }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->connect_info->{"content_type"} or "application/xml" 1 0 1
113 4 );
114  
115 =head2 disabled
116  
117 Will disable any calls to Dezi::Client and indexing. This is useful in preventing
118 exceptions if the Dezi server is temporarily down.
119  
120 =cut
121  
122 has disabled => (
123 is => 'rw',
124 lazy => 1,
125 2 ▶ default => sub { (shift)->connect_info->{disabled} || 0 }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->connect_info->{"disabled"} or 0 1 1 2
126 4 );
127  
128 =head2 source
129  
130 Source object
131  
132 =cut
133 4 has source => (
134 is => 'rw'
135 );
136  
137 =head2 _dezi
138  
139 Internal dezi object.
140  
141 =cut
142 has _dezi => (
143 is => 'rw',
144 lazy => 1,
145 1 ▶ default => sub { return Dezi::Client->new( server => (shift)->connect_info->{server} ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
146 4 );
147  
148 =head2 _field_prep
149  
150 Used for noramalization of fields.
151  
152 =cut
153 has _field_prep => (
154 traits => ['Hash'],
155 is => 'rw',
156 isa => 'HashRef[Str]',
157 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
158 4 handles => {
159 set_field_prep => 'set',
160 get_field_prep => 'get',
161 }
162 );
163  
164 =head1 ATTRIBUTES
165  
166 =head2 as_document( $self, $object )
167  
168 Handles the insert operation. Generates a XML or JSON document
169 that will be indexed by the dezi service.
170  
171 =cut
172  
173 1 ▶sub as_document {
Subroutine as_document: called CC=5 SCAR=16.1
Pod documented
174 1 my ( $self, $object ) = @_;
175  
176 1 my $fields = $object->index_fields;
177  
178 1 my %output;
179 # for each field in schema...
180 1 for my $name ( keys %$fields ) {
181 2 my $opts = $fields->{$name};
182 2 my @values = $self->value_for_field( $object, $name );
183  
184 2 ▶ if ( defined $opts->{is_binary} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"is_binary"}) 1 1 2
185 1 my $file_path = $values[0];
186  
187 1 my $content_type = $self->_determine_content_type($file_path);
188 1 my $binary_data = $self->_read_binary($file_path);
189  
190 1 @values = ($content_type, $binary_data);
191  
192 1 ▶ if ( defined $opts->{base64_encode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'base64_encode'}) { } 1 0 1
193 1 @values = ($content_type, encode_base64($binary_data));
194 } else {
195 0 @values = ($content_type, $binary_data);
196 }
197  
198 }
199
200 2 for( @values ) {
201 3 $output{$name} = [ @values ]
202 }
203 }
204  
205 1 my $output_str = $self->_generate_document(\%output);
206  
207 1 return \$output_str;
208 }
209  
210 =head2 BUILD( $self )
211  
212 Creates a new Dezi::Client object and normalizes the fields to be
213 indexed.
214  
215 =cut
216 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
217 2 my ( $self ) = @_;
218 2 $self->setup_fields( $self->source );
219 2 return $self;
220 }
221  
222  
223 =head2 update_or_create_document( $object )
224  
225 Handles the insert operation.
226  
227 =cut
228  
229 1 ▶sub update_or_create_document {
Subroutine update_or_create_document: called CC=1 SCAR=0.0
Pod documented
230 1 my $self = shift;
231 1 my $object = shift;
232  
233 1 $self->setup_fields( ref $object );
234 1 $self->_dezi->index( $self->as_document( $object ), $object->id, $self->content_type );
235  
236 }
237  
238  
239 =head2 value_for_field( $object, $key )
240  
241 Uses the indexed fields information to determine how to get
242 the values for C<$key> out of C<$object>.
243  
244 The logic here was borrowed from DBIx::Class::Indexer::WebService::Solr
245  
246 =cut
247  
248 2 ▶sub value_for_field {
Subroutine value_for_field: called CC=10 SCAR=27.6
Pod documented
249 2 my( $self, $object, $key ) = @_;
250 2 my $info = $object->index_fields->{ $key };
251 2 ▶ my $source = $info->{ source } || $key;
Condition33%1/3
$info->{'source'} || $key
l!l&&r!l&&!r
020
MC/DC0%0/2
$info->{'source'} || $key
$info->{'source'} $key
Truth table33%1/3
A || B
A= $info->{'source'}B= $key
ABresult
1X1
011
000
252  
253 2 ▶ if( ref $source eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $source eq 'CODE') { } 0 2 2
elsif (not ref $source) { } 2 0 2
254 0 return $source->( $object );
255 }
256 elsif( not ref $source ) {
257 2 my @accessors = split /\./, $source;
258
259 # no use calling 'me' on myself...
260 2 ▶ shift @accessors if lc $accessors[ 0 ] eq 'me';
Branch: 1 branch
BranchTrueFalseTotal
if lc $accessors[0] eq "me" 0 2 2
261
262 # traverse accessors
263 2 my @values = $object;
264 2 for my $accessor ( @accessors ) {
265 2 @values = grep { defined }
266 map {
267 2 Scalar::Util::blessed( $_ ) and $_->can( $accessor ) ? $_->$accessor
268 2 ▶ : ref $_ eq 'HASH' ? $_->{ $accessor }
Branch: 2 branches
BranchTrueFalseTotal
$_->can($accessor) ? : 2 0 2
ref $_ eq 'HASH' ? : 0 0 0
Condition33%1/3
builtin::blessed($_) && ($_->can($accessor) ? $_->$accessor : (ref $_ eq 'HASH' ? $_->{$accessor} : undef))
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($_) && ($_->can($accessor) ? $_->$accessor : (ref $_ eq 'HASH' ? $_->{$accessor} : undef))
builtin::blessed($_) ($_->can($accessor) ? $_->$accessor : (ref $_ eq 'HASH' ? $_->{$accessor} : undef))
Truth table33%1/3
A && B
A= builtin::blessed($_)B= ($_->can($accessor) ? $_->$accessor : (ref $_ eq 'HASH' ? $_->{$accessor} : undef))
ABresult
0X0
100
111
269 : undef
270 } @values;
271 }
272 2 ▶ return wantarray ? @values : $values[ 0 ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
273 }
274 }
275  
276 =head2 setup_fields( $source )
277  
278 Normalizes the index fields so they all have hashref members with an optional
279 boost key.
280  
281 =cut
282 3 ▶sub setup_fields {
Subroutine setup_fields: called CC=4 SCAR=13.9
Pod documented
283 3 my( $self, $source ) = @_;
284  
285 3 ▶ return if $self->get_field_prep( $source );
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_field_prep($source) 1 2 3
286  
287 2 my $fields = $source->index_fields;
288
289 # normalize field defs
290 2 for my $key ( keys %$fields ) {
291 4 ▶ $fields->{ $key } = { } if !ref $fields->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields->{$key} 2 2 4
292 }
293  
294 2 $self->set_field_prep( $source => 1 );
295 }
296  
297 =head2 delete( $object )
298  
299 Handles the delete operation.
300  
301 =cut
302  
303 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
304 0 my $self = shift;
305 0 my $object = shift;
306  
307 0 $self->setup_fields( ref $object );
308  
309 0 my $id = $self->value_for_field( $object, 'id' );
310 0 $self->_dezi->delete( $id );
311 }
312  
313 =head2 insert( $object )
314  
315 Handles the insert operation.
316  
317 =cut
318  
319 2 ▶sub insert {
Subroutine insert: called CC=2 SCAR=6.9
Pod documented
320 2 my $self = shift;
321 2 my $object = shift;
322 2 ▶ return if $self->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disabled 1 1 2
323 1 $self->update_or_create_document( $object );
324 }
325  
326 =head2 update( $object )
327  
328 Handles the update operation.
329  
330 =cut
331  
332 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
333 0 my $self = shift;
334 0 my $object = shift;
335 0 ▶ return if $self->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disabled 0 0 0
336
337 0 $self->update_or_create_document( $object );
338 }
339  
340 1 ▶sub _determine_content_type {
Subroutine _determine_content_type: called CC=1 SCAR=0.0
341 1 my ( $self, $file_path ) = @_;
342 1 my ($ext) = $file_path =~ /\.([^\.]+)$/g;
343 1 my $type = type_from_ext($ext);
344 1 return $type;
345 }
346  
347 1 ▶sub _read_binary {
Subroutine _read_binary: called CC=1 SCAR=0.0
348 1 my ( $self, $file_path ) = @_;
349 1 my $bin_data = read_file( $file_path, binmode => ':raw' ) ;
350 1 return $bin_data;
351 }
352  
353 1 ▶sub _generate_document {
Subroutine _generate_document: called CC=3 SCAR=12.6
354 1 my ( $self, $fields ) = @_;
355  
356 1 my $output_str;
357 1 ▶ if ( $self->content_type eq 'application/xml' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->content_type eq 'application/xml') { } 0 1 1
elsif ($self->content_type eq 'application/json') { } 1 0 1
358 0 require XML::Simple;
359 0 my $xs = XML::Simple->new;
360 0 $output_str = $xs->XMLout($fields, NoAttr => 1, RootName=>'doc',);
361 } elsif ( $self->content_type eq 'application/json' ) {
362 1 require JSON;
363 1 $output_str = JSON::encode_json({ doc => $fields });
364 }
365  
366 1 return $output_str;
367 }
368  
369 4 __PACKAGE__->meta->make_immutable;
370  
371 =head1 AUTHOR
372  
373 Logan Bell, C<< <loganbell at gmail.com> >>
374  
375 =head1 BUGS
376  
377 Please report any bugs or feature requests to C<bug-dbix-class-indexer-webservice-dezi at rt.cpan.org>, or through
378 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-Indexer-WebService-Dezi>. I will be notified, and then you'll
379 automatically be notified of progress on your bug as I make changes.
380  
381 =head1 SUPPORT
382  
383 You can find documentation for this module with the perldoc command.
384  
385 perldoc DBIx::Class::Indexer::WebService::Dezi
386  
387  
388 You can also look for information at:
389  
390 =over 4
391  
392 =item * RT: CPAN's request tracker (report bugs here)
393  
394 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-Indexer-WebService-Dezi>
395  
396 =item * AnnoCPAN: Annotated CPAN documentation
397  
398 L<http://annocpan.org/dist/DBIx-Class-Indexer-WebService-Dezi>
399  
400 =item * CPAN Ratings
401  
402 L<http://cpanratings.perl.org/d/DBIx-Class-Indexer-WebService-Dezi>
403  
404 =item * Search CPAN
405  
406 L<http://search.cpan.org/dist/DBIx-Class-Indexer-WebService-Dezi/>
407  
408 =back
409  
410 =head1 ACKNOWLEDGEMENTS
411  
412 =head1 LICENSE AND COPYRIGHT
413  
414 Copyright 2012 Logan Bell.
415  
416 This program is free software; you can redistribute it and/or modify it
417 under the terms of either: the GNU General Public License as published
418 by the Free Software Foundation; or the Artistic License.
419  
420 See http://dev.perl.org/licenses/ for more information.
421  
422 =cut
423  
424 4 1; # End of DBIx::Class::Indexer::WebService::Dezi
↑ Summary