HiD/Generator/AtomFeed.pm

filter: Statementstmt 32.3% 22 / 68 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 60.0% 6 / 10 Podpod 100% 1 / 1 totaltotal 26.8% 29 / 108 CC 14 SCAR 34.3
CC
14
Cov
24%
generate
72.0
_new_feed
20.0
_upgrade_atom_config
12.0
CRAP 30.9
« HiD/Server.pm ↑ Summary HiD/Generator/ArchivePage.pm »
1 # ABSTRACT: Atom feed generator
2  
3  
4 package HiD::Generator::AtomFeed;
5 1 our $AUTHORITY = 'cpan:GENEHACK';
6 1 $HiD::Generator::AtomFeed::VERSION = '1.96';
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 with 'HiD::Generator';
9  
10 1 ▶use 5.014; # strict, unicode_strings
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use XML::Feed::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use HiD::VirtualPage;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 sub generate {
20 0 ▶ my( $self , $site ) = @_;
Subroutine generate: not called CC=8 SCAR=42.8
Pod documented
21  
22 0 ▶ if ( exists $site->config->{atom_feed} && ref $site->config->{atom_feed} ne 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $site->config->{"atom_feed"} and ref $site->config->{"atom_feed"} ne "HASH") 0 0 0
Condition0%0/3
exists $site->config->{"atom_feed"} and ref $site->config->{"atom_feed"} ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $site->config->{"atom_feed"} and ref $site->config->{"atom_feed"} ne "HASH"
exists $site->config->{"atom_feed"} ref $site->config->{"atom_feed"} ne "HASH"
Truth table0%0/3
A and B
A= exists $site->config->{"atom_feed"}B= ref $site->config->{"atom_feed"} ne "HASH"
ABresult
0X0
100
111
23 0 $site->WARN( "Using deprecated atom_feed config! Please see docs on how to update!" );
24  
25 0 $site->config->{atom_feed} = _upgrade_atom_config( $site->config );
26 }
27  
28 0 ▶ return unless $site->config->{atom_feed}{generate};
Branch: 1 branch
BranchTrueFalseTotal
unless $site->config->{"atom_feed"}{"generate"} 0 0 0
29  
30 0 my $destination = $site->config->{atom_feed}{destination};
31 0 ▶ my $post_limit = $site->config->{atom_feed}{posts} // 20;
Condition0%0/2
$site->config->{"atom_feed"}{"posts"} // 20
l!l
00
MC/DC0%0/1
$site->config->{"atom_feed"}{"posts"} // 20
$site->config->{"atom_feed"}{"posts"}
Truth table0%0/2
A
A= $site->config->{"atom_feed"}{"posts"}
Aresult
11
00
32 0 my $post_count = 1;
33  
34 0 my $feed = $self->_new_feed($site);
35  
36 0 POST: for my $post( @{ $site->posts }) {
37 0 $feed->add_entry($self->_new_entry($post));
38  
39 0 $post_count++;
40 0 ▶ last POST if $post_count > $post_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $post_count > $post_limit 0 0 0
41 }
42  
43 # XML::Feed (I suspect, really XML::Atom) plays silly buggers with the UTF-8
44 # flag...
45 0 my $xml_feed = $feed->as_xml;
46 0 ▶ utf8::decode($xml_feed) # in place
Branch: 1 branch
BranchTrueFalseTotal
utf8::decode($xml_feed) or die "Unable to properly decode feed" 0 0 0
47 or die "Unable to properly decode feed";
48  
49 0 my $feed_page = HiD::VirtualPage->new({
50 output_filename => $site->destination . $destination ,
51 content => $xml_feed ,
52 });
53  
54 0 $site->add_input( "ATOM FEED" => 'page' );
55 0 $site->add_object( $feed_page );
56  
57 0 $site->INFO( "* Injected Atom feed");
58 }
59  
60 sub _new_entry {
61 0 ▶ my( $self , $post ) = @_;
Subroutine _new_entry: not called CC=2 SCAR=17.9
62  
63 0 my $entry = XML::Feed::Entry->new('Atom');
64 0 $entry->title($post->title);
65 0 $entry->author($post->author);
66 0 $entry->link($post->url);
67 0 $entry->id($post->baseurl . $post->url);
68 0 $entry->modified($post->date);
69 0 ▶ $entry->summary($post->description) if $post->description;
Branch: 1 branch
BranchTrueFalseTotal
if $post->description 0 0 0
70 0 $entry->content($post->converted_content);
71  
72 0 return $entry;
73 }
74  
75 sub _new_feed {
76 0 ▶ my( $self , $site ) = @_;
Subroutine _new_feed: not called CC=4 SCAR=30.0
77  
78 0 my $feed = XML::Feed->new('Atom');
79 0 ▶ my $title = $site->config->{atom_feed}{title} // $site->config->{title};
Condition0%0/3
$site->config->{"atom_feed"}{"title"} // $site->config->{"title"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$site->config->{"atom_feed"}{"title"} // $site->config->{"title"}
$site->config->{"atom_feed"}{"title"} $site->config->{"title"}
Truth table0%0/3
A // B
A= $site->config->{"atom_feed"}{"title"}B= $site->config->{"title"}
ABresult
1X1
011
000
80 0 $feed->title( $title );
81  
82 $feed->base( $site->config->{atom_feed}{base} )
83 0 ▶ if $site->config->{atom_feed}{base};
Branch: 1 branch
BranchTrueFalseTotal
if $site->config->{"atom_feed"}{"base"} 0 0 0
84  
85 0 ▶ if( $site->config->{atom_feed}{link}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site->config->{"atom_feed"}{"link"}) 0 0 0
86 0 $feed->link( $site->config->{atom_feed}{link} );
87 0 $feed->id( $site->config->{atom_feed}{link} );
88 }
89  
90 0 $feed->modified(DateTime->now());
91  
92 0 return $feed;
93 }
94  
95  
96 sub _upgrade_atom_config {
97 0 ▶ my( $old_config ) = @_;
Subroutine _upgrade_atom_config: not called CC=3 SCAR=24.8
98  
99 my $new_config = {
100 generate => 1 ,
101 destination => $old_config->{atom_feed}
102 0 };
103  
104 0 foreach (qw/ base link posts title /) {
105 0 ▶ if ( exists $old_config->{"atom_feed_$_"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$old_config{"atom_feed_$_"}) 0 0 0
106 0 $new_config->{$_} = $old_config->{"atom_feed_$_"}
107 };
108 }
109  
110 0 return $new_config;
111 }
112  
113 1 __PACKAGE__->meta->make_immutable;
114 1 1;
115  
116 __END__
117  
118 =pod
119  
120 =encoding UTF-8
121  
122 =head1 NAME
123  
124 HiD::Generator::AtomFeed - Atom feed generator
125  
126 =head1 DESCRIPTION
127  
128 This Generator produces an Atom feed of your posts.
129  
130 Enable it by setting the 'atom_feed.generate' key in your config to 1. Also
131 set an 'atom_feed.destination' key indicating where the feed should be
132 generated.
133  
134 You may also set optional 'atom_feed.base' and 'atom_feed.link' keys to set
135 the 'rel=alternate' base link and the 'rel=self' link to the atom feed,
136 respectively.
137  
138 The 'atom.title' config key can be used to give the feed a title. Otherwise
139 the site title will be used.
140  
141 The 'atom.posts' config key can be used to control the number of posts in the
142 feed. It defaults to 20.
143  
144 =head2 DEPRECATED CONFIG
145  
146 This module formerly used different config keys. If this older configuration
147 is detected, a warning will be emitted and the configuration will be
148 internally upgraded to the new format. If you wish to silence this warning,
149 convert your config to use these new keys:
150  
151 OLD NEW
152 atom_feed ----------> atom_feed.generate
153 *AND* atom_feed.destination
154 atom_feed_base ----> atom_feed.base
155 atom_feed_link ----> atom_feed.link
156 atom_feed_posts ----> atom_feed.posts
157 atom_feed_title ----> atom_feed.title
158  
159 See the documentation above to understand what each key controls.
160  
161 =head1 METHODS
162  
163 =head2 generate
164  
165 =head1 VERSION
166  
167 version 1.96
168  
169 =head1 AUTHOR
170  
171 John SJ Anderson <genehack@genehack.org>
172  
173 =head1 COPYRIGHT AND LICENSE
174  
175 This software is copyright (c) 2015 by John SJ Anderson.
176  
177 This is free software; you can redistribute it and/or modify it under
178 the same terms as the Perl 5 programming language system itself.
179  
180 =cut
« HiD/Server.pm ↑ Summary HiD/Generator/ArchivePage.pm »