File Coverage

blib/lib/WebFetch/Output/Capture.pm
Criterion Covered Total %
statement 26 34 76.4
branch 2 6 33.3
condition 1 6 16.6
subroutine 7 8 87.5
pod 3 3 100.0
total 39 57 68.4


line stmt bran cond sub pod time code
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     1   495 use strict;
  1         2  
  1         30  
13 1     1   5 use warnings;
  1         3  
  1         22  
14 1     1   5 use utf8;
  1         3  
  1         4  
15             ## use critic (Modules::RequireExplicitPackage)
16              
17             package WebFetch::Output::Capture;
18             $WebFetch::Output::Capture::VERSION = '0.15.9';
19 1     1   48 use base "WebFetch";
  1         1  
  1         86  
20 1     1   6 use Data::Dumper;
  1         27  
  1         376  
21              
22             # register capabilities with WebFetch
23             __PACKAGE__->module_register("output:capture");
24             my @data_records;
25             my $grep_func;
26              
27             # set grep function to use when selecting news items
28             sub set_grep_func
29             {
30 0     0 1 0 my $param_func = shift;
31 0 0 0     0 if ( defined $grep_func and ref $grep_func eq "CODE" ) {
32 0         0 $grep_func = $param_func;
33             } else {
34 0         0 __PACKAGE__::throw_param_error("set_grep_func received param which is not a CODE ref");
35             }
36 0         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             {
43 2     2 1 5 my ($self) = @_;
44              
45 2         13 WebFetch::debug "fetch: " . Dumper( $self->{data} );
46 2         17 $self->no_savables_ok(); # rather than let WebFetch save the data, we'll take it here
47 2 50       8 if ( exists $self->{data}{records} ) {
48 2 50 33     7 if ( defined $grep_func and ref $grep_func eq "CODE" ) {
49 0         0 push @data_records, grep { $grep_func->($_) } @{ $self->{data}{records} };
  0         0  
  0         0  
50             } else {
51 2         6 push @data_records, @{ $self->{data}{records} };
  2         10  
52             }
53             }
54 2         7 return 1;
55             }
56              
57             # return the file list
58             sub data_records
59             {
60 2     2 1 56 my @ret_records = @data_records; # save data for return
61 2         6 @data_records = (); # clear saved records list because tests use this more than once
62 2         9 return @ret_records;
63             }
64              
65             1;
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             WebFetch::Output::Capture - capture WebFetch data without generating an output file
76              
77             =head1 VERSION
78              
79             version 0.15.9
80              
81             =head1 SYNOPSIS
82              
83             In perl scripts:
84              
85             use WebFetch::RSS; # or another WebFetch input - this example shows RSS
86             use WebFetch::Output::Capture;
87             # ... fill in $params hashref
88             my %Options = (
89             dir => $params->{temp_dir},
90             source_format => "rss",
91             source => "file://".$input_dir."/".$params->{in},
92             dest_format => "capture",
93             dest => "", # unused
94             );
95             WebFetch::RSS->run(\%Options);
96             my @data_records = WebFetch::Output::Capture::data_records();
97              
98             =head1 DESCRIPTION
99              
100             This is a WebFetch output module which captures WebFetch output as a data structure
101             rather than formatting it and saving it in a file.
102             The data can be collected from any WebFetch input module.
103              
104             This module is used for testing WebFetch.
105              
106             =head1 FUNCTIONS
107              
108             =over 4
109              
110             =item $obj->set_grep_func( $func_ref )
111              
112             This function receives a CODE reference and saves it for use in a grep of the WebFetch data records.
113             It is optional. If not set, the data_records() method will return all the data retreived by WebFetch.
114              
115             =back
116              
117             =over 4
118              
119             =item $obj->fmt_handler_capture()
120              
121             This function is called by WebFetch when the capture destination is selected.
122             It saves the output in a structure. There is no output to any file when using this format.
123              
124             =back
125              
126             =over 4
127              
128             =item WebFetch::Output::Capture::data_records()
129              
130             returns a list of the data records retrived by WebFetch.
131             The structure of each data record varies depending what input format was selected when WebFetch was run.
132             Usually another output module should be used to output this data to a file in a specific data format.
133             When using the capture method, you receive the raw data records.
134              
135             =back
136              
137             =head1 SEE ALSO
138              
139             L<WebFetch>
140             L<https://github.com/ikluft/WebFetch>
141              
142             =head1 BUGS AND LIMITATIONS
143              
144             Please report bugs via GitHub at L<https://github.com/ikluft/WebFetch/issues>
145              
146             Patches and enhancements may be submitted via a pull request at L<https://github.com/ikluft/WebFetch/pulls>
147              
148             =head1 AUTHOR
149              
150             Ian Kluft <https://github.com/ikluft>
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             This software is Copyright (c) 1998-2023 by Ian Kluft.
155              
156             This is free software, licensed under:
157              
158             The GNU General Public License, Version 3, June 2007
159              
160             =cut