blib/lib/HTML/Latemp/News.pm

filter: Statementstmt 12.9% 15 / 116 Branchbran 0.0% 0 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 20.0% 6 / 30 Podpod n/a totaltotal 10.9% 21 / 192 CC 25 SCAR 17.4
CC
25
Cov
9%
initialize
12.0
author
6.0
category
6.0
CRAP 5.7
↑ Summary
1 package HTML::Latemp::News;
2 $HTML::Latemp::News::VERSION = '0.2.2';
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 package HTML::Latemp::News::Base;
10 $HTML::Latemp::News::Base::VERSION = '0.2.2';
11 1 ▶use CGI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new
14 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
15 0 my $class = shift;
16 0 my $self = {};
17 0 bless $self, $class;
18 0 $self->initialize(@_);
19 0 return $self;
20 }
21  
22 package HTML::Latemp::News::Item;
23 $HTML::Latemp::News::Item::VERSION = '0.2.2';
24 our @ISA = (qw(HTML::Latemp::News::Base));
25  
26 sub author
27 0 ▶{
Subroutine author: not called CC=2 SCAR=17.9
28 0 my $self = shift;
29  
30 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
31 {
32 0 $self->{author} = shift;
33 }
34  
35 0 return $self->{author};
36 }
37  
38 sub category
39 0 ▶{
Subroutine category: not called CC=2 SCAR=17.9
40 0 my $self = shift;
41  
42 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
43 {
44 0 $self->{category} = shift;
45 }
46  
47 0 return $self->{category};
48 }
49  
50 sub date
51 0 ▶{
Subroutine date: not called CC=2 SCAR=17.9
52 0 my $self = shift;
53  
54 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
55 {
56 0 $self->{date} = shift;
57 }
58  
59 0 return $self->{date};
60 }
61  
62 sub description
63 0 ▶{
Subroutine description: not called CC=2 SCAR=17.9
64 0 my $self = shift;
65  
66 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
67 {
68 0 $self->{description} = shift;
69 }
70  
71 0 return $self->{description};
72 }
73  
74 sub id
75 0 ▶{
Subroutine id: not called CC=2 SCAR=17.9
76 0 my $self = shift;
77  
78 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
79 {
80 0 $self->{id} = shift;
81 }
82  
83 0 return $self->{id};
84 }
85  
86 sub index
87 0 ▶{
Subroutine index: not called CC=2 SCAR=17.9
88 0 my $self = shift;
89  
90 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
91 {
92 0 $self->{index} = shift;
93 }
94  
95 0 return $self->{index};
96 }
97  
98 sub text
99 0 ▶{
Subroutine text: not called CC=2 SCAR=17.9
100 0 my $self = shift;
101  
102 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
103 {
104 0 $self->{text} = shift;
105 }
106  
107 0 return $self->{text};
108 }
109  
110 sub title
111 0 ▶{
Subroutine title: not called CC=2 SCAR=17.9
112 0 my $self = shift;
113  
114 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
115 {
116 0 $self->{title} = shift;
117 }
118  
119 0 return $self->{title};
120 }
121  
122 sub initialize
123 0 ▶{
Subroutine initialize: not called CC=3 SCAR=24.8
124 0 my $self = shift;
125  
126 0 my (%args) = (@_);
127  
128 0 foreach my $k ( keys(%args) )
129 {
130 0 ▶ if ( !$self->can($k) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($k)) 0 0 0
131 {
132 0 die "Unknown property for HTML::Latemp::News::Item - \"$k\"!";
133 }
134 0 $self->can($k)->( $self, $args{$k} );
135 }
136 }
137  
138 package HTML::Latemp::News;
139  
140 our @ISA = (qw(HTML::Latemp::News::Base));
141  
142 sub copyright
143 0 ▶{
Subroutine copyright: not called CC=2 SCAR=17.9
144 0 my $self = shift;
145  
146 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
147 {
148 0 $self->{copyright} = shift;
149 }
150  
151 0 return $self->{copyright};
152 }
153  
154 sub description
155 0 ▶{
Subroutine description: not called CC=2 SCAR=17.9
156 0 my $self = shift;
157  
158 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
159 {
160 0 $self->{description} = shift;
161 }
162  
163 0 return $self->{description};
164 }
165  
166 sub docs
167 0 ▶{
Subroutine docs: not called CC=2 SCAR=17.9
168 0 my $self = shift;
169  
170 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
171 {
172 0 $self->{docs} = shift;
173 }
174  
175 0 return $self->{docs};
176 }
177  
178 sub generator
179 0 ▶{
Subroutine generator: not called CC=2 SCAR=17.9
180 0 my $self = shift;
181  
182 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
183 {
184 0 $self->{generator} = shift;
185 }
186  
187 0 return $self->{generator};
188 }
189  
190 sub items
191 0 ▶{
Subroutine items: not called CC=2 SCAR=17.9
192 0 my $self = shift;
193  
194 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
195 {
196 0 $self->{items} = shift;
197 }
198  
199 0 return $self->{items};
200 }
201  
202 sub language
203 0 ▶{
Subroutine language: not called CC=2 SCAR=17.9
204 0 my $self = shift;
205  
206 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
207 {
208 0 $self->{language} = shift;
209 }
210  
211 0 return $self->{language};
212 }
213  
214 sub link_
215 0 ▶{
Subroutine link_: not called CC=2 SCAR=17.9
216 0 my $self = shift;
217  
218 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
219 {
220 0 $self->{link} = shift;
221 }
222  
223 0 return $self->{link};
224 }
225  
226 sub managing_editor
227 0 ▶{
Subroutine managing_editor: not called CC=2 SCAR=17.9
228 0 my $self = shift;
229  
230 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
231 {
232 0 $self->{managing_editor} = shift;
233 }
234  
235 0 return $self->{managing_editor};
236 }
237  
238 sub lastBuildDate
239 0 ▶{
Subroutine lastBuildDate: not called CC=2 SCAR=17.9
240 0 my $self = shift;
241  
242 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
243 {
244 0 $self->{lastBuildDate} = shift;
245 }
246  
247 0 return $self->{lastBuildDate};
248 }
249  
250 sub pubDate
251 0 ▶{
Subroutine pubDate: not called CC=2 SCAR=17.9
252 0 my $self = shift;
253  
254 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
255 {
256 0 $self->{pubDate} = shift;
257 }
258  
259 0 return $self->{pubDate};
260 }
261  
262 sub rating
263 0 ▶{
Subroutine rating: not called CC=2 SCAR=17.9
264 0 my $self = shift;
265  
266 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
267 {
268 0 $self->{rating} = shift;
269 }
270  
271 0 return $self->{rating};
272 }
273  
274 sub title
275 0 ▶{
Subroutine title: not called CC=2 SCAR=17.9
276 0 my $self = shift;
277  
278 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
279 {
280 0 $self->{title} = shift;
281 }
282  
283 0 return $self->{title};
284 }
285  
286 sub ttl
287 0 ▶{
Subroutine ttl: not called CC=2 SCAR=17.9
288 0 my $self = shift;
289  
290 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
291 {
292 0 $self->{ttl} = shift;
293 }
294  
295 0 return $self->{ttl};
296 }
297  
298 sub webmaster
299 0 ▶{
Subroutine webmaster: not called CC=2 SCAR=17.9
300 0 my $self = shift;
301  
302 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
303 {
304 0 $self->{webmaster} = shift;
305 }
306  
307 0 return $self->{webmaster};
308 }
309 0 ▶use XML::RSS;
Subroutine BEGIN: called CC=1 SCAR=2.6
310  
311 sub input_items
312 {
313 my $self = shift;
314  
315 my $items = shift;
316  
317 return [ map { $self->input_single_item( $_, $items->[$_] ) }
318 ( 0 .. $#$items ) ];
319 }
320  
321 sub input_single_item
322 {
323 my $self = shift;
324 my ( $index, $inputted_item ) = (@_);
325  
326 return HTML::Latemp::News::Item->new( %$inputted_item, 'index' => $index, );
327 }
328  
329 sub initialize
330 {
331 my $self = shift;
332  
333 my %args = (@_);
334  
335 my $items = $args{'news_items'};
336  
337 $self->items( $self->input_items($items) );
338  
339 $self->title( $args{'title'} );
340 $self->link_( $args{'link'} );
341 $self->language( $args{'language'} );
342 $self->rating( $args{'rating'}
343 || '(PICS-1.1 "http://www.classify.org/safesurf/" 1 r (SS~~000 1))'
344 );
345 $self->copyright( $args{'copyright'} || "" );
346 $self->docs( $args{'docs'} || "http://blogs.law.harvard.edu/tech/rss" );
347 $self->ttl( $args{'ttl'} || "360" );
348 $self->generator( $args{'generator'} || "Perl and XML::RSS" );
349 $self->webmaster( $args{'webmaster'} );
350 $self->managing_editor( $args{'managing_editor'} || $self->webmaster() );
351 $self->description( $args{'description'} );
352  
353 if ( defined( my $date = delete $args{lastBuildDate} ) )
354 {
355 $self->lastBuildDate($date);
356 }
357 if ( defined( my $date = delete $args{pubDate} ) )
358 {
359 $self->pubDate($date);
360 }
361  
362 return 0;
363 }
364  
365  
366 sub add_item_to_rss_feed
367 {
368 my $self = shift;
369 my %args = (@_);
370  
371 my $item = $args{'item'};
372 my $rss_feed = $args{'feed'};
373  
374 my $item_url = $self->get_item_url($item);
375  
376 $rss_feed->add_item(
377 'title' => $item->title(),
378 'link' => $item_url,
379 'permaLink' => $item_url,
380 'enclosure' => { 'url' => $item_url, },
381 'description' => $item->description(),
382 'author' => $item->author(),
383 'pubDate' => $item->date(),
384 'category' => $item->category(),
385 );
386 }
387  
388 sub get_item_url
389 {
390 my $self = shift;
391 my $item = shift;
392 return $self->link_() . $self->get_item_rel_url($item);
393 }
394  
395 sub get_item_rel_url
396 {
397 my $self = shift;
398 my $item = shift;
399 return "news/" . $item->id() . "/";
400 }
401  
402 sub get_items_to_include
403 {
404 my $self = shift;
405 my $args = shift;
406  
407 my $num_items_to_include = $args->{'num_items'} || 10;
408  
409 my $items = $self->items();
410  
411 if ( @$items < $num_items_to_include )
412 {
413 $num_items_to_include = scalar(@$items);
414 }
415  
416 return [ @$items[ ( -$num_items_to_include ) .. (-1) ] ];
417 }
418  
419 sub generate_rss_feed
420 {
421 my $self = shift;
422  
423 my %args = (@_);
424  
425 my $rss_feed = XML::RSS->new( 'version' => "2.0" );
426 $rss_feed->channel(
427 'title' => $self->title(),
428 'link' => $self->link_(),
429 'language' => $self->language(),
430 'description' => $self->description(),
431 'rating' => $self->rating(),
432 'copyright' => $self->copyright(),
433 'pubDate' => ( $self->pubDate // ( scalar( localtime() ) ) ),
434 'lastBuildDate' =>
435 ( $self->lastBuildDate // ( scalar( localtime() ) ) ),
436 'docs' => $self->docs(),
437 'ttl' => $self->ttl(),
438 'generator' => $self->generator(),
439 'managingEditor' => $self->managing_editor(),
440 'webMaster' => $self->webmaster(),
441 );
442  
443 foreach my $single_item ( @{ $self->get_items_to_include( \%args ) } )
444 {
445 $self->add_item_to_rss_feed(
446 'item' => $single_item,
447 'feed' => $rss_feed,
448 );
449 }
450  
451 my $filename = $args{'output_filename'} || "rss.xml";
452  
453 $rss_feed->save($filename);
454 }
455  
456  
457 sub get_navmenu_items
458 {
459 my $self = shift;
460 my %args = (@_);
461  
462 my @ret;
463  
464 foreach my $single_item (
465 reverse( @{ $self->get_items_to_include( \%args ) } ) )
466 {
467 push @ret,
468 {
469 'text' => $single_item->title(),
470 'url' => $self->get_item_rel_url($single_item),
471 };
472 }
473 return \@ret;
474 }
475  
476  
477 sub format_news_page_item
478 {
479 my $self = shift;
480 my (%args) = (@_);
481  
482 my $item = $args{'item'};
483 my $base_url = $args{'base_url'};
484  
485 return
486 "<h3><a href=\"$base_url"
487 . $item->id() . "/\">"
488 . CGI::escapeHTML( $item->title() )
489 . "</a></h3>\n" . "<p>\n"
490 . $item->description()
491 . "\n</p>\n";
492 }
493  
494 sub get_news_page_entries
495 {
496 my $self = shift;
497 my %args = (@_);
498  
499 my $html = "";
500  
501 my $base_url = exists( $args{'base_url'} ) ? $args{'base_url'} : "";
502  
503 foreach my $single_item (
504 reverse( @{ $self->get_items_to_include( \%args ) } ) )
505 {
506 $html .= $self->format_news_page_item(
507 'item' => $single_item,
508 'base_url' => $base_url,
509 );
510 }
511 return $html;
512 }
513  
514  
515 sub get_news_box_contents
516 {
517 my $self = shift;
518 my (%args) = (@_);
519  
520 my $html = "";
521 foreach my $item ( reverse( @{ $self->get_items_to_include( \%args ) } ) )
522 {
523 $html .=
524 "<li><a href=\""
525 . $self->get_item_rel_url($item) . "\">"
526 . CGI::escapeHTML( $item->title() )
527 . "</a></li>\n";
528 }
529 return $html;
530 }
531  
532 sub get_news_box
533 {
534 my $self = shift;
535  
536 my $html = "";
537  
538 $html .= qq{<div class="news">\n};
539 $html .= qq{<h3>News</h3>\n};
540 $html .= qq{<ul>\n};
541 $html .= $self->get_news_box_contents(@_);
542 $html .= qq{<li><a href="./news/">More&hellip;</a></li>};
543 $html .= qq{</ul>\n};
544 $html .= qq{</div>\n};
545 return $html;
546 }
547  
548  
549 1;
550  
551 __END__
↑ Summary