File Coverage

blib/lib/Dezi/Indexer/Doc.pm
Criterion Covered Total %
statement 22 25 88.0
branch n/a
condition n/a
subroutine 8 9 88.8
pod n/a
total 30 34 88.2


line stmt bran cond sub pod time code
1             package Dezi::Indexer::Doc;
2 1     1   2613 use Moose;
  1         2  
  1         9  
3 1     1   9228 use MooseX::XSAccessor;
  1         10803  
  1         7  
4             with 'Dezi::Role';
5 1     1   12331 use Types::Standard qw( Str HashRef Int InstanceOf Maybe );
  1         3  
  1         15  
6 1     1   1178 use Dezi::Types qw( DeziUriStr DeziEpoch );
  1         4  
  1         9  
7 1     1   691 use Carp;
  1         4  
  1         88  
8 1     1   7 use Data::Dump qw( dump );
  1         2  
  1         108  
9             use overload(
10             '""' => \&as_string,
11 0     0   0 'bool' => sub {1},
12 1         14 fallback => 1,
13 1     1   10 );
  1         3  
14 1     1   764 use SWISH::3::Headers;
  0            
  0            
15              
16             use namespace::autoclean;
17              
18             our $VERSION = '0.014';
19              
20             my $default_headers = SWISH::3::Headers->new();
21              
22             my ( $locale, $lang, $charset );
23             {
24              
25             # inside a block to reduce impact on any regex
26             use POSIX;
27              
28             $locale = setlocale(POSIX::LC_CTYPE);
29             ( $lang, $charset ) = split( m/\./, $locale );
30             $charset ||= 'iso-8859-1';
31             }
32              
33             has 'url' => ( is => 'rw', isa => DeziUriStr, coerce => 1 );
34             has 'modtime' => (
35             is => 'rw',
36             isa => DeziEpoch,
37             coerce => 1,
38             default => sub { time() },
39             lazy => 1,
40             );
41             has 'type' => ( is => 'rw', isa => Str );
42             has 'parser' => ( is => 'rw', isa => Maybe [Str], );
43             has 'content' => ( is => 'rw', isa => Str );
44             has 'action' => ( is => 'rw', isa => Maybe [Str], );
45             has 'data' => ( is => 'rw', isa => HashRef );
46             has 'size' => ( is => 'rw', isa => Int );
47             has 'charset' => ( is => 'rw', isa => Str, default => sub {$charset} );
48             has 'version' => ( is => 'rw', isa => Str, default => sub {3} );
49             has 'headers' => (
50             is => 'rw',
51             isa => InstanceOf ['SWISH::3::Headers'],
52             default => sub {$default_headers},
53             required => 1,
54             );
55              
56             =pod
57              
58             =head1 NAME
59              
60             Dezi::Indexer::Doc - Document object class for passing to Dezi::Indexer
61              
62             =head1 SYNOPSIS
63              
64             # subclass Dezi::Indexer::Doc
65             # and override filter() method
66            
67             package MyDoc;
68             use Moose;
69             extends 'Dezi::Indexer::Doc';
70            
71             sub filter {
72             my $doc = shift;
73            
74             # alter url
75             my $url = $doc->url;
76             $url =~ s/my.foo.com/my.bar.org/;
77             $doc->url( $url );
78            
79             # alter content
80             my $buf = $doc->content;
81             $buf =~ s/foo/bar/gi;
82             $doc->content( $buf );
83             }
84            
85             1;
86              
87             =head1 DESCRIPTION
88              
89             Dezi::Indexer::Doc is the base class for Doc objects in the Dezi
90             framework. Doc objects are created by Dezi::Aggregator classes
91             and processed by Dezi::Indexer classes.
92              
93             You can subclass Dezi::Indexer::Doc and add a filter() method to alter
94             the values of the Doc object before it is indexed.
95              
96             =head1 METHODS
97              
98             All of the following methods may be overridden when subclassing
99             this module, but the recommendation is to override only filter().
100              
101             =head2 new
102              
103             Instantiate Doc object.
104              
105             All of the following params are also available as accessors/mutators.
106              
107             =over
108              
109             =item url
110              
111             =item type
112              
113             =item content
114              
115             =item parser
116              
117             =item modtime
118              
119             =item size
120              
121             =item action
122              
123             =item debug
124              
125             =item charset
126              
127             Note that the charset is derived from the B<LC_CTYPE> environment variable.
128              
129             =item data
130              
131             A hashref.
132              
133             =item version
134              
135             Swish-e 2.x or Swish3 style headers. Value should be C<2> or C<3>.
136             Default is C<3>.
137              
138             =back
139              
140             =cut
141              
142             =head2 BUILD
143              
144             Calls filter() on object.
145              
146             =cut
147              
148             sub BUILD {
149             my $self = shift;
150             $self->filter();
151             }
152              
153             =head2 filter
154              
155             Override this method to alter the values in the object prior to it
156             being process()ed by the Indexer.
157              
158             The default is to do nothing.
159              
160             This method can also be set using the filter() callback in Dezi::App->new().
161              
162             =cut
163              
164             sub filter { }
165              
166             =head2 as_string
167              
168             Return the Doc object rendered as a scalar string, ready to be indexed.
169             This will include the proper headers. See L<SWISH::3::Headers>.
170              
171             B<NOTE:> as_string() is also used if you use a Doc object as a string.
172             Example:
173              
174             print $doc->as_string; # one way
175             print $doc; # same thing
176              
177             =cut
178              
179             sub as_string {
180             my $self = shift;
181              
182             my $body = $self->content;
183              
184             # we ignore size() and let Headers compute it based on actual content()
185             return $self->headers->head(
186             $body,
187             { url => $self->url,
188             modtime => $self->modtime,
189             type => $self->type,
190             action => $self->action,
191             parser => $self->parser,
192             version => $self->version,
193             }
194             ) . $body;
195              
196             }
197              
198             __PACKAGE__->meta->make_immutable;
199              
200             1;
201              
202             __END__
203              
204             =head1 AUTHOR
205              
206             Peter Karman, E<lt>perl@peknet.comE<gt>
207              
208             =head1 BUGS
209              
210             Please report any bugs or feature requests to C<bug-swish-prog at rt.cpan.org>, or through
211             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dezi-App>.
212             I will be notified, and then you'll
213             automatically be notified of progress on your bug as I make changes.
214              
215             =head1 SUPPORT
216              
217             You can find documentation for this module with the perldoc command.
218              
219             perldoc Dezi
220              
221              
222             You can also look for information at:
223              
224             =over 4
225              
226             =item * Mailing list
227              
228             L<http://lists.swish-e.org/listinfo/users>
229              
230             =item * RT: CPAN's request tracker
231              
232             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dezi-App>
233              
234             =item * AnnoCPAN: Annotated CPAN documentation
235              
236             L<http://annocpan.org/dist/Dezi-App>
237              
238             =item * CPAN Ratings
239              
240             L<http://cpanratings.perl.org/d/Dezi-App>
241              
242             =item * Search CPAN
243              
244             L<http://search.cpan.org/dist/Dezi-App/>
245              
246             =back
247              
248             =head1 COPYRIGHT AND LICENSE
249              
250             Copyright 2008-2009 by Peter Karman
251              
252             This library is free software; you can redistribute it and/or modify
253             it under the same terms as Perl itself.
254              
255             =head1 SEE ALSO
256              
257             L<http://swish-e.org/>