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.0214';
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.0214
314  
315 =head1 SYNOPSIS
316  
317 use XML::Grammar::Fortune::Synd;
318  
319 my $syndicator = XML::Grammar::Fortune::Synd->new();
320 ...
321  
322 =head1 FUNCTIONS
323  
324 =head2 my $syndicator = $class->new(\%args)
325  
326 Returns the new Syndicator.
327  
328 =head2 $syndicator->calc_feeds(\%args)
329  
330 C<\%args> should be:
331  
332 {
333 yaml_persistence_file => "/path/to/yaml-persistence.yaml",
334 yaml_persistence_file_out => "/path/to/yaml-persistence.yaml",
335 xml_dirs => "/path/to/the/directory-containing-xmls",
336 feed_params =>
337 {
338 title => "My feed title",
339 link => "http://mysite.tld/",
340 tagline => "Feed tagline",
341 author => "john.doe@hello.tld (John Doe)"
342 atom_self_link => "http://mysite.tld/my-feed.atom",
343 rss_self_link => "http://mysite.tld/my-feed.rss",
344 }
345 }
346  
347 Returns:
348  
349 {
350 recent-ids => \@list_of_recent_ids,
351 feeds =>
352 {
353 Atom => $atom_XML_Feed_obj,
354 rss20 => $rss_XML_Feed_obj,
355 },
356 }
357  
358 =head1 AUTHOR
359  
360 Shlomi Fish, C<< <shlomif at cpan.org> >>
361  
362 =head1 BUGS
363  
364 Please report any bugs or feature requests to C<bug-xml-grammar-fortune-synd at rt.cpan.org>, or through
365 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
366 automatically be notified of progress on your bug as I make changes.
367  
368 =head1 SUPPORT
369  
370 You can find documentation for this module with the perldoc command.
371  
372 perldoc XML::Grammar::Fortune::Synd
373  
374 You can also look for information at:
375  
376 =over 4
377  
378 =item * RT: CPAN's request tracker
379  
380 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-Grammar-Fortune-Synd>
381  
382 =item * AnnoCPAN: Annotated CPAN documentation
383  
384 L<http://annocpan.org/dist/XML-Grammar-Fortune-Synd>
385  
386 =item * CPAN Ratings
387  
388 L<http://cpanratings.perl.org/d/XML-Grammar-Fortune-Synd>
389  
390 =item * Search CPAN
391  
392 L<http://search.cpan.org/dist/XML-Grammar-Fortune-Synd>
393  
394 =back
395  
396 =head1 ACKNOWLEDGEMENTS
397  
398 =head1 COPYRIGHT & LICENSE
399  
400 Copyright 2008 Shlomi Fish, all rights reserved.
401  
402 This program is released under the following license: MIT/X11 License
403  
404 http://www.opensource.org/licenses/mit-license.php
405  
406 =head1 AUTHOR
407  
408 Shlomi Fish <shlomif@cpan.org>
409  
410 =head1 COPYRIGHT AND LICENSE
411  
412 This software is Copyright (c) 2019 by Shlomi Fish.
413  
414 This is free software, licensed under:
415  
416 The MIT (X11) License
417  
418 =head1 BUGS
419  
420 Please report any bugs or feature requests on the bugtracker website
421 L<https://github.com/shlomif/fortune-xml/issues>
422  
423 When submitting a bug or request, please include a test-file or a
424 patch to an existing test-file that illustrates the bug or desired
425 feature.
426  
427 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
428  
429 =head1 SUPPORT
430  
431 =head2 Perldoc
432  
433 You can find documentation for this module with the perldoc command.
434  
435 perldoc XML::Grammar::Fortune::Synd
436  
437 =head2 Websites
438  
439 The following websites have more information about this module, and may be of help to you. As always,
440 in addition to those websites please use your favorite search engine to discover more resources.
441  
442 =over 4
443  
444 =item *
445  
446 MetaCPAN
447  
448 A modern, open-source CPAN search engine, useful to view POD in HTML format.
449  
450 L<https://metacpan.org/release/XML-Grammar-Fortune-Synd>
451  
452 =item *
453  
454 Search CPAN
455  
456 The default CPAN search engine, useful to view POD in HTML format.
457  
458 L<http://search.cpan.org/dist/XML-Grammar-Fortune-Synd>
459  
460 =item *
461  
462 RT: CPAN's Bug Tracker
463  
464 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
465  
466 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-Grammar-Fortune-Synd>
467  
468 =item *
469  
470 AnnoCPAN
471  
472 The AnnoCPAN is a website that allows community annotations of Perl module documentation.
473  
474 L<http://annocpan.org/dist/XML-Grammar-Fortune-Synd>
475  
476 =item *
477  
478 CPAN Ratings
479  
480 The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
481  
482 L<http://cpanratings.perl.org/d/XML-Grammar-Fortune-Synd>
483  
484 =item *
485  
486 CPANTS
487  
488 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
489  
490 L<http://cpants.cpanauthors.org/dist/XML-Grammar-Fortune-Synd>
491  
492 =item *
493  
494 CPAN Testers
495  
496 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
497  
498 L<http://www.cpantesters.org/distro/X/XML-Grammar-Fortune-Synd>
499  
500 =item *
501  
502 CPAN Testers Matrix
503  
504 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
505  
506 L<http://matrix.cpantesters.org/?dist=XML-Grammar-Fortune-Synd>
507  
508 =item *
509  
510 CPAN Testers Dependencies
511  
512 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
513  
514 L<http://deps.cpantesters.org/?module=XML::Grammar::Fortune::Synd>
515  
516 =back
517  
518 =head2 Bugs / Feature Requests
519  
520 Please report any bugs or feature requests by email to C<bug-xml-grammar-fortune-synd at rt.cpan.org>, or through
521 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
522 progress on the request by the system.
523  
524 =head2 Source Code
525  
526 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
527 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
528 from your repository :)
529  
530 L<https://github.com/shlomif/fortune-xml>
531  
532 git clone git://github.com/shlomif/fortune-xml.git
533  
534 =cut
« blib/lib/XML/Grammar/Fortune/Synd/App.pm ↑ Summary