lib/MojoMojo/Formatter/RSS.pm

filter: Statementstmt 54.2% 19 / 35 Branchbran 16.6% 2 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 5 / 6 Podpod 100% 4 / 4 totaltotal 42.8% 30 / 70 CC 12 SCAR 41.8
CC
12
Cov
38%
include_rss
110.0
format_content
3.6
BEGIN
1.0
CRAP 65.6
« lib/MojoMojo/Controller/PageAdmin.pm ↑ Summary lib/MojoMojo/Schema/Result/Content.pm »
1 package MojoMojo::Formatter::RSS;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use parent 'MojoMojo::Formatter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 26 eval {require LWP::Simple; require URI::Fetch; require XML::Feed};
7 26 my $dependencies_installed = !$@;
8  
9 =head2 module_loaded
10  
11 Return true if the module is loaded.
12  
13 =cut
14  
15 125 ▶sub module_loaded { $dependencies_installed }
Subroutine module_loaded: called CC=1 SCAR=0.0
Pod documented
16  
17 26 our $VERSION = '0.01';
18  
19 =head1 NAME
20  
21 MojoMojo::Formatter::RSS - Include RSS feeds on your page.
22  
23 =head1 DESCRIPTION
24  
25 This formatter takes a feed in the format {{feed <url>}}, and
26 passes it through L<XML::Feed> to get a formatted feed suitable
27 for inclusion in your page. It also caches them in the chosen
28 Catalyst cache. By default it will render the first element in
29 the feed, but it can take a numeric parameter to choose number of
30 elements.
31  
32 =head1 METHODS
33  
34 =head2 format_content_order
35  
36 Format order can be 1-99. The RSS formatter runs on 6, after the
37 L<Include|MojoMojo::Formatter::Include>), so that transcluding
38 a page from the wiki that brings in a feed, will display the feed
39 in the transcluded section as well.
40  
41 =cut
42  
43  
44 620 ▶sub format_content_order { 6 }
Subroutine format_content_order: called CC=1 SCAR=0.0
Pod documented
45  
46 =head2 format_content
47  
48 Calls the formatter. Takes a ref to the content as well as the
49 context object.
50  
51 =cut
52  
53 124 ▶sub format_content {
Subroutine format_content: called CC=3 SCAR=12.7
Pod documented
54 124 my ( $class, $content, $c ) = @_;
55 124 ▶ return unless $class->module_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->module_loaded 0 124 124
56 # Regexp::Common::URI is overkill
57 124 my $re = $class->gen_re(qr(
58 feed \s+ (\S+) # feed URL
59 (?: \s+ (\d+))? # optional maximum number of entries
60 )x);
61 0 ▶ if ( $$content =~ s/$re/$class->include_rss( $c, $1, $2 )/eg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$content =~ s/$re/$class->include_rss($c, $1, $2);/eg) 0 124 124
62 # we don't want to precompile a page with comments so turn it off
63 0 $c->stash->{precompile_off} = 1;
64 }
65 }
66  
67 =head2 include_rss <c> <url> [<entries>]
68  
69 Returns HTML-formatted feed content for inclusion, up to a specified
70 number of entries. Will store a cached version in C<< $c->cache >>.
71  
72 =cut
73  
74 0 ▶sub include_rss {
Subroutine include_rss: not called CC=10 SCAR=47.0
Pod documented
75 0 my ($class, $c, $url, $entries) = @_;
76 0 ▶ $entries ||= 1;
Condition0%0/2
$entries ||= 1
l!l
00
MC/DC0%0/1
$entries ||= 1
$entries
Truth table0%0/2
A
A= $entries
Aresult
11
00
77 0 $url = URI->new($url);
78 0 ▶ return $c->loc('x is not a valid URL', $url) unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
79
80 0 my $result = URI::Fetch->fetch( $url, Cache => $c->cache );
81 0 ▶ return $c->loc('Could not retrieve x', $url)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 0 0
82 if not defined $result;
83 0 ▶ my $feed = XML::Feed->parse(\$result->content) or
Branch: 1 branch
BranchTrueFalseTotal
my $feed = "XML::Feed"->parse(\$result->content) or return $c->loc("Could not parse feed x", $url) 0 0 0
84 return $c->loc('Could not parse feed x', $url);
85
86 0 my $count = 0;
87 0 my $content = '';
88 0 foreach my $entry ($feed->entries) {
89 0 $count++;
90 0 ▶ $content .= '<div class="feed">'
Condition0%0/7
$entry->title || 'no title'
l!l
00
$entry->content->body || $entry->summary->body || ''
l!l
00
$entry->content->body or $entry->summary->body
l!l&&r!l&&!r
000
MC/DC0%0/1
$entry->title || 'no title'
$entry->title
MC/DC0%0/2
$entry->content->body || $entry->summary->body || ''
$entry->content->body $entry->summary->body
Truth table0%0/2
A
A= $entry->title
Aresult
11
00
Truth table0%0/3
A or B
A= $entry->content->bodyB= $entry->summary->body
ABresult
1X1
011
000
91 . '<h3><a href="'.$entry->link.'">'
92 . ($entry->title||"no title").'</a></h3>'
93 . ($entry->content->body||$entry->summary->body||"")."</div>\n";
94 0 ▶ return $content if $count==$entries;
Branch: 1 branch
BranchTrueFalseTotal
if $count == $entries 0 0 0
95 }
96 0 return $content;
97 }
98  
99 =head1 SEE ALSO
100  
101 L<MojoMojo>, L<Module::Pluggable::Ordered>, L<XML::Feed>, L<URI::Fetch>
102  
103 =head1 AUTHORS
104  
105 Marcus Ramberg <mramberg@cpan.org>
106  
107 =head1 LICENSE
108  
109 This library is free software. You can redistribute it and/or modify
110 it under the same terms as Perl itself.
111  
112 =cut
113  
114 26 1;
« lib/MojoMojo/Controller/PageAdmin.pm ↑ Summary lib/MojoMojo/Schema/Result/Content.pm »