WebFetch/Output/Capture.pm

filter: Statementstmt 78.9% 30 / 38 Branchbran 33.3% 2 / 6 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 7 / 8 Podpod 100% 3 / 3 totaltotal 66.1% 43 / 65 CC 6 SCAR 15.6
CC
6
Cov
66%
set_grep_func
12.0
fmt_handler_capture
4.9
BEGIN
1.0
CRAP 4.8
« WebFetch/Data/Store.pm ↑ Summary WebFetch/Data/Config.pm »
1 # WebFetch::Output::Capture
2 # ABSTRACT: capture WebFetch data without generating an output file
3 # This is mainly expected to be used for testing. But it can collect data retreived by any WebFetch front-end.
4 #
5 # Copyright (c) 2022 Ian Kluft. This program is free software; you can
6 # redistribute it and/or modify it under the terms of the GNU General Public
7 # License Version 3. See https://www.gnu.org/licenses/gpl-3.0-standalone.html
8  
9 # pragmas to silence some warnings from Perl::Critic
10 ## no critic (Modules::RequireExplicitPackage)
11 # This solves a catch-22 where parts of Perl::Critic want both package and use-strict to be first
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 ## use critic (Modules::RequireExplicitPackage)
16  
17 package WebFetch::Output::Capture;
18 1 $WebFetch::Output::Capture::VERSION = '0.15.9';
19 1 ▶use base "WebFetch";
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # register capabilities with WebFetch
23 1 __PACKAGE__->module_register("output:capture");
24 1 my @data_records;
25 my $grep_func;
26  
27 # set grep function to use when selecting news items
28 sub set_grep_func
29 0 ▶{
Subroutine set_grep_func: not called CC=3 SCAR=24.8
Pod documented
30 0 my $param_func = shift;
31 0 ▶ if ( defined $grep_func and ref $grep_func eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $grep_func and ref $grep_func eq 'CODE') { } 0 0 0
Condition0%0/3
defined $grep_func and ref $grep_func eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $grep_func and ref $grep_func eq "CODE"
defined $grep_func ref $grep_func eq "CODE"
Truth table0%0/3
A and B
A= defined $grep_funcB= ref $grep_func eq "CODE"
ABresult
0X0
100
111
32 0 $grep_func = $param_func;
33 } else {
34 0 __PACKAGE__::throw_param_error("set_grep_func received param which is not a CODE ref");
35 }
36 0 return;
37 }
38  
39 # "capture" format handler
40 # capture function stashes all the received data records from SiteNews for inspection
41 sub fmt_handler_capture
42 2 ▶{
Subroutine fmt_handler_capture: called CC=4 SCAR=16.0
Pod documented
43 2 my ($self) = @_;
44  
45 2 WebFetch::debug "fetch: " . Dumper( $self->{data} );
46 2 $self->no_savables_ok(); # rather than let WebFetch save the data, we'll take it here
47 2 ▶ if ( exists $self->{data}{records} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"data"}{"records"}) 2 0 2
48 2 ▶ if ( defined $grep_func and ref $grep_func eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $grep_func and ref $grep_func eq 'CODE') { } 0 2 2
Condition33%1/3
defined $grep_func and ref $grep_func eq "CODE"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $grep_func and ref $grep_func eq "CODE"
defined $grep_func ref $grep_func eq "CODE"
Truth table33%1/3
A and B
A= defined $grep_funcB= ref $grep_func eq "CODE"
ABresult
0X0
100
111
49 0 push @data_records, grep { $grep_func->($_) } @{ $self->{data}{records} };
50 } else {
51 2 push @data_records, @{ $self->{data}{records} };
52 }
53 }
54 2 return 1;
55 }
56  
57 # return the file list
58 sub data_records
59 2 ▶{
Subroutine data_records: called CC=1 SCAR=0.0
Pod documented
60 2 my @ret_records = @data_records; # save data for return
61 2 @data_records = (); # clear saved records list because tests use this more than once
62 2 return @ret_records;
63 }
64  
65 1 1;
66  
67 __END__
« WebFetch/Data/Store.pm ↑ Summary WebFetch/Data/Config.pm »