| 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.8'; | |||||||||||||||||||||||||||
| 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 | { | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $param_func = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_grep_func: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ( defined $grep_func and ref $grep_func eq "CODE" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $grep_func and ref $grep_func eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 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 | { | ||||||||||||||||||||||||||||
| 43 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fmt_handler_capture: called CC=4 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | if ( defined $grep_func and ref $grep_func eq "CODE" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $grep_func and ref $grep_func eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 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 | { | ||||||||||||||||||||||||||||
| 60 | 2 | ▶ | my @ret_records = @data_records; # save data for return | ||||||||||||||||||||||||||
|
Subroutine
data_records: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||
| 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.8 | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||