blib/lib/Text/API/Blueprint.pm

filter: Statementstmt 88.5% 302 / 341 Branchbran 75.7% 106 / 140 Conditioncond 61.9% 26 / 42 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 94.3% 83 / 88 Podpod 100% 29 / 29 totaltotal 83.7% 556 / 664 CC 90 SCAR 18.5
CC
90
Cov
83%
Action
17.6
Resource
15.8
Payload
14.0
CRAP 6.3
↑ Summary
1 11 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Text::API::Blueprint;
4  
5 # ABSTRACT: Markdown generator for API blueprint format
6  
7 11 ▶use Class::Load qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Exception::Delayed;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use HTTP::Headers::Fancy 1.001 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 our $VERSION = '0.003'; # VERSION
13  
14 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 11 our $Autoprint = 0;
19 11 our $Offset = 0;
20  
21 216 ▶sub _autoprint {
Subroutine _autoprint: called CC=5 SCAR=16.4
22 216 my ( $wantarray, $str ) = @_;
23 216 ▶ if ( $Autoprint and not defined $wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::API::Blueprint::Autoprint and not defined $wantarray) { } 2 214 216
Condition66%2/3
$Text::API::Blueprint::Autoprint and not defined $wantarray
!ll&&!rl&&r
21402
MC/DC50%1/2
$Text::API::Blueprint::Autoprint and not defined $wantarray
$Text::API::Blueprint::Autoprint not defined $wantarray
Truth table66%2/3
A and not(B)
A= $Text::API::Blueprint::AutoprintB= not defined $wantarray
ABresult
0X0
100
111
24 2 ▶ if ( ref $Autoprint eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $Text::API::Blueprint::Autoprint eq 'SCALAR') { } 1 1 2
elsif (ref $Text::API::Blueprint::Autoprint eq 'GLOB') { } 1 0 1
25 1 $$Autoprint .= $str;
26 }
27 elsif ( ref $Autoprint eq 'GLOB' ) {
28 1 print $Autoprint $str;
29 }
30 else {
31 0 print $str;
32 }
33 2 return;
34 }
35 else {
36 214 return $str;
37 }
38 }
39  
40 11 ▶no namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 135 ▶sub _rpl {
Subroutine _rpl: called CC=2 SCAR=7.0
43 135 my ( $re, $str, $rpl ) = @_;
44 135 ▶ $rpl //= '';
Condition50%1/2
$rpl //= ""
l!l
0135
MC/DC0%0/1
$rpl //= ""
$rpl
Truth table50%1/2
A
A= $rpl
Aresult
11
00
45 135 $str =~ s{^${re}}{$rpl}seg;
46 135 $str =~ s{${re}$}{$rpl}seg;
47 135 return $str;
48 }
49  
50 135 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
51 135 _rpl( qr{\s+}, +shift );
52 }
53  
54 87 ▶sub _indent {
Subroutine _indent: called CC=2 SCAR=6.9
55 87 my ( $str, $n ) = @_;
56 87 ▶ $n //= 4;
Condition100%2/2
$n //= 4
l!l
384
MC/DC100%1/1
$n //= 4
$n
Truth table100%2/2
A
A= $n
Aresult
11
00
57 87 my $indent = ' ' x $n;
58 87 $str =~ s{(\r?\n)}{$1.$indent}eg;
59 87 return $indent . $str;
60 }
61  
62 21 ▶sub _flatten {
Subroutine _flatten: called CC=3 SCAR=11.0
63 21 my ($str) = @_;
64 21 ▶ return unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 21 21
65 21 my ($pre) = ( $str =~ m{^(\s*)\S} );
66 21 ▶ return $str unless $pre;
Branch: 1 branch
BranchTrueFalseTotal
unless $pre 20 1 21
67 1 $str =~ s{^\Q$pre\E}{}mg;
68 1 return $str;
69 }
70  
71 33 ▶sub _header {
Subroutine _header: called CC=4 SCAR=13.9
72 33 my ( $level, $title, $body, $indent ) = @_;
73 33 my $str = '#' x ( $level + $Offset );
74 33 $str .= " $title\n\n";
75 33 ▶ $body = _indent( $body, $indent ) if $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent 0 33 33
76 33 ▶ if ( ref $body eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 1 32 33
elsif ($body) { } 22 10 32
77 1 $str .= Concat(@$body) . "\n\n";
78 }
79 elsif ($body) {
80 22 $str .= "$body\n\n";
81 }
82 33 return $str;
83 }
84  
85 93 ▶sub _listitem {
Subroutine _listitem: called CC=2 SCAR=6.9
86 93 my ( $keyword, $body, $indent ) = @_;
87 93 my $str = "+ $keyword\n\n";
88 93 ▶ $str .= _indent( $body, $indent ) . "\n\n" if $body;
Branch: 1 branch
BranchTrueFalseTotal
if $body 85 8 93
89 93 return $str;
90 }
91  
92 8 ▶sub _list {
Subroutine _list: called CC=1 SCAR=0.0
93 8 my @items = @_;
94 8 return join "\n" => map { '+ ' . _trim($_) } @items;
95 }
96  
97 62 ▶sub _arrayhashloop {
Subroutine _arrayhashloop: called CC=3 SCAR=11.0
98 62 my ( $arrayhash, $coderef ) = @_;
99 62 ▶ return unless ref $arrayhash eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arrayhash eq "ARRAY" 24 38 62
100 38 my @list = @$arrayhash;
101 38 my @result;
102 38 ▶ while (@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 54 38 92
103 54 my ( $key, $val ) = splice @list, 0 => 2;
104 54 push @result => $coderef->( $key, $val );
105 }
106 38 return @result;
107 }
108  
109 63 ▶sub _complain {
Subroutine _complain: called CC=2 SCAR=7.6
110 63 my ( $name, $hash ) = @_;
111 63 foreach my $key ( keys %$hash ) {
112 0 die "unsupported keyword in $name: $key\n";
113 }
114 }
115  
116 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 11 our @EXPORT_OK;
119  
120 # Compile (8) Meta Intro Resource Group Concat
121 11 ▶BEGIN { push @EXPORT_OK => qw(Compile) }
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 6 ▶sub Compile {
Subroutine Compile: called CC=5 SCAR=16.1
Pod documented
124 6 my $struct = shift;
125 6 my @Body;
126  
127 my ( $host, $name, $description ) =
128 6 map { delete $struct->{$_} } qw(host name description);
129  
130 6 push @Body => Meta($host);
131 6 ▶ push @Body => Intro( $name, $description ) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 3 3 6
132  
133 6 ▶ if ( my $resources = delete $struct->{resources} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $resources = delete $struct->{"resources"}) 1 5 6
134 1 foreach my $resource (@$resources) {
135 1 push @Body => Resource($resource);
136 }
137 }
138 6 ▶ if ( my $groups = delete $struct->{groups} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $groups = delete $struct->{"groups"}) 1 5 6
139 _arrayhashloop(
140 $groups,
141 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 1 my ( $group, $args ) = @_;
143 1 push @Body => Group( $group, $args );
144 }
145 1 );
146 }
147 6 _complain( Compile => $struct );
148 6 return _autoprint( wantarray, Concat(@Body) . "\n" );
149 }
150  
151 # Section (-)
152 11 ▶BEGIN { push @EXPORT_OK => qw(Section) }
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 0 ▶sub Section {
Subroutine Section: not called CC=2 SCAR=17.9
Pod documented
155 0 my ( $coderef, $offset ) = @_;
156 0 ▶ $offset //= 1;
Condition0%0/2
$offset //= 1
l!l
00
MC/DC0%0/1
$offset //= 1
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
157 0 $Offset += $offset;
158 0 my $autoprint = $Autoprint;
159 0 $Autoprint = \"";
160 0 my $X = Exception::Delayed->wantany( undef, $coderef );
161 0 my $str = $$Autoprint;
162 0 $Autoprint = $autoprint;
163 0 $Offset -= $offset;
164 0 $X->result;
165 0 return _autoprint( wantarray, $str );
166 }
167  
168 # Meta (0)
169 11 ▶BEGIN { push @EXPORT_OK => qw(Meta) }
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 7 ▶sub Meta {
Subroutine Meta: called CC=2 SCAR=6.9
Pod documented
172 7 my $str = "FORMAT: 1A8\n";
173 7 ▶ if ( my $host = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = shift()) 4 3 7
174 4 $str .= "HOST: $host\n";
175 }
176 7 return _autoprint( wantarray, "$str\n" );
177 }
178  
179 # Intro (0)
180 11 ▶BEGIN { push @EXPORT_OK => qw(Intro) }
Subroutine BEGIN: called CC=1 SCAR=0.0
181  
182 5 ▶sub Intro {
Subroutine Intro: called CC=2 SCAR=6.9
Pod documented
183 5 my ( $name, $description ) = @_;
184 5 ▶ return _autoprint( wantarray, _header( 1, $name, $description // '' ) );
Condition100%2/2
$description // ""
l!l
32
MC/DC100%1/1
$description // ""
$description
Truth table100%2/2
A
A= $description
Aresult
11
00
185 }
186  
187 # Concat (0)
188 11 ▶BEGIN { push @EXPORT_OK => qw(Concat) }
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 49 ▶sub Concat {
Subroutine Concat: called CC=1 SCAR=0.0
Pod documented
191 49 return _autoprint( wantarray, join "\n\n", map { _trim($_) } grep defined,
192 @_ );
193 }
194  
195 # Text (1) Concat
196 11 ▶BEGIN { push @EXPORT_OK => qw(Text) }
Subroutine BEGIN: called CC=1 SCAR=0.0
197  
198 1 ▶sub Text {
Subroutine Text: called CC=1 SCAR=0.0
Pod documented
199 return _autoprint( wantarray,
200 1 Concat( map { _flatten($_) } map { s{[\r\n]+}{\n}gr } @_ ) );
201 }
202  
203 # Code (0)
204 11 ▶BEGIN { push @EXPORT_OK => qw(Code) }
Subroutine BEGIN: called CC=1 SCAR=0.0
205  
206 17 ▶sub Code {
Subroutine Code: called CC=3 SCAR=11.0
Pod documented
207 17 my ( $code, $lang ) = @_;
208 17 $code = _flatten($code);
209 17 ▶ $lang //= '';
Condition100%2/2
$lang //= ""
l!l
152
MC/DC100%1/1
$lang //= ""
$lang
Truth table100%2/2
A
A= $lang
Aresult
11
00
210 17 my $delimiters = 3;
211 17 my $delimiter;
212 17 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not $code =~ /\Q$delimiter\E/ or do { do { $delimiter = "`" x $delimiters; ++$delimiters } } 17 5 22
213 22 $delimiter = '`' x $delimiters;
214 22 $delimiters++;
215 } until $code !~ m{\Q$delimiter\E};
216 17 return _autoprint( wantarray, "$delimiter$lang\n$code\n$delimiter\n\n" );
217 }
218  
219 # Group (2) Concat Resource
220 11 ▶BEGIN { push @EXPORT_OK => qw(Group) }
Subroutine BEGIN: called CC=1 SCAR=0.0
221  
222 2 ▶sub Group {
Subroutine Group: called CC=2 SCAR=8.4
Pod documented
223 2 my ( $identifier, $body, $indent ) = @_;
224 2 ▶ if ( ref $body eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq "ARRAY") 0 2 2
225 0 $body = Concat( map { Resource($_) } @$body );
226 }
227 2 return _autoprint( wantarray,
228 _header( 1, "Group $identifier", $body, $indent ) );
229 }
230  
231 # Resource (7) Sesction Parameters Model Attributes Action
232 11 ▶BEGIN { push @EXPORT_OK => qw(Resource) }
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 10 ▶sub Resource {
Subroutine Resource: called CC=15 SCAR=27.6
Pod documented
235 10 my $args = shift;
236 my (
237 $method, $uri, $identifier, $body,
238 $indent, $level, $description, $parameters,
239 $attributes, $model, $actions
240 )
241 = delete @$args{
242 10 qw{ method uri identifier body indent level description parameters attributes model actions }
243 };
244 10 _complain( Resource => $args );
245 10 ▶ $level //= 2;
Condition50%1/2
$level //= 2
l!l
010
MC/DC0%0/1
$level //= 2
$level
Truth table50%1/2
A
A= $level
Aresult
11
00
246 10 ▶ $body //= '';
Condition50%1/2
$body //= ""
l!l
010
MC/DC0%0/1
$body //= ""
$body
Truth table50%1/2
A
A= $body
Aresult
11
00
247 10 ▶ if ( ref $body eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'CODE') { } 0 10 10
248 0 $body = Section($body);
249 }
250 else {
251 10 my @body;
252 10 ▶ if ( ref $description eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $description eq 'ARRAY') { } 1 9 10
elsif (defined $description) { } 2 7 9
253 1 push @body => @$description;
254 }
255 elsif ( defined $description ) {
256 2 push @body => $description;
257 }
258 10 ▶ push @body => Parameters($parameters) if defined $parameters;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parameters 1 9 10
259 10 ▶ push @body => Attributes($attributes) if defined $attributes;
Branch: 1 branch
BranchTrueFalseTotal
if defined $attributes 1 9 10
260 10 ▶ push @body => Model($model) if defined $model;
Branch: 1 branch
BranchTrueFalseTotal
if defined $model 1 9 10
261 10 ▶ push @body => map { Action($_) } @$actions if defined $actions;
Branch: 1 branch
BranchTrueFalseTotal
if defined $actions 1 9 10
262 10 $body = Concat(@body);
263 }
264 10 ▶ if ( $method and $uri ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($method and $uri) { } 1 9 10
elsif ($identifier and $uri) { } 1 8 9
elsif ($uri) { } 8 0 8
Condition66%2/3
$method and $uri
!ll&&!rl&&r
901
MC/DC50%1/2
$method and $uri
$method $uri
Truth table66%2/3
A and B
A= $methodB= $uri
ABresult
0X0
100
111
265 1 return _autoprint( wantarray,
266 _header( $level, "$method $uri", $body, $indent ) );
267 }
268 ▶ elsif ( $identifier and $uri ) {
Condition66%2/3
$identifier and $uri
!ll&&!rl&&r
801
MC/DC50%1/2
$identifier and $uri
$identifier $uri
Truth table66%2/3
A and B
A= $identifierB= $uri
ABresult
0X0
100
111
269 1 return _autoprint( wantarray,
270 _header( $level, "$identifier [$uri]", $body, $indent ) );
271 }
272 elsif ($uri) {
273 8 return _autoprint( wantarray,
274 _header( $level, "$uri", $body, $indent ) );
275 }
276 else {
277 0 die "no method and uri or identifier and uri or single uri given";
278 }
279 }
280  
281 # Model (4) Payload
282 11 ▶BEGIN { push @EXPORT_OK => qw(Model) }
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 6 ▶sub Model {
Subroutine Model: called CC=4 SCAR=13.9
Pod documented
285 6 ▶ if ( @_ == 1 and ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 2 4 6
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
402
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
286 2 my $args = shift;
287 2 my $type = delete $args->{type};
288 2 return _autoprint( wantarray, Model( $type, $args ) );
289 }
290 else {
291 4 my ( $media_type, $payload, $indent ) = @_;
292 4 ▶ $payload = Payload($payload) if ref $payload;
Branch: 1 branch
BranchTrueFalseTotal
if ref $payload 3 1 4
293 4 return _autoprint( wantarray,
294 _listitem( "Model ($media_type)", $payload, $indent ) );
295 }
296 }
297  
298 # Schema (0)
299 11 ▶BEGIN { push @EXPORT_OK => qw(Schema) }
Subroutine BEGIN: called CC=1 SCAR=0.0
300  
301 15 ▶sub Schema {
Subroutine Schema: called CC=1 SCAR=0.0
Pod documented
302 15 my ( $body, $indent ) = @_;
303 15 return _autoprint( wantarray, _listitem( "Schema", $body, $indent ) );
304 }
305  
306 # Attribute (-)
307 sub Attribute;
308 11 ▶BEGIN { push @EXPORT_OK => qw(Attribute) }
Subroutine BEGIN: called CC=1 SCAR=0.0
309  
310 14 ▶sub Attribute {
Subroutine Attribute: called CC=8 SCAR=22.6
Pod documented
311 14 my ( $attr, $def ) = @_;
312 14 my $str = "$attr";
313 14 ▶ if ( ref $def eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $def eq 'HASH') { } 12 2 14
elsif (ref $def eq 'ARRAY') { } 2 0 2
314 12 ▶ if ( my $enum = delete $def->{enum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $enum = delete $def->{"enum"}) 0 12 12
315 0 $def->{type} = 'enum[' . $enum . ']';
316 }
317 12 ▶ if ( my $example = delete $def->{example} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $example = delete $def->{"example"}) 12 0 12
318 12 $str .= ": `$example`";
319 }
320 12 ▶ if ( my $type = delete $def->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = delete $def->{"type"}) 12 0 12
321 12 $str .= " ($type)";
322 }
323 12 ▶ if ( my $desc = delete $def->{description} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $desc = delete $def->{"description"}) 12 0 12
324 12 $str .= " - $desc";
325 }
326 12 ▶ if ( my $members = delete $def->{members} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $members = delete $def->{"members"}) 0 12 12
327 0 $str .= "\n" . _indent( _list( map { "`$_`" } @$members ) );
328 }
329 12 _complain( "Attributes($attr)" => $def );
330 }
331 elsif ( ref $def eq 'ARRAY' ) {
332 my @strs = _arrayhashloop(
333 $def,
334 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
335 3 return Attribute(@_);
336 }
337 2 );
338 2 $str .= "\n" . _indent( _list(@strs) );
339 }
340 else {
341 0 croak("second argument is not a HashRef nor an ArrayRef");
342 }
343 14 return $str;
344 }
345  
346 # Attributes (0) Attribute
347 11 ▶BEGIN { push @EXPORT_OK => qw(Attributes) }
Subroutine BEGIN: called CC=1 SCAR=0.0
348  
349 6 ▶sub Attributes {
Subroutine Attributes: called CC=2 SCAR=6.9
Pod documented
350 6 my ( $attrs, $indent ) = @_;
351 6 ▶ if ( ref $attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs) { } 5 1 6
352 my @attrs = _arrayhashloop(
353 $attrs,
354 11 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
355 11 return Attribute(@_);
356 }
357 5 );
358 5 return _autoprint( wantarray,
359 _listitem( "Attributes", _list(@attrs), $indent ) );
360 }
361 else {
362 1 return _autoprint( wantarray,
363 _listitem( "Attributes ($attrs)", _list(), $indent ) );
364 }
365 }
366  
367 # Action (6) Section Relation Parameters Attributes Asset Reference Request_Ref Request Response_Ref Response Concat
368 11 ▶BEGIN { push @EXPORT_OK => qw(Action) }
Subroutine BEGIN: called CC=1 SCAR=0.0
369  
370 16 ▶sub Action {
Subroutine Action: called CC=17 SCAR=28.7
Pod documented
371 16 my $args = shift;
372 my (
373 $method, $uri, $identifier, $body, $indent,
374 $level, $description, $relation, $parameters, $attributes,
375 $assets, $requests, $responses
376 )
377 = delete @$args{
378 16 qw{ method uri identifier body indent level description relation parameters attributes assets requests responses }
379 };
380 16 _complain( Action => $args );
381 16 ▶ $level //= 3;
Condition50%1/2
$level //= 3
l!l
016
MC/DC0%0/1
$level //= 3
$level
Truth table50%1/2
A
A= $level
Aresult
11
00
382 16 ▶ $body //= '';
Condition50%1/2
$body //= ""
l!l
016
MC/DC0%0/1
$body //= ""
$body
Truth table50%1/2
A
A= $body
Aresult
11
00
383 16 ▶ if ( ref $body eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'CODE') { } 0 16 16
384 0 $body = Section($body);
385 }
386 else {
387 16 my @body;
388 16 ▶ if ( ref $description eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $description eq 'ARRAY') { } 1 15 16
elsif (defined $description) { } 1 14 15
389 1 push @body => @$description;
390 }
391 elsif ( defined $description ) {
392 1 push @body => $description;
393 }
394 16 ▶ push @body => Relation($relation) if defined $relation;
Branch: 1 branch
BranchTrueFalseTotal
if defined $relation 1 15 16
395 16 ▶ push @body => Parameters($parameters) if defined $parameters;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parameters 1 15 16
396 16 ▶ push @body => Attributes($attributes) if defined $attributes;
Branch: 1 branch
BranchTrueFalseTotal
if defined $attributes 1 15 16
397 16 ▶ if ($assets) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assets) { } 1 15 16
398 _arrayhashloop(
399 $assets,
400 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
401 2 my ( $identifier, $args ) = @_;
402 2 my @keyword_id = split( m{\s+}, $identifier, 2 );
403 2 ▶ if ( ref $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args) { } 2 0 2
404 2 push @body => Asset( @keyword_id, $args );
405 }
406 else {
407 0 push @body => Reference( @keyword_id, $args );
408 }
409 }
410 1 );
411 }
412 else {
413 _arrayhashloop(
414 $requests,
415 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
416 4 my ( $identifier, $args ) = @_;
417 4 ▶ if ( ref $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args) { } 2 2 4
418 2 push @body => Request( $identifier, $args );
419 }
420 else {
421 2 push @body => Request_Ref( $identifier, $args );
422 }
423 }
424 15 );
425 _arrayhashloop(
426 $responses,
427 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
428 4 my ( $identifier, $args ) = @_;
429 4 ▶ if ( ref $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args) { } 2 2 4
430 2 push @body => Response( $identifier, $args );
431 }
432 else {
433 2 push @body => Response_Ref( $identifier, $args );
434 }
435 }
436 15 );
437 }
438 16 ▶ $body = Concat(@body) if @body;
Branch: 1 branch
BranchTrueFalseTotal
if @body 11 5 16
439 }
440  
441 16 ▶ if ( $identifier and $method and $uri ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($identifier and $method and $uri) { } 1 15 16
elsif ($identifier and $method) { } 1 14 15
elsif ($method) { } 14 0 14
Condition83%5/6
$identifier and $method and $uri
!ll&&!rl&&r
1411
$identifier and $method
!ll&&!rl&&r
1402
MC/DC66%2/3
$identifier and $method and $uri
$identifier $method $uri
Truth table75%3/4
A and B and C
A= $identifierB= $methodC= $uri
ABCresult
0XX0
10X0
1100
1111
442 1 return _autoprint( wantarray,
443 _header( $level, "$identifier [$method $uri]", $body, $indent ) );
444 }
445 ▶ elsif ( $identifier and $method ) {
Condition66%2/3
$identifier and $method
!ll&&!rl&&r
1401
MC/DC50%1/2
$identifier and $method
$identifier $method
Truth table66%2/3
A and B
A= $identifierB= $method
ABresult
0X0
100
111
446 1 return _autoprint( wantarray,
447 _header( $level, "$identifier [$method]", $body, $indent ) );
448 }
449 elsif ($method) {
450 14 return _autoprint( wantarray,
451 _header( $level, "$method", $body, $indent ) );
452 }
453 else {
454 0 die
455 "no identifier and method and uri or identifier and method or single method given";
456 }
457 }
458  
459 # Payload (3) Headers Attributes Body Body_CODE Body_YAML Body_JSON Schema Concat
460 11 ▶BEGIN { push @EXPORT_OK => qw(Payload) }
Subroutine BEGIN: called CC=1 SCAR=0.0
461  
462 14 ▶sub Payload {
Subroutine Payload: called CC=10 SCAR=26.4
Pod documented
463 14 my $args = shift;
464 14 my @body;
465 14 ▶ if ( exists $args->{description} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"description"}) 14 0 14
466 14 ▶ if ( ref $args->{description} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'description'} eq 'ARRAY') { } 1 13 14
467 1 push @body => @{ delete $args->{description} };
468 }
469 else {
470 13 push @body => delete $args->{description};
471 }
472 }
473 14 ▶ push @body => Headers( delete $args->{headers} ) if exists $args->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"headers"} 14 0 14
474 push @body => Attributes( delete $args->{attributes} )
475 14 ▶ if exists $args->{attributes};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"attributes"} 2 12 14
476  
477 14 ▶ if ( exists $args->{body} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $args->{'body'}) { } 0 14 14
elsif (exists $args->{'code'}) { } 14 0 14
elsif (exists $args->{'yaml'}) { } 0 0 0
elsif (exists $args->{'json'}) { } 0 0 0
478 0 push @body => Body( delete $args->{body} );
479 }
480 elsif ( exists $args->{code} ) {
481 14 push @body => Body_CODE( delete $args->{code}, delete $args->{lang} );
482 }
483 elsif ( exists $args->{yaml} ) {
484 0 push @body => Body_YAML( delete $args->{yaml} );
485 }
486 elsif ( exists $args->{json} ) {
487 0 push @body => Body_JSON( delete $args->{json} );
488 }
489  
490 14 ▶ push @body => Schema( delete $args->{schema} ) if exists $args->{schema};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"schema"} 14 0 14
491  
492 14 _complain( Payload => $args );
493 14 return _autoprint( wantarray, Concat(@body) );
494 }
495  
496 # Asset (4) Payload
497 11 ▶BEGIN { push @EXPORT_OK => qw(Asset) }
Subroutine BEGIN: called CC=1 SCAR=0.0
498  
499 9 ▶sub Asset {
Subroutine Asset: called CC=2 SCAR=7.0
Pod documented
500 9 my ( $keyword, $identifier, $payload ) = @_;
501 9 my $str = "$keyword $identifier";
502 9 ▶ if ( my $media_type = delete $payload->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $media_type = delete $payload->{"type"}) 9 0 9
503 9 $str .= " ($media_type)";
504 }
505 9 return _autoprint( wantarray, _listitem( $str, Payload($payload) ) );
506 }
507  
508 # Reference (0)
509 11 ▶BEGIN { push @EXPORT_OK => qw(Reference) }
Subroutine BEGIN: called CC=1 SCAR=0.0
510  
511 7 ▶sub Reference {
Subroutine Reference: called CC=1 SCAR=0.0
Pod documented
512 7 my ( $keyword, $identifier, $reference ) = @_;
513 7 return _autoprint( wantarray,
514 _listitem( "$keyword $identifier", "[$reference][]" ) );
515 }
516  
517 # Request (5) Asset
518 11 ▶BEGIN { push @EXPORT_OK => qw(Request) }
Subroutine BEGIN: called CC=1 SCAR=0.0
519  
520 3 ▶sub Request {
Subroutine Request: called CC=1 SCAR=0.0
Pod documented
521 3 unshift @_ => 'Request';
522 3 goto &Asset;
523 }
524  
525 # Request_Ref (1) Reference
526 11 ▶BEGIN { push @EXPORT_OK => qw(Request_Ref) }
Subroutine BEGIN: called CC=1 SCAR=0.0
527  
528 3 ▶sub Request_Ref {
Subroutine Request_Ref: called CC=1 SCAR=0.0
Pod documented
529 3 unshift @_ => 'Request';
530 3 goto &Reference;
531 }
532  
533 # Response (5) Asset
534 11 ▶BEGIN { push @EXPORT_OK => qw(Response) }
Subroutine BEGIN: called CC=1 SCAR=0.0
535  
536 3 ▶sub Response {
Subroutine Response: called CC=1 SCAR=0.0
Pod documented
537 3 unshift @_ => 'Response';
538 3 goto &Asset;
539 }
540  
541 # Response_Ref (1) Reference
542 11 ▶BEGIN { push @EXPORT_OK => qw(Response_Ref) }
Subroutine BEGIN: called CC=1 SCAR=0.0
543  
544 3 ▶sub Response_Ref {
Subroutine Response_Ref: called CC=1 SCAR=0.0
Pod documented
545 3 unshift @_ => 'Response';
546 3 goto &Reference;
547 }
548  
549 # Parameters (2) Parameter
550 11 ▶BEGIN { push @EXPORT_OK => qw(Parameters) }
Subroutine BEGIN: called CC=1 SCAR=0.0
551  
552 3 ▶sub Parameters {
Subroutine Parameters: called CC=1 SCAR=0.0
Pod documented
553 3 my $body = '';
554 _arrayhashloop(
555 shift,
556 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
557 4 my ( $name, $opts ) = @_;
558 4 $body .= Parameter( $name, $opts );
559 }
560 3 );
561 3 return _autoprint( wantarray, _listitem( 'Parameters', $body ) );
562 }
563  
564 # Parameter (1) Concat
565 11 ▶BEGIN { push @EXPORT_OK => qw(Parameter) }
Subroutine BEGIN: called CC=1 SCAR=0.0
566  
567 5 ▶sub Parameter {
Subroutine Parameter: called CC=10 SCAR=24.7
Pod documented
568 5 my ( $name, $opts ) = @_;
569 my ( $example_value, $required, $type, $enum, $shortdesc, $longdesc,
570 $default, $members )
571 = delete @$opts{
572 5 qw{ example required type enum shortdesc longdesc default members }};
573 5 _complain( Parameter => $opts );
574  
575 5 ▶ my $constraint = $required ? 'required' : 'optional';
Branch: 1 branch
BranchTrueFalseTotal
$required ? : 5 0 5
576  
577 5 ▶ if ( defined $enum ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $enum) 5 0 5
578 5 $type = "enum[$enum]";
579 }
580  
581 5 my @itembody;
582  
583 5 ▶ if ( ref $longdesc eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $longdesc eq 'ARRAY') { } 0 5 5
elsif (defined $longdesc) { } 5 0 5
584 0 push @itembody => @$longdesc;
585 }
586 elsif ( defined $longdesc ) {
587 5 push @itembody => split /(\r?\n){2,}/, $longdesc;
588 }
589  
590 5 my $str = "$name:";
591 5 ▶ $str .= " `$example_value`" if defined $example_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $example_value 5 0 5
592 5 ▶ $str .= " ($type, $constraint)" if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 5 0 5
593 5 ▶ $str .= " - $shortdesc" if defined $shortdesc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $shortdesc 5 0 5
594  
595 5 ▶ push @itembody => _listitem("Default: `$default`") if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 5 0 5
596  
597 my @members = _arrayhashloop(
598 $members,
599 8 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
600 8 sprintf '+ `%s` - %s' => @_;
601 }
602 5 );
603 5 ▶ push @itembody => _listitem( "Members", join( "\n" => @members ) )
Branch: 1 branch
BranchTrueFalseTotal
if @members 5 0 5
604 if @members;
605  
606 5 my $itembody = Concat(@itembody);
607  
608 5 return _autoprint( wantarray, _listitem( $str, $itembody ) );
609 }
610  
611 # Headers (0)
612 11 ▶BEGIN { push @EXPORT_OK => qw(Headers) }
Subroutine BEGIN: called CC=1 SCAR=0.0
613  
614 15 ▶sub Headers {
Subroutine Headers: called CC=1 SCAR=0.0
Pod documented
615 15 my $body = '';
616 15 my $fancy = HTTP::Headers::Fancy->new;
617 _arrayhashloop(
618 shift,
619 17 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
620 17 my ( $name, $value ) = @_;
621 17 $name = $fancy->prettify_key( $fancy->encode_key($name) );
622 17 $body .= "\n $name: $value";
623 }
624 15 );
625 15 $body =~ s{^\n+}{}s;
626 15 return _autoprint( wantarray, _listitem( 'Headers', $body ) );
627 }
628  
629 # Body (0)
630 11 ▶BEGIN { push @EXPORT_OK => qw(Body) }
Subroutine BEGIN: called CC=1 SCAR=0.0
631  
632 1 ▶sub Body {
Subroutine Body: called CC=1 SCAR=0.0
Pod documented
633 1 my $body = _flatten(shift);
634 1 return _autoprint( wantarray, _listitem( 'Body', $body, 8 ) );
635 }
636  
637 # Body_CODE (1) Code
638 11 ▶BEGIN { push @EXPORT_OK => qw(Body_CODE) }
Subroutine BEGIN: called CC=1 SCAR=0.0
639  
640 16 ▶sub Body_CODE {
Subroutine Body_CODE: called CC=1 SCAR=0.0
Pod documented
641 16 return _autoprint( wantarray, _listitem( 'Body', Code(@_) ) );
642 }
643  
644 0 ▶sub _yaml {
Subroutine _yaml: not called CC=1 SCAR=6.9
645 0 my ($struct) = @_;
646 0 load_class('YAML::Any');
647 0 YAML::Any::Dump($struct);
648 }
649  
650 # Body_YAML (2) Body_CODE
651 11 ▶BEGIN { push @EXPORT_OK => qw(Body_YAML) }
Subroutine BEGIN: called CC=1 SCAR=0.0
652  
653 0 ▶sub Body_YAML {
Subroutine Body_YAML: not called CC=1 SCAR=6.9
Pod documented
654 0 my ($struct) = @_;
655 0 return _autoprint( wantarray, Body_CODE( _yaml($struct), 'yaml' ) );
656 }
657  
658 0 ▶sub _json {
Subroutine _json: not called CC=2 SCAR=17.9
659 0 my ($struct) = @_;
660 0 load_class('JSON');
661 0 ▶ our $JSON //= JSON->new->utf8->pretty->allow_nonref->convert_blessed;
Condition0%0/3
our $JSON //= "JSON"->new->utf8->pretty->allow_nonref->convert_blessed
l!l&&r!l&&!r
000
MC/DC0%0/2
our $JSON //= "JSON"->new->utf8->pretty->allow_nonref->convert_blessed
our $JSON "JSON"->new->utf8->pretty->allow_nonref->convert_blessed
Truth table0%0/3
A //= B
A= our $JSONB= "JSON"->new->utf8->pretty->allow_nonref->convert_blessed
ABresult
1X1
011
000
662 0 $JSON->encode($struct);
663 }
664  
665 # Body_JSON (2) Body_CODE
666 11 ▶BEGIN { push @EXPORT_OK => qw(Body_JSON) }
Subroutine BEGIN: called CC=1 SCAR=0.0
667  
668 0 ▶sub Body_JSON {
Subroutine Body_JSON: not called CC=1 SCAR=6.9
Pod documented
669 0 my ($struct) = @_;
670 0 return _autoprint( wantarray, Body_CODE( _json($struct), 'json' ) );
671 }
672  
673 # Relation (0)
674 11 ▶BEGIN { push @EXPORT_OK => qw(Relation) }
Subroutine BEGIN: called CC=1 SCAR=0.0
675  
676 2 ▶sub Relation {
Subroutine Relation: called CC=1 SCAR=0.0
Pod documented
677 2 my $link = shift;
678 2 return _autoprint( wantarray, _listitem("Relation: $link") );
679 }
680  
681 11 1;
682  
683 __END__
↑ Summary