lib/Templer/Plugin/RSS.pm

filter: Statementstmt 48.6% 18 / 37 Branchbran 12.5% 1 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 47.3% 27 / 57 CC 8 SCAR 28.5
CC
8
Cov
42%
expand_variables
31.0
new
2.1
BEGIN
1.1
CRAP 17.3
« lib/Templer/Plugin/Hash.pm ↑ Summary lib/Templer/Plugin/FileGlob.pm »
1  
2 =head1 NAME
3  
4 Templer::Plugin::RSS - A plugin to include RSS feeds in pages.
5  
6 =cut
7  
8 =head1 SYNOPSIS
9  
10 The following is a good example use of this plugin
11  
12 title: About my site
13 feed: rss(4, http://blog.steve.org.uk/index.rss )
14 ----
15 <p>This is my page content.</p>
16 <ul>
17 <!-- tmpl_loop name='feed' -->
18 <li><a href="<!-- tmpl_var name='link' -->"><!-- tmpl_var name='title' --></a></li>
19 <!-- /tmpl_loop -->
20 </ul>
21  
22  
23 Here the variable 'feed' will contain the first four elements of the
24 RSS feed my blog produces.
25  
26 The feed entries will contain the following three attributes
27  
28 =over 8
29  
30 =item author
31 The author of the post.
32  
33 =item link
34 The link to the post.
35  
36 =item title
37 The title of the popst
38  
39 =back
40  
41 =cut
42  
43 =head1 DESCRIPTION
44  
45 This plugin uses L<XML::Feed> to extract remote RSS feeds and allow
46 them to be included in your site bodies - if that module is not
47 available then the plugin will disable itself.
48  
49 =cut
50  
51 =head1 LICENSE
52  
53 This module is free software; you can redistribute it and/or modify it
54 under the terms of either:
55  
56 a) the GNU General Public License as published by the Free Software
57 Foundation; either version 2, or (at your option) any later version,
58 or
59  
60 b) the Perl "Artistic License".
61  
62 =cut
63  
64 =head1 AUTHOR
65  
66 Steve Kemp <steve@steve.org.uk>
67  
68 =cut
69  
70 =head1 COPYRIGHT AND LICENSE
71  
72 Copyright (C) 2015 Steve Kemp <steve@steve.org.uk>.
73  
74 This library is free software. You can modify and or distribute it under
75 the same terms as Perl itself.
76  
77 =cut
78  
79 =head1 METHODS
80  
81 =cut
82  
83  
84 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87  
88 package Templer::Plugin::RSS;
89  
90  
91 =head2 new
92  
93 Constructor. No arguments are required/supported.
94  
95 =cut
96  
97 sub new
98 11 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
99 11 my ( $proto, %supplied ) = (@_);
100 11 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
101  
102 11 my $self = {};
103 11 bless( $self, $class );
104 11 return $self;
105 }
106  
107  
108  
109 =head2 expand_variables
110  
111 This is the method which is called by the L<Templer::Plugin::Factory>
112 to expand the variables contained in a L<Templer::Site::Page> object.
113  
114 This method will expand any variable that has a defintion of the
115 form "rss( NN, http... )" and replace the variable definition
116 with the result of fetching that RSS feed.
117  
118 =cut
119  
120 sub expand_variables
121 9 ▶{
Subroutine expand_variables: called CC=7 SCAR=34.3
Pod documented
122 9 my ( $self, $site, $page, $data ) = (@_);
123  
124  
125 #
126 # Get the page-variables in the template.
127 #
128 9 my %hash = %$data;
129  
130  
131 #
132 # Load XML::Feed if we can
133 #
134 9 my $module = "use XML::Feed";
135  
136 ## no critic (Eval)
137 0 ▶ eval($module);
Subroutine BEGIN: called CC=1 SCAR=1.2
138 ## use critic
139  
140 #
141 # If there were errors loading the module then we're done.
142 #
143 9 ▶ return ( \%hash ) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 9 0 9
144  
145 #
146 # Look for a value of "read_file" in each key.
147 #
148 0 foreach my $key ( keys %hash )
149 {
150 0 ▶ if ( $hash{ $key } =~ /^rss\(\s?([0-9]+)\s?,\s?(https?:\/\/.*)\s?\)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{$key} =~ m[^rss\(\s?([0-9]+)\s?,\s?(https?://.*)\s?\)]) 0 0 0
151 {
152 0 my $count = $1;
153 0 my $link = $2;
154  
155 0 $link =~ s/^\s+|\s+$//g;
156 0 $count =~ s/^\s+|\s+$//g;
157  
158 # remove the variable.
159 0 delete( $hash{ $key } );
160  
161 # try to parse the feed.
162 0 my $feed = XML::Feed->parse( URI->new($link) );
163  
164 0 ▶ if ($feed)
Branch: 1 branch
BranchTrueFalseTotal
if ($feed) { } 0 0 0
165 {
166 0 my $tmp;
167  
168 # loop over entries.
169 0 for my $entry ( $feed->entries )
170 {
171 0 ▶ if ( $count > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 0 0 0
172 {
173 0 push( @$tmp,
174 { link => $entry->link,
175 author => $entry->author,
176 title => $entry->title
177 } );
178 }
179 0 $count = $count - 1;
180 }
181  
182 # store the expanded feed.
183 0 $hash{ $key } = $tmp;
184 }
185 else
186 {
187 0 print "WARNING: Failed to fetch $link\n";
188 }
189 }
190 }
191  
192 0 return ( \%hash );
193 }
194  
195  
196 #
197 # Register the plugin.
198 #
199 11 Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::RSS");
« lib/Templer/Plugin/Hash.pm ↑ Summary lib/Templer/Plugin/FileGlob.pm »