lib/Web/DataService/Format.pm

filter: Statementstmt 54.2% 51 / 94 Branchbran 25.0% 16 / 64 Conditioncond 28.5% 16 / 56 MC/DCmcdc 6.0% 2 / 33 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 4 totaltotal 35.1% 91 / 259 CC 56 SCAR 57.2
CC
56
Cov
39%
document_formats
650.0
define_format
90.3
format_validator
2.0
CRAP 303.9
« lib/Web/DataService/Request.pm ↑ Summary blib/lib/Web/DataService/Execute.pm »
1 #
2 # Web::DataService::Format
3 #
4 # This module provides a role that is used by 'Web::DataService'. It implements
5 # routines for defining and documenting output formats.
6 #
7 # Author: Michael McClennen
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package Web::DataService::Format;
12  
13 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 our (%FORMAT_DEF) = (name => 'ignore',
20 suffix => 'single',
21 title => 'single',
22 content_type => 'single',
23 disposition => 'single',
24 uses_header => 'single',
25 is_text => 'single',
26 encode_as_text => 'single',
27 default_vocab => 'single',
28 doc_node => 'single',
29 module => 'single',
30 package => 'single',
31 doc_string => 'single',
32 undocumented => 'single',
33 disabled => 'single');
34  
35 2 our (%FORMAT_CT) = (json => 'application/json',
36 txt => 'text/plain',
37 tsv => 'text/tab-separated-values',
38 csv => 'text/csv',
39 xml => 'text/xml');
40  
41 2 our (%FORMAT_CLASS) = (json => 'Web::DataService::Plugin::JSON',
42 txt => 'Web::DataService::Plugin::Text',
43 tsv => 'Web::DataService::Plugin::Text',
44 csv => 'Web::DataService::Plugin::Text',
45 xml => 'Web::DataService::Plugin::XML');
46  
47  
48 # define_format ( attrs... )
49 #
50 # Define one or more formats for data service responses.
51  
52 sub define_format {
53  
54 1 ▶ my $ds = shift;
Subroutine define_format: called CC=32 SCAR=45.0
Pod undocumented
55
56 1 my ($last_node);
57
58 # Now we go through the rest of the arguments. Hashrefs define new
59 # vocabularies, while strings add to the documentation of the vocabulary
60 # whose definition they follow.
61
62 1 foreach my $item (@_)
63 {
64 # A hashref defines a new vocabulary.
65
66 4 ▶ if ( ref $item eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item eq 'HASH') { } 2 2 4
elsif (not ref $item) { } 2 0 2
67 {
68 # Make sure the attributes include 'name'.
69
70 2 my $name = $item->{name};
71
72 2 ▶ croak "define_format: the attributes must include 'name'" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 2 2
73
74 # Make sure this format was not already defined by a previous call.
75
76 2 ▶ croak "define_format: '$name' was already defined" if defined $ds->{format}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ds->{"format"}{$name} 0 2 2
77
78 # Create a new record to represent this format and check the attributes.
79
80 2 my $record = bless { name => $name }, 'Web::DataService::Format';
81
82 2 foreach my $k ( keys %$item )
83 {
84 8 ▶ croak "define_format: invalid attribute '$k'" unless $FORMAT_DEF{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $Web::DataService::Format::FORMAT_DEF{$k} 0 8 8
85
86 8 my $v = $item->{$k};
87
88 8 ▶ if ( $k eq 'default_vocab' && defined $v && $v ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "default_vocab" and defined $v and $v ne "") 0 8 8
Condition33%2/6
$k eq "default_vocab" and defined $v and $v ne ""
!ll&&!rl&&r
800
$k eq "default_vocab" and defined $v
!ll&&!rl&&r
800
MC/DC0%0/3
$k eq "default_vocab" and defined $v and $v ne ""
$k eq "default_vocab" defined $v $v ne ""
Truth table25%1/4
A and B and C
A= $k eq "default_vocab"B= defined $vC= $v ne ""
ABCresult
0XX0
10X0
1100
1111
89 {
90 croak "define_format: unknown vocabulary '$v'"
91 0 ▶ unless ref $ds->{vocab}{$v};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ds->{"vocab"}{$v} 0 0 0
92
93 croak "define_format: cannot default to disabled vocabulary '$v'"
94 0 ▶ if $ds->{vocab}{$v}{disabled} and not $item->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
if $ds->{"vocab"}{$v}{"disabled"} and not $item->{"disabled"} 0 0 0
Condition0%0/3
$ds->{"vocab"}{$v}{"disabled"} and not $item->{"disabled"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ds->{"vocab"}{$v}{"disabled"} and not $item->{"disabled"}
$ds->{"vocab"}{$v}{"disabled"} not $item->{"disabled"}
Truth table0%0/3
A and not(B)
A= $ds->{"vocab"}{$v}{"disabled"}B= not $item->{"disabled"}
ABresult
0X0
100
111
95 }
96
97 8 $record->{$k} = $item->{$k};
98 }
99
100 # Set defaults and check values.
101
102 2 ▶ $record->{content_type} ||= $FORMAT_CT{$name};
Condition66%2/3
$record->{"content_type"} ||= $Web::DataService::Format::FORMAT_CT{$name}
l!l&&r!l&&!r
110
MC/DC0%0/2
$record->{"content_type"} ||= $Web::DataService::Format::FORMAT_CT{$name}
$record->{"content_type"} $Web::DataService::Format::FORMAT_CT{$name}
Truth table66%2/3
A ||= B
A= $record->{"content_type"}B= $Web::DataService::Format::FORMAT_CT{$name}
ABresult
1X1
011
000
103 2 ▶ $record->{uses_header} //= 1 if $name eq 'txt' || $name eq 'tsv' || $name eq 'csv';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "txt" or $name eq "tsv" or $name eq "csv" 0 2 2
Condition25%2/8
$name eq "txt" or $name eq "tsv" or $name eq "csv"
l!l&&r!l&&!r
002
$name eq "txt" or $name eq "tsv"
l!l&&r!l&&!r
002
$record->{"uses_header"} //= 1
l!l
00
MC/DC0%0/3
$name eq "txt" or $name eq "tsv" or $name eq "csv"
$name eq "txt" $name eq "tsv" $name eq "csv"
MC/DC0%0/1
$record->{"uses_header"} //= 1
$record->{"uses_header"}
Truth table25%1/4
A or B or C
A= $name eq "txt"B= $name eq "tsv"C= $name eq "csv"
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $record->{"uses_header"}
Aresult
11
00
104 $record->{is_text} //= 1 if $record->{content_type} =~ /(x(?:ht)?ml|text|json|javascript)/
105 2 ▶ || $record->{encode_as_text};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"content_type"} =~ /(x(?:ht)?ml|text|json|javascript)/ or $record->{"encode_as_text"} 1 1 2
Condition60%3/5
$record->{"content_type"} =~ /(x(?:ht)?ml|text|json|javascript)/ or $record->{"encode_as_text"}
l!l&&r!l&&!r
101
$record->{"is_text"} //= 1
l!l
01
MC/DC50%1/2
$record->{"content_type"} =~ /(x(?:ht)?ml|text|json|javascript)/ or $record->{"encode_as_text"}
$record->{"content_type"} =~ /(x(?:ht)?ml|text|json|javascript)/ $record->{"encode_as_text"}
MC/DC0%0/1
$record->{"is_text"} //= 1
$record->{"is_text"}
Truth table66%2/3
A or B
A= $record->{"content_type"} =~ /(x(?:ht)?ml|text|json|javascript)/B= $record->{"encode_as_text"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $record->{"is_text"}
Aresult
11
00
106
107 croak "define_format: you must specify an HTTP content type for format '$name' using the attribute 'content_type'"
108 2 ▶ unless $record->{content_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $record->{"content_type"} 0 2 2
109
110 2 ▶ $record->{package} //= $record->{module};
Condition66%2/3
$record->{"package"} //= $record->{"module"}
l!l&&r!l&&!r
011
MC/DC50%1/2
$record->{"package"} //= $record->{"module"}
$record->{"package"} $record->{"module"}
Truth table66%2/3
A //= B
A= $record->{"package"}B= $record->{"module"}
ABresult
1X1
011
000
111 2 ▶ $record->{package} //= $FORMAT_CLASS{$name};
Condition66%2/3
$record->{"package"} //= $Web::DataService::Format::FORMAT_CLASS{$name}
l!l&&r!l&&!r
110
MC/DC0%0/2
$record->{"package"} //= $Web::DataService::Format::FORMAT_CLASS{$name}
$record->{"package"} $Web::DataService::Format::FORMAT_CLASS{$name}
Truth table66%2/3
A //= B
A= $record->{"package"}B= $Web::DataService::Format::FORMAT_CLASS{$name}
ABresult
1X1
011
000
112
113 croak "define_format: you must specify a package to implement format '$name' using the attribute 'module'"
114 2 ▶ unless defined $record->{package};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record->{"package"} 0 2 2
115
116 2 ▶ $record->{module} //= $record->{package};
Condition66%2/3
$record->{"module"} //= $record->{"package"}
l!l&&r!l&&!r
110
MC/DC0%0/2
$record->{"module"} //= $record->{"package"}
$record->{"module"} $record->{"package"}
Truth table66%2/3
A //= B
A= $record->{"module"}B= $record->{"package"}
ABresult
1X1
011
000
117
118 # Make sure that the module is loaded, unless the format is disabled.
119
120 2 ▶ if ( $record->{module} && ! $record->{disabled} )
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"module"} and not $record->{"disabled"}) 2 0 2
Condition33%1/3
$record->{"module"} and not $record->{"disabled"}
!ll&&!rl&&r
002
MC/DC0%0/2
$record->{"module"} and not $record->{"disabled"}
$record->{"module"} not $record->{"disabled"}
Truth table33%1/3
A and not(B)
A= $record->{"module"}B= not $record->{"disabled"}
ABresult
0X0
100
111
121 {
122 2 my $filename = $record->{module};
123 2 $filename =~ s{::}{/}g;
124 2 ▶ $filename .= '.pm' unless $filename =~ /\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ /\.pm$/ 2 0 2
125
126 2 require $filename;
127 }
128
129 # Now store the record as a response format for this data service.
130
131 2 $ds->{format}{$name} = $record;
132 2 ▶ push @{$ds->{format_list}}, $name unless $record->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $record->{"disabled"} 2 0 2
133 2 $last_node = $record;
134 }
135
136 # A scalar is taken to be a documentation string.
137
138 elsif ( not ref $item )
139 {
140 2 $ds->add_node_doc($last_node, $item);
141 }
142
143 else
144 {
145 0 croak "define_format: the arguments to this routine must be hashrefs and strings";
146 }
147 }
148
149 1 ▶ croak "define_format: you must include at least one hashref of attributes"
Branch: 1 branch
BranchTrueFalseTotal
unless $last_node 0 1 1
150 unless $last_node;
151 }
152  
153  
154 # list_formats ( )
155 #
156 # Return the list of names of all the formats that have been defined for this
157 # data service.
158  
159 sub list_formats {
160
161 1 ▶ my ($ds) = @_;
Subroutine list_formats: called CC=1 SCAR=0.0
Pod undocumented
162 1 return @{$ds->{format_list}};
163 }
164  
165  
166 # valid_format ( )
167 #
168 # Return a code reference (actually a reference to a closure) that can be used
169 # in a parameter rule to validate a format-selecting parameter. All
170 # non-disabled formats are included.
171  
172 sub format_validator {
173
174 0 ▶ my ($self) = @_;
Subroutine format_validator: not called CC=1 SCAR=6.9
Pod undocumented
175
176 # The ENUM_VALUE subroutine is defined by HTTP::Validate.pm.
177
178 0 return ENUM_VALUE(@{$self->{format_list}});
179 }
180  
181  
182 # document_formats ( path, options )
183 #
184 # Return a string containing POD documentation of the response formats that
185 # are allowed for the request path. If the root path '/' is specified, then
186 # document all of the formats enabled for this data service regardless of
187 # whether they are actually allowed for that path. But formats marked as
188 # undocumented are never shown. If the option 'extended' is specified, then
189 # include the text description of each format.
190  
191 sub document_formats {
192  
193 0 ▶ my ($ds, $path, $options) = @_;
Subroutine document_formats: not called CC=25 SCAR=64.8
Pod undocumented
194
195 0 ▶ $options ||= {};
Condition0%0/2
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
196 0 ▶ $path ||= '/';
Condition0%0/2
$path ||= "/"
l!l
00
MC/DC0%0/1
$path ||= "/"
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
197
198 # If no formats have been defined, return a note to that effect.
199
200 return "MSG_FORMAT_NONE_DEFINED"
201 0 ▶ unless ref $ds->{format_list} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ds->{"format_list"} eq "ARRAY" 0 0 0
202
203 # Now figure out which formats to document. If the path is '/', then
204 # document all of them. Otherwise, go thorugh the list of defined formats
205 # in order, filtering out those which are not allowed for this path. The
206 # reason for doing it this way is so that the formats will always be
207 # listed in the order defined, instead of the arbitrary hash order.
208
209 0 my @formats;
210
211 0 ▶ if ( $path eq '/' )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '/') { } 0 0 0
212 {
213 0 @formats = grep { ! $ds->{format}{$_}{undocumented} } @{$ds->{format_list}};
214 0 ▶ return "MSG_FORMAT_NONE_DEFINED" unless @formats;
Branch: 1 branch
BranchTrueFalseTotal
unless @formats 0 0 0
215 }
216
217 else
218 {
219 0 my $allowed = $ds->node_attr($path, 'allow_format');
220
221 0 ▶ return "MSG_FORMAT_NONE_ALLOWED"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $allowed eq "HASH" 0 0 0
222 unless ref $allowed eq 'HASH';
223
224 0 ▶ @formats = grep { $allowed->{$_} && ! $ds->{format}{$_}{undocumented} } @{$ds->{format_list}};
Branch: 1 branch
BranchTrueFalseTotal
$allowed->{$_} and not $ds->{"format"}{$_}{"undocumented"} 0 0 0
225 0 ▶ return "MSG_FORMAT_NONE_ALLOWED" unless @formats;
Branch: 1 branch
BranchTrueFalseTotal
unless @formats 0 0 0
226 }
227
228 # Go through the list of defined formats in order,
229
230 0 my @paths = grep { $ds->{format}{$_}{doc_node} } @formats;
231
232 0 ▶ my $name_header = $ds->has_feature('format_suffix') ? 'Suffix' : 'Name';
Branch: 1 branch
BranchTrueFalseTotal
$ds->has_feature('format_suffix') ? : 0 0 0
233 0 ▶ my $ext_header = $options->{extended} || ! @paths ? "| Description" : '';
Branch: 1 branch
BranchTrueFalseTotal
$options->{'extended'} || !@paths ? : 0 0 0
Condition0%0/3
$options->{"extended"} or not @paths
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"extended"} or not @paths
$options->{"extended"} not @paths
Truth table0%0/3
A or not(B)
A= $options->{"extended"}B= not @paths
ABresult
1X1
011
000
234 0 ▶ my $doc_header = @paths ? "| Documentation" : '';
Branch: 1 branch
BranchTrueFalseTotal
@paths ? : 0 0 0
235
236 0 my $doc = "=for wds_table_header Format* | $name_header $doc_header $ext_header\n\n";
237 0 $doc .= "=over 4\n\n";
238
239 FORMAT:
240 0 foreach my $name (@formats)
241 {
242 0 my $frec = $ds->{format}{$name};
243 0 ▶ my $title = $frec->{title} || $frec->{name};
Condition0%0/3
$frec->{'title'} || $frec->{'name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$frec->{'title'} || $frec->{'name'}
$frec->{'title'} $frec->{'name'}
Truth table0%0/3
A || B
A= $frec->{'title'}B= $frec->{'name'}
ABresult
1X1
011
000
244 0 ▶ my $doc_link = $ds->node_link($frec->{doc_node}) if $frec->{doc_node};
Branch: 1 branch
BranchTrueFalseTotal
if $frec->{"doc_node"} 0 0 0
245 0 ▶ my $name_or_suffix = $ds->has_feature('format_suffix') ? ".$frec->{name}" : $frec->{name};
Branch: 1 branch
BranchTrueFalseTotal
$ds->has_feature('format_suffix') ? : 0 0 0
246
247 0 ▶ next FORMAT if $frec->{undocumented};
Branch: 1 branch
BranchTrueFalseTotal
if $frec->{"undocumented"} 0 0 0
248
249 0 $doc .= "=item $title | C<$name_or_suffix>";
250 0 ▶ $doc .= " | $doc_link" if $doc_link && @paths && $options->{extended};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_link and @paths and $options->{"extended"} 0 0 0
Condition0%0/6
$doc_link and @paths and $options->{"extended"}
!ll&&!rl&&r
000
$doc_link and @paths
!ll&&!rl&&r
000
MC/DC0%0/3
$doc_link and @paths and $options->{"extended"}
$doc_link @paths $options->{"extended"}
Truth table0%0/4
A and B and C
A= $doc_linkB= @pathsC= $options->{"extended"}
ABCresult
0XX0
10X0
1100
1111
251 0 $doc .= "\n\n";
252
253 0 ▶ if ( $options->{extended} || ! @paths )
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'extended'} or not @paths) { } 0 0 0
elsif ($doc_link) { } 0 0 0
Condition0%0/3
$options->{"extended"} or not @paths
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"extended"} or not @paths
$options->{"extended"} not @paths
Truth table0%0/3
A or not(B)
A= $options->{"extended"}B= not @paths
ABresult
1X1
011
000
254 {
255 0 ▶ $doc .= "$frec->{doc_string}\n\n" if $frec->{doc_string};
Branch: 1 branch
BranchTrueFalseTotal
if $frec->{"doc_string"} 0 0 0
256 }
257
258 elsif ( $doc_link )
259 {
260 0 $doc .= "$doc_link\n\n";
261 }
262 }
263
264 0 $doc .= "=back";
265
266 0 return $doc;
267 }
268  
269  
270 2 1;
« lib/Web/DataService/Request.pm ↑ Summary blib/lib/Web/DataService/Execute.pm »