Synd.pm

filter: Statementstmt 93.7% 30 / 32 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.3% 41 / 43 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Synd/App.pm ↑ Summary
1 package XML::Grammar::Fortune::Synd;
2 $XML::Grammar::Fortune::Synd::VERSION = '0.0400';
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 2 ▶use parent 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Path::Tiny qw/ path tempdir tempfile cwd /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use File::Update qw( write_on_change_no_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use YAML::XS (qw( Dump DumpFile LoadFile ));
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Heap::Elem::Ref (qw(RefElem));
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Heap::Binary ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use XML::Feed ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶use XML::Grammar::Fortune ();
Subroutine BEGIN: called CC=1 SCAR=2.6
19 use DateTime::Format::W3CDTF ();
20 use XML::Grammar::Fortune::Synd::Heap::Elem ();
21  
22 use File::Spec ();
23  
24 __PACKAGE__->mk_accessors(
25 qw(
26 _xml_parser
27 _file_doms
28 _date_formatter
29 xml_files
30 url_callback
31 _file_processors
32 )
33 );
34  
35  
36 sub new
37 {
38 my ( $class, $args ) = @_;
39  
40 my $self = $class->SUPER::new($args);
41  
42 $self->_xml_parser( XML::LibXML->new() );
43 $self->_date_formatter( DateTime::Format::W3CDTF->new() );
44 $self->_file_doms( +{} );
45 $self->_file_processors( +{} );
46  
47 return $self;
48 }
49  
50  
51 sub calc_feeds
52 {
53 my ( $self, $args ) = @_;
54  
55 my $scripts_hash_filename = $args->{'yaml_persistence_file'};
56 my $scripts_hash_fn_out = $args->{'yaml_persistence_file_out'};
57 my $xmls_dir = $args->{xmls_dir};
58  
59 my $persistent_data;
60 if ( -e $scripts_hash_filename )
61 {
62 $persistent_data = LoadFile($scripts_hash_filename);
63 }
64 else
65 {
66 $persistent_data = +{};
67 }
68  
69 if ( !exists( $persistent_data->{'files'} ) )
70 {
71 $persistent_data->{'files'} = +{};
72 }
73  
74 my $scripts_hash = $persistent_data->{'files'};
75  
76 my $ids_heap = Heap::Binary->new();
77  
78 my $ids_heap_count = 0;
79  
80 my $ids_limit = 20;
81  
82 foreach my $file ( @{ $self->xml_files() } )
83 {
84 my $xml = $self->_xml_parser->parse_file(
85 File::Spec->catfile( $xmls_dir, $file ) );
86  
87 $self->_file_doms->{$file} = $xml;
88  
89 my @fortune_elems = $xml->findnodes("//fortune");
90  
91 my @ids = ( map { $_->getAttribute("id") } @fortune_elems );
92  
93 my $id_count = 1;
94  
95 # Get rid of IDs in the hash refs that don't exist in the file,
96 # so we won't have globally duplicate IDs.
97 {
98 my $hash_ref = $scripts_hash->{$file};
99 my %ids_map = ( map { $_ => 1 } @ids );
100  
101 foreach my $id ( keys(%$hash_ref) )
102 {
103 if ( !exists( $ids_map{$id} ) )
104 {
105 delete( $hash_ref->{$id} );
106 }
107 }
108 }
109  
110 IDS_LOOP:
111 foreach my $id (@ids)
112 {
113 if ( !exists( $scripts_hash->{$file}->{$id} ) )
114 {
115 $scripts_hash->{$file}->{$id} = {
116 'date' => $self->_date_formatter->format_datetime(
117 DateTime->now(),
118 ),
119 };
120 }
121  
122 my $date = $self->_date_formatter->parse_datetime(
123 $scripts_hash->{$file}->{$id}->{'date'},
124 );
125  
126 $ids_heap->add(
127 RefElem(
128 XML::Grammar::Fortune::Synd::Heap::Elem->new(
129 {
130 date => $date,
131 idx => $id_count,
132 id => $id,
133 file => $file,
134 }
135 )
136 )
137 );
138  
139 if ( ++$ids_heap_count > $ids_limit )
140 {
141 $ids_heap->extract_top();
142 $ids_heap_count--;
143 }
144 }
145 continue
146 {
147 $id_count++;
148 }
149 }
150  
151 my @recent_ids = ();
152  
153 # TODO : Should we reverse this?
154 while ( defined( my $id_obj = $ids_heap->extract_top() ) )
155 {
156 push @recent_ids, $id_obj;
157 }
158 write_on_change_no_utf8(
159 path( $scripts_hash_fn_out),
160 \(Dump($persistent_data) ),
161 );
162  
163 my @feed_formats = (qw(Atom RSS));
164  
165 my %feeds = ( map { $_ => XML::Feed->new($_), } @feed_formats );
166  
167 # First set some global parameters
168 foreach my $feed ( values(%feeds) )
169 {
170 $feed->title( $args->{feed_params}->{'title'} );
171 $feed->link( $args->{feed_params}->{'link'} );
172 $feed->tagline( $args->{feed_params}->{'tagline'} );
173 $feed->author( $args->{feed_params}->{'author'} );
174  
175 my $self_link = $args->{feed_params}->{'atom_self_link'};
176 $feed->self_link($self_link);
177 $feed->id($self_link);
178 }
179  
180 # Now fill the XML-Feed object:
181 {
182  
183 foreach my $id_obj ( map { $_->val() } @recent_ids )
184 {
185 my $file_dom =
186 $self->_file_doms()->{ $id_obj->file() };
187  
188 my ($fortune_dom) =
189 $file_dom->findnodes(
190 "descendant::fortune[\@id='" . $id_obj->id() . "']" );
191  
192 my %entries =
193 ( map { $_ => XML::Feed::Entry->new($_) } @feed_formats );
194  
195 my $title = $fortune_dom->findnodes("meta/title")->get_node(0)
196 ->textContent();
197  
198 my $on_entries = sub {
199  
200 my ($callback) = @_;
201  
202 foreach my $entry ( values(%entries) )
203 {
204 $callback->($entry);
205 }
206 };
207  
208 $on_entries->(
209 sub {
210 my $entry = shift;
211  
212 $entry->title($title);
213 $entry->summary($title);
214 }
215 );
216  
217 my $url = $self->url_callback()->(
218 $self,
219 {
220 id_obj => $id_obj,
221 }
222 );
223  
224 $on_entries->(
225 sub {
226 my $entry = shift;
227  
228 $entry->link($url);
229  
230 $entry->id($url);
231  
232 $entry->issued( $id_obj->date() );
233 }
234 );
235  
236 {
237 $self->_file_processors()->{ $id_obj->file() } ||=
238 XML::Grammar::Fortune->new(
239 {
240 mode => "convert_to_html",
241 output_mode => "string",
242 }
243 );
244  
245 my $file_processor =
246 $self->_file_processors()->{ $id_obj->file() };
247  
248 my $content = "";
249  
250 $file_processor->run(
251 {
252 xslt_params => {
253 'fortune.id' => "'" . $id_obj->id() . "'",
254 },
255 output => \$content,
256 input =>
257 File::Spec->catfile( $xmls_dir, $id_obj->file() ),
258 }
259 );
260  
261 $content =~ s{\A.*?<body>}{}ms;
262 $content =~ s{</body>.*\z}{}ms;
263  
264 $on_entries->(
265 sub {
266 my $entry = shift;
267  
268 $entry->content(
269 XML::Feed::Content->new(
270 {
271 type => "text/html",
272 body => $content,
273 },
274 )
275 );
276  
277 }
278 );
279 }
280  
281 foreach my $format (@feed_formats)
282 {
283 $feeds{$format}->add_entry( $entries{$format} );
284 }
285 }
286 }
287  
288 $feeds{"RSS"}->self_link( $args->{feed_params}->{'rss_self_link'} );
289  
290 {
291 my $num_entries = scalar( () = $feeds{'RSS'}->entries() );
292 if ( $num_entries > $ids_limit )
293 {
294 die "Assert failed. $num_entries rather than the $ids_limit limit.";
295 }
296 }
297  
298 return {
299 'recent_ids' => [ reverse(@recent_ids) ],
300 'feeds' => {
301 'Atom' => $feeds{"Atom"},
302 'rss20' => $feeds{"RSS"},
303 },
304 };
305 }
306  
307  
308 1; # End of XML::Grammar::Fortune::Synd
309  
310 __END__
« Synd/App.pm ↑ Summary