blib/lib/Dancer2/Plugin/Feed.pm

filter: Statementstmt 95.6% 66 / 69 Branchbran 90.9% 20 / 22 Conditioncond 91.6% 11 / 12 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 93.5% 115 / 123 CC 20 SCAR 21.3
CC
20
Cov
94%
_create_feed
15.0
_validate_format
4.0
__ANON__
3.1
CRAP 8.4
↑ Summary
1 #
2 # This file is part of Dancer2-Plugin-Feed
3 #
4 # This software is copyright (c) 2026 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 1 $Dancer2::Plugin::Feed::VERSION = '1.263070';
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use XML::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #ABSTRACT: Easy to generate feed (RSS or atom) for Dancer2 applications.
20  
21 1 my $ct = {
22 atom => 'application/atom+xml',
23 rss => 'application/rss+xml',
24 };
25  
26 1 my @feed_properties =
27 qw/format title base link tagline description author id language copyright self_link modified/;
28  
29 1 my @entries_properties =
30 qw/title base link content summary category tags author id issued modified enclosure/;
31  
32 4 ▶register create_feed => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
33 4 my ( $dsl, %params ) = @_;
34  
35 4 my $format = _validate_format( \%params );
36  
37 2 ▶ if ( lc $format eq 'atom' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $format eq 'atom') { } 1 1 2
elsif (lc $format eq 'rss') { } 1 0 1
38 1 _create_atom_feed( $dsl, \%params );
39 }
40 elsif ( lc $format eq 'rss' ) {
41 1 _create_rss_feed( $dsl, \%params );
42 }
43 else {
44 0 die "Unknown format $format, use rss or atom\n";
45 }
46 1 };
47  
48 1 ▶register create_atom_feed => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 my ( $dsl, %params ) = @_;
50  
51 1 _create_atom_feed( $dsl, \%params );
52 1 };
53  
54 0 ▶register create_rss_feed => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 my ( $dsl, %params ) = @_;
56  
57 0 _create_rss_feed( $dsl, \%params );
58 1 };
59  
60 4 ▶sub _validate_format {
Subroutine _validate_format: called CC=4 SCAR=13.9
61 4 my ( $params, $dsl ) = @_;
62 4 my $format = delete $params->{format};
63  
64 4 ▶ if ( !$format ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format) 1 3 4
65 1 my $settings = plugin_setting;
66 $format = $settings->{format}
67 1 ▶ or die "Feed format is missing\n";
Branch: 1 branch
BranchTrueFalseTotal
$format = $settings->{"format"} or die "Feed format is missing\n" 0 1 1
68 }
69  
70 3 ▶ if ( !exists $ct->{$format} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ct->{$format}) 1 2 3
71 1 die "Unknown format $format, use rss or atom\n";
72 }
73  
74 2 return $format;
75 }
76  
77 3 ▶sub _create_feed {
Subroutine _create_feed: called CC=15 SCAR=27.1
78 3 my ( $format, $params ) = @_;
79  
80 3 my $entries = delete $params->{entries};
81  
82 3 my $feed = XML::Feed->new($format);
83 2 my $settings = plugin_setting;
84  
85 2 foreach (@feed_properties) {
86 24 ▶ my $val = $params->{$_} || $settings->{$_};
Condition66%2/3
$params->{$_} || $settings->{$_}
l!l&&r!l&&!r
2022
MC/DC50%1/2
$params->{$_} || $settings->{$_}
$params->{$_} $settings->{$_}
Truth table66%2/3
A || B
A= $params->{$_}B= $settings->{$_}
ABresult
1X1
011
000
87 24 ▶ $feed->$_($val) if ($val);
Branch: 1 branch
BranchTrueFalseTotal
if $val 2 22 24
88 }
89  
90 2 foreach my $entry (@$entries) {
91 18 my $e = XML::Feed::Entry->new($format);
92  
93 18 foreach (@entries_properties) {
94 216 my $val = $entry->{$_};
95 216 ▶ if ( $_ eq 'category' || $_ eq 'tags' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'category' or $_ eq 'tags') { } 36 180 216
Condition100%3/3
$_ eq "category" or $_ eq "tags"
l!l&&r!l&&!r
1818180
MC/DC100%2/2
$_ eq "category" or $_ eq "tags"
$_ eq "category" $_ eq "tags"
Truth table100%3/3
A or B
A= $_ eq "category"B= $_ eq "tags"
ABresult
1X1
011
000
96 36 ▶ if ( $val && !ref $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val and not ref $val) 2 34 36
Condition100%3/3
$val and not ref $val
!ll&&!rl&&r
20142
MC/DC100%2/2
$val and not ref $val
$val not ref $val
Truth table100%3/3
A and not(B)
A= $valB= not ref $val
ABresult
0X0
100
111
97 2 $val = [$val];
98 }
99 36 ▶ if ( ref $val eq 'ARRAY' && @{$val} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY" and @{$val;}) 14 22 36
Condition100%3/3
ref $val eq "ARRAY" and @{$val;}
!ll&&!rl&&r
20214
MC/DC100%2/2
ref $val eq "ARRAY" and @{$val;}
ref $val eq "ARRAY" @{$val;}
Truth table100%3/3
A and B
A= ref $val eq "ARRAY"B= @{$val;}
ABresult
0X0
100
111
100 14 foreach my $val_item ( @{$val} ) {
101 30 ▶ $e->$_($val_item) if $val_item;
Branch: 1 branch
BranchTrueFalseTotal
if $val_item 26 4 30
102 }
103 }
104 }
105 else {
106 180 ▶ $e->$_($val) if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 18 162 180
107 }
108 }
109  
110 18 $feed->add_entry($e);
111 }
112  
113 2 return $feed->as_xml;
114 }
115  
116 2 ▶sub _create_atom_feed {
Subroutine _create_atom_feed: called CC=1 SCAR=0.0
117 2 my ( $dsl, $params ) = @_;
118  
119 2 $dsl->content_type( $ct->{atom} );
120 2 _create_feed( 'Atom', $params );
121 }
122  
123 1 ▶sub _create_rss_feed {
Subroutine _create_rss_feed: called CC=1 SCAR=0.0
124 1 my ( $dsl, $params ) = @_;
125  
126 1 $dsl->content_type( $ct->{rss} );
127 1 _create_feed( 'RSS', $params );
128 }
129  
130 1 register_plugin for_versions => [2];
131  
132 1 1;
133  
134 __END__
↑ Summary