FeedDeduplicator/Publisher.pm

filter: Statementstmt 86.6% 26 / 30 Branchbran 50.0% 2 / 4 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 46.0% 35 / 76 CC 13 SCAR 42.0
CC
13
Cov
47%
publish
91.7
BEGIN
1.0
BEGIN
1.0
CRAP 66.5
↑ Summary FeedDeduplicator/Deduplicator.pm »
1 =head1 NAME
2  
3 App::FeedDeduplicator::Publisher - Publisher class for App::FeedDeduplicator
4  
5 =head1 DESCRIPTION
6  
7 This module is part of the App::FeedDeduplicator application. It is
8 responsible for publishing the deduplicated entries to a specified format
9 (Atom or JSON).
10  
11 =head1 SYNOPSIS
12  
13 use App::FeedDeduplicator::Publisher;
14  
15 my $publisher = App::FeedDeduplicator::Publisher->new(
16 entries => $deduplicated_entries,
17 format => 'Atom',
18 max_entries => 10,
19 );
20  
21 $publisher->publish();
22  
23 =head1 METHODS
24  
25 =head2 new
26  
27 Creates a new instance of App::FeedDeduplicator::Publisher. The constructor
28 accepts an arrau of entries, a format (Atom or JSON), and a maximum number of
29 entries as parameters.
30  
31 The entries should be an array reference containing hash references with the
32 keys 'entry' and 'feed'.
33  
34 The 'entry' key should contain an XML::Feed::Entry object, and the 'feed' key
35 should contain a hash reference with the feed information.
36  
37 The format should be either 'Atom' or 'JSON', and the maximum number of
38 entries specifies how many entries to include in the output.
39  
40 =head2 publish
41  
42 The main method that publishes the deduplicated entries to the specified
43 format (Atom or JSON).
44  
45 It sorts the entries based on their issued, modified, updated, or date
46 attributes and limits the output to the specified maximum number of entries.
47  
48 It generates the output in the specified format and prints it to STDOUT.
49  
50 =cut
51  
52 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 class App::FeedDeduplicator::Publisher {
57 1 ▶ use XML::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶ use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 1 field $entries :param;
61 1 field $format :param //= 'Atom';
62 1 field $max_entries :param //= 10;
63  
64 1 ▶ method publish {
Subroutine publish: called CC=13 SCAR=45.2
Pod documented
65 my @sorted = sort {
66 1 ($b->{entry}->issued || $b->{entry}->modified || $b->{entry}->updated || $b->{entry}->date || 0)
67 <=>
68 0 ▶ ($a->{entry}->issued || $a->{entry}->modified || $a->{entry}->updated || $a->{entry}->date || 0)
Condition0%0/22
$App::FeedDeduplicator::Publisher::b->{'entry'}->issued || $App::FeedDeduplicator::Publisher::b->{'entry'}->modified || $App::FeedDeduplicator::Publisher::b->{'entry'}->updated || $App::FeedDeduplicator::Publisher::b->{'entry'}->date || 0
l!l
00
$App::FeedDeduplicator::Publisher::b->{"entry"}->issued or $App::FeedDeduplicator::Publisher::b->{"entry"}->modified or $App::FeedDeduplicator::Publisher::b->{"entry"}->updated or $App::FeedDeduplicator::Publisher::b->{"entry"}->date
l!l&&r!l&&!r
000
$App::FeedDeduplicator::Publisher::b->{"entry"}->issued or $App::FeedDeduplicator::Publisher::b->{"entry"}->modified or $App::FeedDeduplicator::Publisher::b->{"entry"}->updated
l!l&&r!l&&!r
000
$App::FeedDeduplicator::Publisher::b->{"entry"}->issued or $App::FeedDeduplicator::Publisher::b->{"entry"}->modified
l!l&&r!l&&!r
000
$App::FeedDeduplicator::Publisher::a->{'entry'}->issued || $App::FeedDeduplicator::Publisher::a->{'entry'}->modified || $App::FeedDeduplicator::Publisher::a->{'entry'}->updated || $App::FeedDeduplicator::Publisher::a->{'entry'}->date || 0
l!l
00
$App::FeedDeduplicator::Publisher::a->{"entry"}->issued or $App::FeedDeduplicator::Publisher::a->{"entry"}->modified or $App::FeedDeduplicator::Publisher::a->{"entry"}->updated or $App::FeedDeduplicator::Publisher::a->{"entry"}->date
l!l&&r!l&&!r
000
$App::FeedDeduplicator::Publisher::a->{"entry"}->issued or $App::FeedDeduplicator::Publisher::a->{"entry"}->modified or $App::FeedDeduplicator::Publisher::a->{"entry"}->updated
l!l&&r!l&&!r
000
$App::FeedDeduplicator::Publisher::a->{"entry"}->issued or $App::FeedDeduplicator::Publisher::a->{"entry"}->modified
l!l&&r!l&&!r
000
MC/DC0%0/4
$App::FeedDeduplicator::Publisher::b->{'entry'}->issued || $App::FeedDeduplicator::Publisher::b->{'entry'}->modified || $App::FeedDeduplicator::Publisher::b->{'entry'}->updated || $App::FeedDeduplicator::Publisher::b->{'entry'}->date || 0
$App::FeedDeduplicator::Publisher::b->{"entry"}->issued $App::FeedDeduplicator::Publisher::b->{"entry"}->modified $App::FeedDeduplicator::Publisher::b->{"entry"}->updated $App::FeedDeduplicator::Publisher::b->{"entry"}->date
MC/DC0%0/4
$App::FeedDeduplicator::Publisher::a->{'entry'}->issued || $App::FeedDeduplicator::Publisher::a->{'entry'}->modified || $App::FeedDeduplicator::Publisher::a->{'entry'}->updated || $App::FeedDeduplicator::Publisher::a->{'entry'}->date || 0
$App::FeedDeduplicator::Publisher::a->{"entry"}->issued $App::FeedDeduplicator::Publisher::a->{"entry"}->modified $App::FeedDeduplicator::Publisher::a->{"entry"}->updated $App::FeedDeduplicator::Publisher::a->{"entry"}->date
Truth table0%0/5
A or B or C or D
A= $App::FeedDeduplicator::Publisher::b->{"entry"}->issuedB= $App::FeedDeduplicator::Publisher::b->{"entry"}->modifiedC= $App::FeedDeduplicator::Publisher::b->{"entry"}->updatedD= $App::FeedDeduplicator::Publisher::b->{"entry"}->date
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table0%0/5
A or B or C or D
A= $App::FeedDeduplicator::Publisher::a->{"entry"}->issuedB= $App::FeedDeduplicator::Publisher::a->{"entry"}->modifiedC= $App::FeedDeduplicator::Publisher::a->{"entry"}->updatedD= $App::FeedDeduplicator::Publisher::a->{"entry"}->date
ABCDresult
1XXX1
01XX1
001X1
00011
00000
69 } @$entries;
70  
71 1 ▶ if (@sorted > $max_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted > $max_entries) 0 1 1
72 0 $#sorted = $max_entries - 1;
73 }
74  
75 1 ▶ if ($format eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'json') { } 0 1 1
76 0 say encode_json([ map { {
77 title => $_->{entry}->title,
78 link => $_->{entry}->link,
79 summary => $_->{entry}->summary->body,
80 issued => $_->{entry}->issued && $_->{entry}->issued->iso8601,
81 source_name => $_->{feed}{name},
82 source_url => $_->{feed}{web},
83 0 ▶ } } @sorted ]);
Condition0%0/3
$_->{'entry'}->issued && $_->{'entry'}->issued->iso8601
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'entry'}->issued && $_->{'entry'}->issued->iso8601
$_->{'entry'}->issued $_->{'entry'}->issued->iso8601
Truth table0%0/3
A && B
A= $_->{'entry'}->issuedB= $_->{'entry'}->issued->iso8601
ABresult
0X0
100
111
84 } else {
85 1 my $feed = XML::Feed->new($format);
86 1 $feed->title("Deduplicated Feed");
87 1 $feed->add_entry($_) for @sorted;
88 1 say $feed->as_xml;
89 }
90 }
91 }
92  
93 =head1 AUTHOR
94  
95 Dave Cross <dave@perlhacks.com>
96  
97 =head1 COPYRIGHT AND LICENSE
98  
99 This software is copyright (c) 2025 Magnum Solutions Ltd.
100 All rights reserved.
101  
102 This program is free software; you can redistribute it and/or modify it under
103 the same terms as Perl itself.
104  
105 See L<http://dev.perl.org/licenses/artistic.html> for more details.
106  
107 =cut
108  
109 1 1;
↑ Summary FeedDeduplicator/Deduplicator.pm »