Noss/FeedConfig.pm

filter: Statementstmt 93.0% 186 / 200 Branchbran 72.2% 39 / 54 Conditioncond 50.0% 10 / 20 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 96.3% 26 / 27 Podpod 94.4% 17 / 18 totaltotal 84.5% 279 / 330 CC 38 SCAR 22.4
CC
38
Cov
86%
initialize
22.1
retry
5.9
set_groups
4.6
CRAP 9.4
« Noss/OPML.pm ↑ Summary Noss/Timestamp.pm »
1 package WWW::Noss::FeedConfig;
2 6 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 our $VERSION = '2.03';
6  
7 6 ▶use parent 'WWW::Noss::BaseConfig';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use List::Util qw(any min);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub new {
12  
13 17 ▶ my ($class, %param) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
14  
15 17 my $self = bless {}, $class;
16  
17 17 $self->initialize(%param);
18  
19 17 return $self;
20  
21 }
22  
23 sub initialize {
24  
25 17 ▶ my ($self, %param) = @_;
Subroutine initialize: called CC=22 SCAR=31.0
Pod undocumented
26  
27 17 $self->SUPER::initialize;
28 17 $self->set_name($param{ name });
29 17 $self->set_feed($param{ feed });
30 17 $self->set_path($param{ path });
31 17 $self->set_etag($param{ etag });
32 17 $self->set_retry_cache($param{ retry_cache });
33  
34 17 my $default = $param{ default };
35  
36 # Apply default parameters
37 17 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 1 16 17
38 1 $self->set_limit($default->limit);
39 1 $self->set_respect_skip($default->respect_skip);
40 1 $self->set_include_title($default->include_title);
41 1 $self->set_exclude_title($default->exclude_title);
42 1 $self->set_include_content($default->include_content);
43 1 $self->set_exclude_content($default->exclude_content);
44 1 $self->set_include_tags($default->include_tags);
45 1 $self->set_exclude_tags($default->exclude_tags);
46 1 $self->set_autoread($default->autoread);
47 1 $self->set_default_update($default->default_update);
48 1 $self->set_hidden($default->hidden);
49 }
50  
51 17 ▶ $self->set_groups($param{ groups } // []);
Condition100%2/2
$param{"groups"} // []
l!l
116
MC/DC100%1/1
$param{"groups"} // []
$param{"groups"}
Truth table100%2/2
A
A= $param{"groups"}
Aresult
11
00
52  
53 17 ▶ if (@{ $self->groups }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->groups;}) 1 16 17
54  
55 # Set lowest limit defined by groups if present
56 1 my $limit = min grep { defined } map { $_->limit } @{ $self->groups };
57 1 ▶ $self->set_limit($limit) if defined $limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit 1 0 1
58  
59 # If any group respects skip, we respect skip
60 1 ▶ my $rs = any { $_->respect_skip } @{ $self->groups };
Subroutine __ANON__: called CC=1 SCAR=0.0
61 1 $self->set_respect_skip($rs);
62  
63 # Overlay group filters
64 1 push @{ $self->include_title },
65 3 ▶ map { @{ $_->include_title // [] } }
Condition50%1/2
$_->include_title // []
l!l
30
MC/DC0%0/1
$_->include_title // []
$_->include_title
Truth table50%1/2
A
A= $_->include_title
Aresult
11
00
66 1 @{ $self->groups };
67 1 push @{ $self->exclude_title },
68 3 ▶ map { @{ $_->exclude_title // [] } }
Condition50%1/2
$_->exclude_title // []
l!l
30
MC/DC0%0/1
$_->exclude_title // []
$_->exclude_title
Truth table50%1/2
A
A= $_->exclude_title
Aresult
11
00
69 1 @{ $self->groups };
70 1 push @{ $self->include_content },
71 3 ▶ map { @{ $_->include_content // [] } }
Condition50%1/2
$_->include_content // []
l!l
30
MC/DC0%0/1
$_->include_content // []
$_->include_content
Truth table50%1/2
A
A= $_->include_content
Aresult
11
00
72 1 @{ $self->groups };
73 1 push @{ $self->exclude_content },
74 3 ▶ map { @{ $_->exclude_content // [] } }
Condition50%1/2
$_->exclude_content // []
l!l
30
MC/DC0%0/1
$_->exclude_content // []
$_->exclude_content
Truth table50%1/2
A
A= $_->exclude_content
Aresult
11
00
75 1 @{ $self->groups };
76 1 push @{ $self->include_tags },
77 3 ▶ map { @{ $_->include_tags // [] } }
Condition50%1/2
$_->include_tags // []
l!l
30
MC/DC0%0/1
$_->include_tags // []
$_->include_tags
Truth table50%1/2
A
A= $_->include_tags
Aresult
11
00
78 1 @{ $self->groups };
79 1 push @{ $self->exclude_tags },
80 3 ▶ map { @{ $_->exclude_tags // [] } }
Condition50%1/2
$_->exclude_tags // []
l!l
30
MC/DC0%0/1
$_->exclude_tags // []
$_->exclude_tags
Truth table50%1/2
A
A= $_->exclude_tags
Aresult
11
00
81 1 @{ $self->groups };
82  
83 # If any group wants autoread, we'll take autoread
84 1 ▶ my $ar = any { $_->autoread } @{ $self->groups };
Subroutine __ANON__: called CC=1 SCAR=0.0
85 1 $self->set_autoread($ar);
86  
87 # If any group does not want default_updates, we'll take no default
88 # updates
89 1 ▶ my $ndu = any { !$_->default_update } @{ $self->groups };
Subroutine __ANON__: called CC=1 SCAR=0.0
90 1 $self->set_default_update(!$ndu);
91  
92 # If any groups wants hidden, take hidden
93 1 ▶ my $hid = any { $_->hidden } @{ $self->groups };
Subroutine __ANON__: called CC=1 SCAR=0.0
94 1 $self->set_hidden($hid);
95  
96 }
97  
98 17 ▶ if (defined $param{ limit }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"limit"}) 2 15 17
99 2 $self->set_limit($param{ limit });
100 }
101 17 ▶ if (defined $param{ respect_skip }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"respect_skip"}) 0 17 17
102 0 $self->set_respect_skip($param{ respect_skip });
103 }
104 17 ▶ if (defined $param{ include_title }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"include_title"}) 3 14 17
105 3 push @{ $self->include_title }, @{ $param{ include_title } };
106 }
107 17 ▶ if (defined $param{ exclude_title }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"exclude_title"}) 3 14 17
108 3 push @{ $self->exclude_title }, @{ $param{ exclude_title } };
109 }
110 17 ▶ if (defined $param{ include_content }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"include_content"}) 3 14 17
111 3 push @{ $self->include_content }, @{ $param{ include_content } };
112 }
113 17 ▶ if (defined $param{ exclude_content }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"exclude_content"}) 3 14 17
114 3 push @{ $self->exclude_content }, @{ $param{ exclude_content } };
115 }
116 17 ▶ if (defined $param{ include_tags }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"include_tags"}) 3 14 17
117 3 push @{ $self->include_tags }, @{ $param{ include_tags } };
118 }
119 17 ▶ if (defined $param{ exclude_tags }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"exclude_tags"}) 3 14 17
120 3 push @{ $self->exclude_tags }, @{ $param{ exclude_tags } };
121 }
122 17 ▶ if (defined $param{ autoread }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"autoread"}) 1 16 17
123 1 $self->set_autoread($param{ autoread });
124 }
125 17 ▶ if (defined $param{ default_update }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"default_update"}) 1 16 17
126 1 $self->set_default_update($param{ default_update });
127 }
128 17 ▶ if (defined $param{ hidden }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"hidden"}) 1 16 17
129 1 $self->set_hidden($param{ hidden });
130 }
131  
132 17 return 1;
133  
134 }
135  
136 sub name {
137  
138 19 ▶ my ($self) = @_;
Subroutine name: called CC=1 SCAR=0.0
Pod documented
139  
140 19 return $self->{ Name };
141  
142 }
143  
144 sub set_name {
145  
146 17 ▶ my ($self, $name) = @_;
Subroutine set_name: called CC=3 SCAR=12.7
Pod documented
147  
148 17 ▶ unless (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 17 17
149 0 die "name cannot be undefined";
150 }
151  
152 # ':' feeds are reserved for internal use
153 17 ▶ unless ($name =~ /^\:?\w+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^\:?\w+$/u) 0 17 17
154 0 die "name can only contain alphanumeric and underscore characters";
155 }
156  
157 17 $self->{ Name } = $name;
158  
159 }
160  
161 sub feed {
162  
163 17 ▶ my ($self) = @_;
Subroutine feed: called CC=1 SCAR=0.0
Pod documented
164  
165 17 return $self->{ Feed };
166  
167 }
168  
169 sub set_feed {
170  
171 17 ▶ my ($self, $feed) = @_;
Subroutine set_feed: called CC=2 SCAR=7.6
Pod documented
172  
173 17 ▶ unless (defined $feed) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $feed) 0 17 17
174 0 die "feed cannot be undefined";
175 }
176  
177 17 $self->{ Feed } = $feed;
178  
179 }
180  
181 sub groups {
182  
183 32 ▶ my ($self) = @_;
Subroutine groups: called CC=1 SCAR=0.0
Pod documented
184  
185 32 return $self->{ Groups };
186  
187 }
188  
189 sub set_groups {
190  
191 17 ▶ my ($self, $new) = @_;
Subroutine set_groups: called CC=4 SCAR=15.2
Pod documented
192  
193 17 ▶ unless (ref $new eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $new eq "ARRAY") 0 17 17
194 0 die "groups must be an array ref";
195 }
196  
197 17 for my $i (0 .. $#$new) {
198 3 ▶ unless (eval { $new->[$i]->isa('WWW::Noss::GroupConfig') }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $new->[$i]->isa("WWW::Noss::GroupConfig") } }) 0 3 3
199 0 die "group[$i] is not a WWW::Noss::GroupConfig object";
200 }
201 }
202  
203 17 $self->{ Groups } = $new;
204  
205 }
206  
207 sub has_group {
208  
209 3 ▶ my ($self, $grp) = @_;
Subroutine has_group: called CC=1 SCAR=0.0
Pod documented
210  
211 3 return !! grep { $_->name eq $grp } @{ $self->groups };
212  
213 }
214  
215 sub path {
216  
217 17 ▶ my ($self) = @_;
Subroutine path: called CC=1 SCAR=0.0
Pod documented
218  
219 17 return $self->{ Path };
220  
221 }
222  
223 sub set_path {
224  
225 17 ▶ my ($self, $path) = @_;
Subroutine set_path: called CC=2 SCAR=7.6
Pod documented
226  
227 17 ▶ unless (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 17 17
228 0 die "path cannot be undefined";
229 }
230  
231 17 $self->{ Path } = $path;
232  
233 }
234  
235 sub etag {
236  
237 1 ▶ my ($self) = @_;
Subroutine etag: called CC=1 SCAR=0.0
Pod documented
238  
239 1 return $self->{ Etag };
240  
241 }
242  
243 sub set_etag {
244  
245 17 ▶ my ($self, $etag) = @_;
Subroutine set_etag: called CC=1 SCAR=0.0
Pod documented
246  
247 17 $self->{ Etag } = $etag;
248  
249 }
250  
251 sub retry_cache {
252  
253 0 ▶ my ($self) = @_;
Subroutine retry_cache: not called CC=1 SCAR=6.9
Pod documented
254  
255 0 return $self->{ RetryCache };
256  
257 }
258  
259 sub set_retry_cache {
260  
261 17 ▶ my ($self, $cache) = @_;
Subroutine set_retry_cache: called CC=1 SCAR=0.0
Pod documented
262  
263 17 $self->{ RetryCache } = $cache;
264  
265 }
266  
267 sub retry {
268  
269 2 ▶ my ($self) = @_;
Subroutine retry: called CC=5 SCAR=17.8
Pod documented
270  
271 2 ▶ if (not defined $self->{ RetryCache } or not -f $self->{ RetryCache }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"RetryCache"} and -f $self->{"RetryCache"}) 0 2 2
Condition33%1/3
defined $self->{"RetryCache"} and -f $self->{"RetryCache"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"RetryCache"} and -f $self->{"RetryCache"}
defined $self->{"RetryCache"} -f $self->{"RetryCache"}
Truth table33%1/3
A and B
A= defined $self->{"RetryCache"}B= -f $self->{"RetryCache"}
ABresult
0X0
100
111
272 0 return undef;
273 }
274  
275 open my $fh, '<', $self->{ RetryCache }
276 2 ▶ or die "Failed to open $self->{ RetryCache } for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"RetryCache"} or die qq[Failed to open $self->{"RetryCache"} for reading: $!] 0 2 2
277 2 my $retry = do { local $/; (readline $fh)[0] };
278 2 close $fh;
279 2 chomp $retry;
280  
281 2 ▶ if ($retry !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($retry =~ /^\d+$/u) 0 2 2
282 0 die "$self->{ RetryCache }: corrupted retry cache";
283 }
284  
285 2 return int $retry;
286  
287 }
288  
289 sub set_retry {
290  
291 1 ▶ my ($self, $retry) = @_;
Subroutine set_retry: called CC=4 SCAR=14.8
Pod documented
292  
293 1 ▶ if ($retry !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($retry =~ /^\d+$/u) 0 1 1
294 0 die 'retry time must be an integar';
295 }
296  
297 1 ▶ if (not defined $self->{ RetryCache }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"RetryCache"}) 0 1 1
298 0 die 'retry_cache must be set before setting retry';
299 }
300  
301 1 $retry = int $retry;
302  
303 open my $fh, '>', $self->{ RetryCache }
304 1 ▶ or die "Failed to open $self->{ RetryCache } for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->{"RetryCache"} or die qq[Failed to open $self->{"RetryCache"} for writing: $!] 0 1 1
305 1 print { $fh } $retry, "\n";
306 1 close $fh;
307  
308 1 return $retry;
309  
310 }
311  
312 sub can_we_retry {
313  
314 1 ▶ my ($self, $time) = @_;
Subroutine can_we_retry: called CC=3 SCAR=12.3
Pod documented
315 1 ▶ $time //= time;
Condition33%1/3
$time //= time
l!l&&r!l&&!r
010
MC/DC0%0/2
$time //= time
$time time
Truth table33%1/3
A //= B
A= $timeB= time
ABresult
1X1
011
000
316  
317 1 my $retry = $self->retry;
318 1 ▶ if (not defined $retry) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retry) 0 1 1
319 0 return 1;
320 }
321  
322 1 return $time >= $retry;
323  
324 }
325  
326 6 1;
327  
328 =head1 NAME
329  
330 WWW::Noss::FeedConfig - Class for storing feed configurations
331  
332 =head1 USAGE
333  
334 use WWW::Noss::FeedConfig;
335  
336 my $feed = WWW::Noss::FeedConfig->new(
337 name => 'feed',
338 feed => 'https://feed.xml',
339 path => 'feed.xml',
340 );
341  
342 =head1 DESCRIPTION
343  
344 B<WWW::Noss::FeedConfig> is a module that provides a class for storing L<noss>
345 feed configurations. This is a private module, please consult the L<noss>
346 manual for user documentation.
347  
348 =head1 METHODS
349  
350 Not all methods are documented here, as this class is derived from the
351 L<WWW::Noss::BaseConfig> module. Consult its documentation for additional
352 methods.
353  
354 =over 4
355  
356 =item $feed = WWW::Noss::FeedConfig->new(%param)
357  
358 Returns a blessed B<WWW::Noss::FeedConfig> object based on the parameters
359 provided in the C<%param> hash.
360  
361 The following are valid fields for the C<%param> hash. The C<name>, C<feed>,
362 and C<path> fields are the only required fields.
363  
364 =over 4
365  
366 =item name
367  
368 The name of feed. Can only only contain alphanumeric and underscore
369 characters.
370  
371 =item feed
372  
373 The feed URL.
374  
375 =item path
376  
377 Path to the location to store the feed.
378  
379 =item etag
380  
381 Path to store the feed's etag.
382  
383 =item groups
384  
385 Array ref of L<WWW::Noss::GroupConfig> groups that the feed is a part of.
386  
387 =item default
388  
389 L<WWW::Noss::GroupConfig> object representing the default feed group.
390  
391 =back
392  
393 The following fields from L<WWW::Noss::BaseConfig> are also available:
394  
395 =over 4
396  
397 =item limit
398  
399 =item respect_skip
400  
401 =item include_title
402  
403 =item exclude_title
404  
405 =item include_content
406  
407 =item exclude_content
408  
409 =item include_tags
410  
411 =item exclude_tags
412  
413 =item autoread
414  
415 =item default_update
416  
417 =item hidden
418  
419 =back
420  
421 =item $name = $feed->name()
422  
423 =item $feed->set_name($name)
424  
425 Getter/setter for the feed's name attribute.
426  
427 =item $url = $feed->feed()
428  
429 =item $feed->set_feed($url)
430  
431 Getter/setter for the feed's feed attribute.
432  
433 =item \@groups = $feed->groups()
434  
435 =item $feed->set_groups(\@groups)
436  
437 Getter/setter for the feed's groups attribute. Do note that modifying the
438 groups attribute does not affect the feed's configuration like it does during
439 initialization.
440  
441 =item $ok = $feed->has_group($group)
442  
443 Returns true if C<$feed> is a part of the group C<$group>.
444  
445 =item $path = $feed->path()
446  
447 =item $feed->set_path($path)
448  
449 Getter/setter for the feed's path attribute.
450  
451 =item $etag = $feed->etag()
452  
453 =item $feed->set_etag($etag)
454  
455 Getter/setter for the feed's etag attribute.
456  
457 =item $file = $feed->retry_cache()
458  
459 =item $feed->set_retry_cache($file)
460  
461 Getter/setter for the feed's retry cache file.
462  
463 =item $retry = $feed->retry()
464  
465 =item $feed->set_retry($retry)
466  
467 =item $ok = $feed->can_we_retry([ $time ])
468  
469 Getter/setter for the feed's retry time.
470  
471 =back
472  
473 =head1 AUTHOR
474  
475 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
476  
477 This project's source can be found on its
478 L<Codeberg page|https://codeberg.org/1-1sam/noss.git>. Comments and pull
479 requests are welcome!
480  
481 =head1 COPYRIGHT
482  
483 Copyright (C) 2025-2026 Samuel Young
484  
485 This program is free software: you can redistribute it and/or modify
486 it under the terms of the GNU General Public License as published by
487 the Free Software Foundation, either version 3 of the License, or
488 (at your option) any later version.
489  
490 =head1 SEE ALSO
491  
492 L<WWW::Noss::BaseConfig>, L<WWW::Noss::GroupConfig>, L<noss>
493  
494 =cut
495  
496 # vim: expandtab shiftwidth=4
« Noss/OPML.pm ↑ Summary Noss/Timestamp.pm »