File Coverage

lib/Templer/Plugin/RSS.pm
Criterion Covered Total %
statement 17 36 47.2
branch 1 8 12.5
condition 1 3 33.3
subroutine 5 5 100.0
pod 2 2 100.0
total 26 54 48.1


line stmt bran cond sub pod time code
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            

This is my page content.

16            
17            
18            
  • 19            
    20            
    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 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
    67              
    68             =cut
    69              
    70             =head1 COPYRIGHT AND LICENSE
    71              
    72             Copyright (C) 2015 Steve Kemp .
    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     12   10527 use strict;
      11         19  
      11         277  
    85 11     11   137 use warnings;
      11         18  
      11         3891  
    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             {
    99 11     11 1 24 my ( $proto, %supplied ) = (@_);
    100 11   33     59 my $class = ref($proto) || $proto;
    101              
    102 11         21 my $self = {};
    103 11         20 bless( $self, $class );
    104 11         94 return $self;
    105             }
    106              
    107              
    108              
    109             =head2 expand_variables
    110              
    111             This is the method which is called by the L
    112             to expand the variables contained in a L 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             {
    122 9     9 1 31 my ( $self, $site, $page, $data ) = (@_);
    123              
    124              
    125             #
    126             # Get the page-variables in the template.
    127             #
    128 9         45 my %hash = %$data;
    129              
    130              
    131             #
    132             # Load XML::Feed if we can
    133             #
    134 9         55 my $module = "use XML::Feed";
    135              
    136             ## no critic (Eval)
    137 8     8   1273 eval($module);
      0         0  
      0         0  
      9         792  
    138             ## use critic
    139              
    140             #
    141             # If there were errors loading the module then we're done.
    142             #
    143 9 50       82 return ( \%hash ) if ($@);
    144              
    145             #
    146             # Look for a value of "read_file" in each key.
    147             #
    148 0           foreach my $key ( keys %hash )
    149             {
    150 0 0         if ( $hash{ $key } =~ /^rss\(\s?([0-9]+)\s?,\s?(https?:\/\/.*)\s?\)/ )
    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 0         if ($feed)
    165             {
    166 0           my $tmp;
    167              
    168             # loop over entries.
    169 0           for my $entry ( $feed->entries )
    170             {
    171 0 0         if ( $count > 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             Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::RSS");