blib/lib/XML/Grammar/Fortune/Synd.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/XML/Grammar/Fortune/Synd/App.pm ↑ Summary
1 package XML::Grammar::Fortune::Synd;
2 $XML::Grammar::Fortune::Synd::VERSION = '0.0212';
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 3 ▶use parent 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use YAML::XS (qw( DumpFile LoadFile ));
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Heap::Elem::Ref (qw(RefElem));
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Heap::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use XML::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use XML::Grammar::Fortune;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use DateTime::Format::W3CDTF;
17 use XML::Grammar::Fortune::Synd::Heap::Elem;
18  
19 use File::Spec;
20  
21 __PACKAGE__->mk_accessors(qw(
22 _xml_parser
23 _file_doms
24 _date_formatter
25 xml_files
26 url_callback
27 _file_processors
28 ));
29  
30  
31 sub new
32 {
33 my ($class, $args) = @_;
34  
35 my $self = $class->SUPER::new($args);
36  
37 $self->_xml_parser(XML::LibXML->new());
38 $self->_date_formatter(DateTime::Format::W3CDTF->new());
39 $self->_file_doms(+{});
40 $self->_file_processors(+{});
41  
42 return $self;
43 }
44  
45  
46 sub calc_feeds
47 {
48 my ($self, $args) = @_;
49  
50 my $scripts_hash_filename = $args->{'yaml_persistence_file'};
51 my $scripts_hash_fn_out = $args->{'yaml_persistence_file_out'};
52 my $xmls_dir = $args->{xmls_dir};
53  
54  
55 my $persistent_data;
56 if (-e $scripts_hash_filename)
57 {
58 $persistent_data = LoadFile ($scripts_hash_filename);
59 }
60 else
61 {
62 $persistent_data = +{};
63 }
64  
65 if (!exists($persistent_data->{'files'}))
66 {
67 $persistent_data->{'files'} = +{};
68 }
69  
70 my $scripts_hash = $persistent_data->{'files'};
71  
72 my $ids_heap = Heap::Binary->new();
73  
74 my $ids_heap_count = 0;
75  
76 my $ids_limit = 20;
77  
78 foreach my $file (@{$self->xml_files()})
79 {
80 my $xml = $self->_xml_parser->parse_file(
81 File::Spec->catfile($xmls_dir, $file)
82 );
83  
84 $self->_file_doms->{$file} = $xml;
85  
86 my @fortune_elems = $xml->findnodes("//fortune");
87  
88 my @ids = (map { $_->getAttribute("id") } @fortune_elems);
89  
90 my $id_count = 1;
91  
92 # Get rid of IDs in the hash refs that don't exist in the file,
93 # so we won't have globally duplicate IDs.
94 {
95 my $hash_ref = $scripts_hash->{$file};
96 my %ids_map = (map { $_ => 1 } @ids);
97  
98 foreach my $id (keys(%$hash_ref))
99 {
100 if (! exists($ids_map{$id}))
101 {
102 delete ($hash_ref->{$id});
103 }
104 }
105 }
106  
107 IDS_LOOP:
108 foreach my $id (@ids)
109 {
110 if (! exists($scripts_hash->{$file}->{$id}))
111 {
112 $scripts_hash->{$file}->{$id} =
113 {
114 'date' => $self->_date_formatter->format_datetime(
115 DateTime->now(),
116 ),
117 };
118 }
119  
120 my $date = $self->_date_formatter->parse_datetime(
121 $scripts_hash->{$file}->{$id}->{'date'},
122 );
123  
124 $ids_heap->add(
125 RefElem(
126 XML::Grammar::Fortune::Synd::Heap::Elem->new(
127 {
128 date => $date,
129 idx => $id_count,
130 id => $id,
131 file => $file,
132 }
133 )
134 )
135 );
136  
137 if (++$ids_heap_count > $ids_limit)
138 {
139 $ids_heap->extract_top();
140 $ids_heap_count--;
141 }
142 }
143 continue
144 {
145 $id_count++;
146 }
147 }
148  
149 my @recent_ids = ();
150  
151 # TODO : Should we reverse this?
152 while (defined(my $id_obj = $ids_heap->extract_top()))
153 {
154 push @recent_ids, $id_obj;
155 }
156 DumpFile($scripts_hash_fn_out, $persistent_data);
157  
158 my @feed_formats = (qw(Atom RSS));
159  
160 my %feeds = (map { $_ => XML::Feed->new($_), } @feed_formats);
161  
162 # First set some global parameters
163 foreach my $feed (values(%feeds))
164 {
165 $feed->title($args->{feed_params}->{'title'});
166 $feed->link($args->{feed_params}->{'link'});
167 $feed->tagline($args->{feed_params}->{'tagline'});
168 $feed->author($args->{feed_params}->{'author'});
169  
170 my $self_link = $args->{feed_params}->{'atom_self_link'};
171 $feed->self_link($self_link);
172 $feed->id($self_link);
173 }
174  
175 # Now fill the XML-Feed object:
176 {
177  
178 foreach my $id_obj (map { $_->val() } @recent_ids)
179 {
180 my $file_dom =
181 $self->_file_doms()->{$id_obj->file()};
182  
183 my ($fortune_dom) =
184 $file_dom->findnodes("descendant::fortune[\@id='". $id_obj->id() . "']");
185  
186 my %entries = (map { $_ => XML::Feed::Entry->new($_) } @feed_formats);
187  
188 my $title = $fortune_dom->findnodes("meta/title")->get_node(0)->textContent();
189  
190 my $on_entries = sub {
191  
192 my ($callback) = @_;
193  
194 foreach my $entry (values(%entries))
195 {
196 $callback->($entry);
197 }
198 };
199  
200 $on_entries->(sub {
201 my $entry = shift;
202  
203 $entry->title($title);
204 $entry->summary($title);
205 });
206  
207 my $url =
208 $self->url_callback()->(
209 $self,
210 {
211 id_obj => $id_obj,
212 }
213 );
214  
215 $on_entries->(sub {
216 my $entry = shift;
217  
218 $entry->link( $url );
219  
220 $entry->id($url);
221  
222 $entry->issued($id_obj->date());
223 });
224  
225 {
226 $self->_file_processors()->{$id_obj->file()} ||=
227 XML::Grammar::Fortune->new(
228 {
229 mode => "convert_to_html",
230 output_mode => "string",
231 }
232 );
233  
234 my $file_processor =
235 $self->_file_processors()->{$id_obj->file()};
236  
237 my $content = "";
238  
239 $file_processor->run(
240 {
241 xslt_params =>
242 {
243 'fortune.id' => "'" . $id_obj->id() . "'",
244 },
245 output => \$content,
246 input => File::Spec->catfile($xmls_dir, $id_obj->file()),
247 }
248 );
249  
250 $content =~ s{\A.*?<body>}{}ms;
251 $content =~ s{</body>.*\z}{}ms;
252  
253 $on_entries->(sub {
254 my $entry = shift;
255  
256 $entry->content(
257 XML::Feed::Content->new(
258 {
259 type => "text/html",
260 body => $content,
261 },
262 )
263 );
264  
265 });
266 }
267  
268 foreach my $format (@feed_formats)
269 {
270 $feeds{$format}->add_entry($entries{$format});
271 }
272 }
273 }
274  
275 $feeds{"RSS"}->self_link($args->{feed_params}->{'rss_self_link'});
276  
277 {
278 my $num_entries = scalar (() = $feeds{'RSS'}->entries());
279 if ($num_entries > $ids_limit)
280 {
281 die "Assert failed. $num_entries rather than the $ids_limit limit.";
282 }
283 }
284  
285 return
286 {
287 'recent_ids' => [reverse(@recent_ids)],
288 'feeds' =>
289 {
290 'Atom' => $feeds{"Atom"},
291 'rss20' => $feeds{"RSS"},
292 },
293 };
294 }
295  
296  
297  
298 1; # End of XML::Grammar::Fortune::Synd
299  
300 __END__
301  
302 =pod
303  
304 =encoding UTF-8
305  
306 =head1 NAME
307  
308 XML::Grammar::Fortune::Synd - Provides syndication for a set of
309 XML-Grammar-Fortune files.
310  
311 =head1 VERSION
312  
313 version 0.0212
314  
315 =head1 SYNOPSIS
316  
317 use XML::Grammar::Fortune::Synd;
318  
319 my $syndicator = XML::Grammar::Fortune::Synd->new();
320 ...
321  
322 =head1 VERSION
323  
324 version 0.0212
325  
326 =head1 FUNCTIONS
327  
328 =head2 my $syndicator = $class->new(\%args)
329  
330 Returns the new Syndicator.
331  
332 =head2 $syndicator->calc_feeds(\%args)
333  
334 C<\%args> should be:
335  
336 {
337 yaml_persistence_file => "/path/to/yaml-persistence.yaml",
338 yaml_persistence_file_out => "/path/to/yaml-persistence.yaml",
339 xml_dirs => "/path/to/the/directory-containing-xmls",
340 feed_params =>
341 {
342 title => "My feed title",
343 link => "http://mysite.tld/",
344 tagline => "Feed tagline",
345 author => "john.doe@hello.tld (John Doe)"
346 atom_self_link => "http://mysite.tld/my-feed.atom",
347 rss_self_link => "http://mysite.tld/my-feed.rss",
348 }
349 }
350  
351 Returns:
352  
353 {
354 recent-ids => \@list_of_recent_ids,
355 feeds =>
356 {
357 Atom => $atom_XML_Feed_obj,
358 rss20 => $rss_XML_Feed_obj,
359 },
360 }
361  
362 =head1 AUTHOR
363  
364 Shlomi Fish, C<< <shlomif at cpan.org> >>
365  
366 =head1 BUGS
367  
368 Please report any bugs or feature requests to C<bug-xml-grammar-fortune-synd at rt.cpan.org>, or through
369 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-Grammar-Fortune-Synd>. I will be notified, and then you'll
370 automatically be notified of progress on your bug as I make changes.
371  
372 =head1 SUPPORT
373  
374 You can find documentation for this module with the perldoc command.
375  
376 perldoc XML::Grammar::Fortune::Synd
377  
378 You can also look for information at:
379  
380 =over 4
381  
382 =item * RT: CPAN's request tracker
383  
384 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-Grammar-Fortune-Synd>
385  
386 =item * AnnoCPAN: Annotated CPAN documentation
387  
388 L<http://annocpan.org/dist/XML-Grammar-Fortune-Synd>
389  
390 =item * CPAN Ratings
391  
392 L<http://cpanratings.perl.org/d/XML-Grammar-Fortune-Synd>
393  
394 =item * Search CPAN
395  
396 L<http://search.cpan.org/dist/XML-Grammar-Fortune-Synd>
397  
398 =back
399  
400 =head1 ACKNOWLEDGEMENTS
401  
402 =head1 COPYRIGHT & LICENSE
403  
404 Copyright 2008 Shlomi Fish, all rights reserved.
405  
406 This program is released under the following license: MIT/X11 License
407  
408 http://www.opensource.org/licenses/mit-license.php
409  
410 =head1 AUTHOR
411  
412 Shlomi Fish <shlomif@cpan.org>
413  
414 =head1 COPYRIGHT AND LICENSE
415  
416 This software is Copyright (c) 2018 by Shlomi Fish.
417  
418 This is free software, licensed under:
419  
420 The MIT (X11) License
421  
422 =head1 BUGS
423  
424 Please report any bugs or feature requests on the bugtracker website
425 L<https://github.com/shlomif/xml-grammar-fortune-synd/issues>
426  
427 When submitting a bug or request, please include a test-file or a
428 patch to an existing test-file that illustrates the bug or desired
429 feature.
430  
431 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
432  
433 =head1 SUPPORT
434  
435 =head2 Perldoc
436  
437 You can find documentation for this module with the perldoc command.
438  
439 perldoc XML::Grammar::Fortune::Synd
440  
441 =head2 Websites
442  
443 The following websites have more information about this module, and may be of help to you. As always,
444 in addition to those websites please use your favorite search engine to discover more resources.
445  
446 =over 4
447  
448 =item *
449  
450 MetaCPAN
451  
452 A modern, open-source CPAN search engine, useful to view POD in HTML format.
453  
454 L<https://metacpan.org/release/XML-Grammar-Fortune-Synd>
455  
456 =item *
457  
458 Search CPAN
459  
460 The default CPAN search engine, useful to view POD in HTML format.
461  
462 L<http://search.cpan.org/dist/XML-Grammar-Fortune-Synd>
463  
464 =item *
465  
466 RT: CPAN's Bug Tracker
467  
468 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
469  
470 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-Grammar-Fortune-Synd>
471  
472 =item *
473  
474 AnnoCPAN
475  
476 The AnnoCPAN is a website that allows community annotations of Perl module documentation.
477  
478 L<http://annocpan.org/dist/XML-Grammar-Fortune-Synd>
479  
480 =item *
481  
482 CPAN Ratings
483  
484 The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
485  
486 L<http://cpanratings.perl.org/d/XML-Grammar-Fortune-Synd>
487  
488 =item *
489  
490 CPANTS
491  
492 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
493  
494 L<http://cpants.cpanauthors.org/dist/XML-Grammar-Fortune-Synd>
495  
496 =item *
497  
498 CPAN Testers
499  
500 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
501  
502 L<http://www.cpantesters.org/distro/X/XML-Grammar-Fortune-Synd>
503  
504 =item *
505  
506 CPAN Testers Matrix
507  
508 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
509  
510 L<http://matrix.cpantesters.org/?dist=XML-Grammar-Fortune-Synd>
511  
512 =item *
513  
514 CPAN Testers Dependencies
515  
516 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
517  
518 L<http://deps.cpantesters.org/?module=XML::Grammar::Fortune::Synd>
519  
520 =back
521  
522 =head2 Bugs / Feature Requests
523  
524 Please report any bugs or feature requests by email to C<bug-xml-grammar-fortune-synd at rt.cpan.org>, or through
525 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-Grammar-Fortune-Synd>. You will be automatically notified of any
526 progress on the request by the system.
527  
528 =head2 Source Code
529  
530 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
531 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
532 from your repository :)
533  
534 L<https://github.com/shlomif/xml-grammar-fortune-synd>
535  
536 git clone https://bitbucket.org/shlomif/fortune-xml
537  
538 =cut
« blib/lib/XML/Grammar/Fortune/Synd/App.pm ↑ Summary