File Coverage

blib/lib/Dancer/Plugin/Feed.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             #
2             # This file is part of Dancer-Plugin-Feed
3             #
4             # This software is copyright (c) 2013 by Natal Ngétal.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package Dancer::Plugin::Feed;
10             {
11             $Dancer::Plugin::Feed::VERSION = '1.131470';
12             }
13              
14 2     2   809784 use Dancer ':syntax';
  2         4953217  
  2         13  
15 2     2   2909 use Dancer::Plugin;
  2         3973  
  2         157  
16 2     2   12 use Dancer::Exception qw(:all);
  2         9  
  2         204  
17 2     2   307619 use XML::Feed;
  0            
  0            
18              
19             #ABSTRACT: Easy to generate feed rss or atom for Dancer applications.
20              
21             my $ct = {
22             atom => 'application/atom+xml',
23             rss => 'application/rss+xml',
24             };
25              
26             #Register exception
27             register_exception('FeedInvalidFormat',
28             message_pattern => "Unknown format use rss or atom: %s"
29             );
30             register_exception('FeedNoFormat',
31             message_pattern => "Format is missing"
32             );
33              
34             my @feed_properties =
35             qw/format title base link tagline description author id language copyright self_link modified/;
36              
37             my @entries_properties =
38             qw/title base link content summary category tags author id issued modified enclosure/;
39              
40             register create_feed => sub {
41             my ($dsl, %params) = plugin_args(@_);
42              
43             my $format = _validate_format(\%params);
44              
45             if ($format =~ /^atom$/i) {
46             _create_atom_feed(\%params);
47             }
48             elsif($format =~/^rss$/i) {
49             _create_rss_feed(\%params);
50             }
51             else{
52             raise FeedInvalidFormat => $format;
53             }
54             };
55              
56             register create_atom_feed => sub {
57             my ($dsl, %params) = plugin_args(@_);
58              
59             _create_atom_feed(\%params);
60             };
61              
62             register create_rss_feed => sub {
63             my ($dsl, %params) = plugin_args(@_);
64              
65             _create_rss_feed(\%params);
66             };
67              
68             sub _validate_format {
69             my $params = shift;
70             my $format = delete $params->{format};
71              
72             if (!$format) {
73             my $settings = plugin_setting;
74             $format = $settings->{format} or raise 'FeedNoFormat';
75             }
76              
77             if ($format !~ /^(?:atom|rss)$/i) {
78             raise FeedInvalidFormat => $format;
79             }
80              
81             return $format;
82             }
83              
84             sub _create_feed {
85             my ($format, $params) = @_;
86              
87             my $entries = delete $params->{entries};
88             my $feed = XML::Feed->new($format);
89             my $settings = plugin_setting;
90              
91             map {
92             my $val = $params->{$_} || $settings->{$_};
93             $feed->$_($val) if ($val);
94             } @feed_properties;
95              
96             foreach my $entry (@$entries) {
97             my $e = XML::Feed::Entry->new($format);
98              
99             map {
100             my $val = $entry->{$_};
101             $e->$_($val) if $val
102             } @entries_properties;
103              
104             $feed->add_entry($e);
105             }
106              
107             return $feed->as_xml;
108             }
109              
110             sub _create_atom_feed {
111             my $params = shift;
112              
113             content_type($ct->{atom});
114             _create_feed('Atom', $params);
115             }
116              
117             sub _create_rss_feed {
118             my $params = shift;
119              
120             content_type($ct->{rss});
121             _create_feed('RSS', $params);
122             }
123              
124             register_plugin for_versions => [1];
125              
126             1;
127              
128             __END__