WebFetch/Data/Store.pm

filter: Statementstmt 58.0% 65 / 112 Branchbran 16.6% 4 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 56.0% 14 / 25 Podpod 100% 17 / 17 totaltotal 56.1% 100 / 178 CC 14 SCAR 17.4
CC
14
Cov
51%
next_record
20.0
wk2fname
12.0
wk2fnum
12.0
CRAP 5.7
« WebFetch/Input/SiteNews.pm ↑ Summary WebFetch/Output/Capture.pm »
1 # WebFetch::Data::Store
2 # ABSTRACT: WebFetch Embedding API top-level data store
3 #
4 # Copyright (c) 2009-2022 Ian Kluft. This program is free software; you can
5 # redistribute it and/or modify it under the terms of the GNU General Public
6 # License Version 3. See https://www.gnu.org/licenses/gpl-3.0-standalone.html
7 #
8 # The WebFetch Embedding API manages the following data:
9 # * {data} - top level hash container (WebFetch::Data::Store)
10 # * {fields} - array of field names
11 # * {records} - array of data records (WebFetch::Data::Record)
12 # * each record is an array of data fields in the order of the field names
13 # * {wk_names} - hash of WebFetch well-known fields to actual field names
14 # * {feed} - top-level arbitrary info about the feed
15 #
16  
17 # pragmas to silence some warnings from Perl::Critic
18 ## no critic (Modules::RequireExplicitPackage)
19 # This solves a catch-22 where parts of Perl::Critic want both package and use-strict to be first
20 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 ## use critic (Modules::RequireExplicitPackage)
24  
25 package WebFetch::Data::Store;
26 3 $WebFetch::Data::Store::VERSION = '0.15.9';
27 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use WebFetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use base qw( WebFetch );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # define exceptions/errors
33 3 ▶use Exception::Class ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # no user-servicable parts beyond this point
36  
37 # instantiate new object
38 sub new
39 4 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
40 4 my ( $class, @params ) = @_;
41 4 my $self = {};
42 4 bless $self, $class;
43 4 $self->init(@params);
44 4 return $self;
45 }
46  
47 # initialization
48 sub init
49 4 ▶{
Subroutine init: called CC=1 SCAR=0.0
Pod documented
50 4 my ( $self, @params ) = @_;
51 4 $self->{fields} = [];
52 4 $self->{findex} = {};
53 4 $self->{records} = [];
54 4 $self->{wk_names} = {};
55 4 $self->{wkindex} = {};
56 4 $self->{feed} = {};
57  
58 # signal WebFetch that Data subclasses do not provide a fetch function
59 4 $self->{no_fetch} = 1;
60 4 $self->SUPER::init(@params);
61  
62 4 return $self;
63 }
64  
65 # add field names
66 sub add_fields
67 2 ▶{
Subroutine add_fields: called CC=2 SCAR=6.9
Pod documented
68 2 my ( $self, @fields ) = @_;
69 2 foreach my $field (@fields) {
70 18 $self->{findex}{$field} = scalar @{ $self->{fields} };
71 18 push @{ $self->{fields} }, $field;
72 }
73 2 return;
74 }
75  
76 # get number of fields
77 sub num_fields
78 0 ▶{
Subroutine num_fields: not called CC=1 SCAR=6.9
Pod documented
79 0 my $self = shift;
80 0 return scalar @{ $self->{fields} };
81 }
82  
83 # get field names
84 sub get_fields
85 0 ▶{
Subroutine get_fields: not called CC=1 SCAR=6.9
Pod documented
86 0 my $self = shift;
87 0 return keys %{ $self->{fields} };
88 }
89  
90 # get field name by number
91 sub field_bynum
92 0 ▶{
Subroutine field_bynum: not called CC=1 SCAR=6.9
Pod documented
93 0 my $self = shift;
94 0 my $num = shift;
95 0 return $self->{fields}[$num];
96 }
97  
98 # add well-known names
99 sub add_wk_names
100 2 ▶{
Subroutine add_wk_names: called CC=2 SCAR=6.9
Pod documented
101 2 my $self = shift;
102 2 my ( $wk_name, $field );
103  
104 2 ▶ while ( @_ >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 10 2 12
105 10 $wk_name = shift;
106 10 $field = shift;
107 10 WebFetch::debug "add_wk_names $wk_name => $field";
108 10 $self->{wk_names}{$wk_name} = $field;
109 10 $self->{wkindex}{$wk_name} = $self->{findex}{$field};
110 }
111 2 return;
112 }
113  
114 # get feed info
115 sub get_feed
116 0 ▶{
Subroutine get_feed: not called CC=2 SCAR=17.9
Pod documented
117 0 my $self = shift;
118 0 my $name = shift;
119 0 ▶ return ( exists $self->{$name} ) ? $self->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$name} ? : 0 0 0
120 }
121  
122 # set feed info
123 sub set_feed
124 0 ▶{
Subroutine set_feed: not called CC=2 SCAR=17.9
Pod documented
125 0 my $self = shift;
126 0 my $name = shift;
127 0 my $value = shift;
128 0 ▶ my $retval = ( exists $self->{$name} ) ? $self->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$name} ? : 0 0 0
129 0 $self->{$name} = $value;
130 0 return $retval;
131 }
132  
133 # add a data record
134 # this adds the field values in the same order the field names were added
135 sub add_record
136 33 ▶{
Subroutine add_record: called CC=1 SCAR=0.0
Pod documented
137 33 my ( $self, @args ) = @_;
138 33 push @{ $self->{records} }, [@args];
139 33 return;
140 }
141  
142 # TODO: add a function add_record_unordered( name => value, ... )
143 # less efficient, but may be OK for cases where that doesn't matter
144  
145 # get the number of data records
146 sub num_records
147 0 ▶{
Subroutine num_records: not called CC=1 SCAR=6.9
Pod documented
148 0 my $self = shift;
149 0 return scalar @{ $self->{records} };
150 }
151  
152 # get a data record by index
153 sub get_record
154 0 ▶{
Subroutine get_record: not called CC=1 SCAR=6.9
Pod documented
155 0 my $self = shift;
156 0 my $n = shift;
157 0 WebFetch::debug "get_record $n";
158 0 require WebFetch::Data::Record;
159 0 return WebFetch::Data::Record->new( $self, $n );
160 }
161  
162 # reset iterator position
163 sub reset_pos
164 0 ▶{
Subroutine reset_pos: not called CC=1 SCAR=6.9
Pod documented
165 0 my $self = shift;
166  
167 0 WebFetch::debug "reset_pos";
168 0 delete $self->{pos};
169 0 return;
170 }
171  
172 # get next record
173 sub next_record
174 0 ▶{
Subroutine next_record: not called CC=4 SCAR=30.0
Pod documented
175 0 my $self = shift;
176  
177 # initialize if necessary
178 0 ▶ if ( !exists $self->{pos} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"pos"}) 0 0 0
179 0 $self->{pos} = 0;
180 }
181 0 WebFetch::debug "next_record n=" . $self->{pos} . " of " . scalar @{ $self->{records} };
182  
183 # return undef if position is out of bounds
184 0 ▶ ( $self->{pos} < 0 ) and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"pos"} < 0 and return 0 0 0
185 0 ▶ ( $self->{pos} > scalar @{ $self->{records} } - 1 ) and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"pos"} > scalar @{$self->{"records"};} - 1 and return 0 0 0
186  
187 # get record
188 0 return $self->get_record( $self->{pos}++ );
189 }
190  
191 # convert well-known name to field name
192 sub wk2fname
193 0 ▶{
Subroutine wk2fname: not called CC=3 SCAR=24.8
Pod documented
194 0 my $self = shift;
195 0 my $wk = shift;
196  
197 0 ▶ WebFetch::debug "wk2fname $wk => " . ( ( exists $self->{wk_names}{$wk} ) ? $self->{wk_names}{$wk} : "undef" );
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'wk_names'}{$wk} ? : 0 0 0
198 return ( exists $self->{wk_names}{$wk} )
199 0 ▶ ? $self->{wk_names}{$wk}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'wk_names'}{$wk} ? : 0 0 0
200 : undef;
201 }
202  
203 # convert a field name to a field number
204 sub fname2fnum
205 258 ▶{
Subroutine fname2fnum: called CC=3 SCAR=11.4
Pod documented
206 258 my $self = shift;
207 258 my $fname = shift;
208  
209 WebFetch::debug "fname2fnum $fname => "
210 . (
211 ( exists $self->{findex}{$fname} )
212 258 ▶ ? $self->{findex}{$fname}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'findex'}{$fname} ? : 258 0 258
213 : "undef"
214 );
215 return ( exists $self->{findex}{$fname} )
216 258 ▶ ? $self->{findex}{$fname}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'findex'}{$fname} ? : 258 0 258
217 : undef;
218 }
219  
220 # convert well-known name to field number
221 sub wk2fnum
222 0 ▶{
Subroutine wk2fnum: not called CC=3 SCAR=24.8
Pod documented
223 0 my $self = shift;
224 0 my $wk = shift;
225  
226 0 ▶ WebFetch::debug "wk2fnum $wk => " . ( ( exists $self->{wkindex}{$wk} ) ? $self->{wkindex}{$wk} : "undef" );
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'wkindex'}{$wk} ? : 0 0 0
227 return ( exists $self->{wkindex}{$wk} )
228 0 ▶ ? $self->{wkindex}{$wk}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'wkindex'}{$wk} ? : 0 0 0
229 : undef;
230 }
231  
232 3 1;
233  
234 __END__
« WebFetch/Input/SiteNews.pm ↑ Summary WebFetch/Output/Capture.pm »