| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | 1 | my $settings = plugin_setting; | |||||||||||||||||||||||||||
| 66 | $format = $settings->{format} | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | or die "Feed format is missing\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | ▶ | if ( !exists $ct->{$format} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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->{$_}
MC/DC50%1/2
$params->{$_} || $settings->{$_}
$params->{$_}
$settings->{$_}
Truth table66%2/3
A || B
A= $params->{$_}B= $settings->{$_}
| |||||||||||||||||||||||||||||
| 87 | 24 | ▶ | $feed->$_($val) if ($val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$_ eq "category" or $_ eq "tags"
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"
| |||||||||||||||||||||||||||||
| 96 | 36 | ▶ | if ( $val && !ref $val ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$val and not ref $val
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
| |||||||||||||||||||||||||||||
| 97 | 2 | $val = [$val]; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 36 | ▶ | if ( ref $val eq 'ARRAY' && @{$val} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $val eq "ARRAY" and @{$val;}
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;}
| |||||||||||||||||||||||||||||
| 100 | 14 | foreach my $val_item ( @{$val} ) { | |||||||||||||||||||||||||||
| 101 | 30 | ▶ | $e->$_($val_item) if $val_item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||
| 106 | 180 | ▶ | $e->$_($val) if $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||