line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Dancer2-Plugin-Feed |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2016 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 Dancer2::Plugin::Feed; |
10
|
|
|
|
|
|
|
$Dancer2::Plugin::Feed::VERSION = '1.160190'; |
11
|
1
|
|
|
1
|
|
646764
|
use Dancer2::Plugin; |
|
1
|
|
|
|
|
2388
|
|
|
1
|
|
|
|
|
6
|
|
12
|
1
|
|
|
1
|
|
1974
|
use XML::Feed; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
282445
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#ABSTRACT: Easy to generate feed rss or atom for Dancer2 applications. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $ct = { |
17
|
|
|
|
|
|
|
atom => 'application/atom+xml', |
18
|
|
|
|
|
|
|
rss => 'application/rss+xml', |
19
|
|
|
|
|
|
|
}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my @feed_properties = |
22
|
|
|
|
|
|
|
qw/format title base link tagline description author id language copyright self_link modified/; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my @entries_properties = |
25
|
|
|
|
|
|
|
qw/title base link content summary category tags author id issued modified enclosure/; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
register create_feed => sub { |
28
|
|
|
|
|
|
|
my ($dsl, %params) = @_; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $format = _validate_format(\%params); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
if (lc $format eq 'atom') { |
33
|
|
|
|
|
|
|
_create_atom_feed($dsl, \%params); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
elsif(lc $format eq 'rss') { |
36
|
|
|
|
|
|
|
_create_rss_feed($dsl, \%params); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
else { |
39
|
|
|
|
|
|
|
die "Unknown format $format, use rss or atom\n"; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
register create_atom_feed => sub { |
44
|
|
|
|
|
|
|
my ($dsl, %params) = plugin_args(@_); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
_create_atom_feed($dsl, \%params); |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
register create_rss_feed => sub { |
50
|
|
|
|
|
|
|
my ($dsl, %params) = plugin_args(@_); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
_create_rss_feed($dsl, \%params); |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _validate_format { |
56
|
0
|
|
|
0
|
|
|
my( $params, $dsl ) = @_; |
57
|
0
|
|
|
|
|
|
my $format = delete $params->{format}; |
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
if (!$format) { |
60
|
0
|
|
|
|
|
|
my $settings = plugin_setting; |
61
|
|
|
|
|
|
|
$format = $settings->{format} |
62
|
0
|
0
|
|
|
|
|
or die "Feed format is missing\n"; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
|
if (! exists $ct->{$format}) { |
66
|
0
|
|
|
|
|
|
die "Unknown format $format, use rss or atom\n"; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
return $format; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _create_feed { |
73
|
|
|
|
|
|
|
my ($format, $params) = @_; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $entries = delete $params->{entries}; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $feed = XML::Feed->new($format); |
78
|
|
|
|
|
|
|
my $settings = plugin_setting; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
foreach (@feed_properites) { |
81
|
|
|
|
|
|
|
my $val = $params->{$_} || $settings->{$_}; |
82
|
|
|
|
|
|
|
$feed->$_($val) if ($val); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
foreach my $entry (@$entries) { |
86
|
|
|
|
|
|
|
my $e = XML::Feed::Entry->new($format); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
foreach (@entries_properties) { |
89
|
|
|
|
|
|
|
my $val = $entry->{$_}; |
90
|
|
|
|
|
|
|
$e->$_($val) if $val |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
$feed->add_entry($e); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
return $feed->as_xml; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _create_atom_feed { |
100
|
|
|
|
|
|
|
my ($dsl, $params) = @_; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$dsl->content_type($ct->{atom}); |
103
|
|
|
|
|
|
|
_create_feed('Atom', $params); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _create_rss_feed { |
107
|
|
|
|
|
|
|
my ($dsl, $params) = @_; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
$dsl->content_type($ct->{rss}); |
110
|
|
|
|
|
|
|
_create_feed('RSS', $params); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
register_plugin for_versions => [2]; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |