blib/lib/Open/API/UI.pm

filter: Statementstmt 92.3% 169 / 183 Branchbran 73.0% 73 / 100 Conditioncond 53.0% 44 / 83 MC/DCmcdc 25.0% 11 / 44 Subroutinesub 96.4% 27 / 28 Podpod 100% 8 / 8 totaltotal 74.4% 332 / 446 CC 93 SCAR 37.5
CC
93
Cov
78%
_build_data
120.8
new
24.1
_wrap_psgi
9.1
CRAP 42.3
↑ Summary blib/arch/Open/API/Install/Files.pm »
1 package Open::API::UI;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Raw::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Open::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Template::Stencil;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Markdown::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '0.13';
14  
15 3 my @HEADERS = (
16 'Content-Security-Policy' =>
17 "default-src 'none'; script-src 'self'; style-src 'self'; "
18 . "connect-src 'self'; img-src 'self' data:; frame-ancestors 'none'; "
19 . "base-uri 'none'; form-action 'self'",
20 'X-Content-Type-Options' => 'nosniff',
21 'X-Frame-Options' => 'DENY',
22 'Referrer-Policy' => 'no-referrer',
23 'Cache-Control' => 'no-cache',
24 );
25  
26 3 my %ASSETS = (
27 'app.css' => 'text/css; charset=utf-8',
28 'app.js' => 'application/javascript; charset=utf-8',
29 );
30  
31 sub _base_dir {
32 43 ▶ my $pm = $INC{'Open/API/UI.pm'}
Subroutine _base_dir: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
my $pm = $INC{"Open/API/UI.pm"} or Carp::croak("Open::API::UI: cannot locate my own installation") 0 43 43
33 or Carp::croak("Open::API::UI: cannot locate my own installation");
34 43 (my $dir = $pm) =~ s/\.pm\z//;
35 43 return $dir;
36 }
37  
38 sub new {
39 23 ▶ my $class = shift;
Subroutine new: called CC=22 SCAR=31.8
Pod documented
40 23 ▶ my %opts = @_ == 1 && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 23 23
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2300
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
41  
42 23 my $api = delete $opts{api};
43 23 ▶ Carp::croak("Open::API::UI->new: 'api' must be an Open::API")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($api) and $api->isa("Open::API") 1 22 23
Condition66%2/3
builtin::blessed($api) and $api->isa("Open::API")
!ll&&!rl&&r
1022
MC/DC50%1/2
builtin::blessed($api) and $api->isa("Open::API")
builtin::blessed($api) $api->isa("Open::API")
Truth table66%2/3
A and B
A= builtin::blessed($api)B= $api->isa("Open::API")
ABresult
0X0
100
111
44 unless Scalar::Util::blessed($api) && $api->isa('Open::API');
45  
46 22 my $self = bless { api => $api }, $class;
47  
48 22 for my $k (qw(path spec_path title try_it csrf headers)) {
49 132 ▶ $self->{$k} = delete $opts{$k} if exists $opts{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{$k} 22 110 132
50 }
51 22 ▶ Carp::croak("Open::API::UI->new: unknown option(s) "
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 21 22
52 . join(', ', map "'$_'", sort keys %opts)) if %opts;
53  
54 21 ▶ $self->{path} = '/docs' unless defined $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"path"} 16 5 21
55 21 ▶ $self->{spec_path} = '/openapi.json' unless defined $self->{spec_path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"spec_path"} 18 3 21
56 21 ▶ $self->{try_it} = 1 unless defined $self->{try_it};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"try_it"} 20 1 21
57 $self->{csrf} = { header => 'X-CSRF-Token', cookie => 'csrf' }
58 21 ▶ unless exists $self->{csrf};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"csrf"} 10 11 21
59  
60 21 for my $k (qw(path spec_path)) {
61 Carp::croak("Open::API::UI->new: '$k' must start with '/'")
62 41 ▶ unless $self->{$k} =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$k} =~ m[\A/] 1 40 41
63 40 ▶ $self->{$k} =~ s{/\z}{} if length $self->{$k} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{$k} > 1 40 0 40
64 }
65 Carp::croak("Open::API::UI->new: 'path' and 'spec_path' must differ")
66 20 ▶ if $self->{path} eq $self->{spec_path};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"path"} eq $self->{"spec_path"} 1 19 20
67 19 ▶ if (my $c = $self->{csrf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->{"csrf"}) 10 9 19
68 10 ▶ Carp::croak("Open::API::UI->new: 'csrf' must be a hashref or false")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "HASH" 0 10 10
69 unless ref $c eq 'HASH';
70 10 ▶ $c->{header} = 'X-CSRF-Token' unless defined $c->{header};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->{"header"} 0 10 10
71 10 ▶ $c->{cookie} = 'csrf' unless defined $c->{cookie};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->{"cookie"} 1 9 10
72 }
73 19 ▶ if (defined $self->{headers} && ref $self->{headers} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"headers"} and ref $self->{"headers"} ne "HASH") 0 19 19
Condition66%2/3
defined $self->{"headers"} and ref $self->{"headers"} ne "HASH"
!ll&&!rl&&r
1810
MC/DC0%0/2
defined $self->{"headers"} and ref $self->{"headers"} ne "HASH"
defined $self->{"headers"} ref $self->{"headers"} ne "HASH"
Truth table66%2/3
A and B
A= defined $self->{"headers"}B= ref $self->{"headers"} ne "HASH"
ABresult
0X0
100
111
74 0 Carp::croak("Open::API::UI->new: 'headers' must be a hashref");
75 }
76  
77 19 $self->{header_pairs} = _merge_headers($self->{headers});
78  
79 # Render eagerly: a template problem is a configuration error and
80 # belongs at startup, like everything else in this stack.
81 19 $self->{html} = $self->_render;
82 19 return $self;
83 }
84  
85 sub _merge_headers {
86 19 ▶ my ($over) = @_;
Subroutine _merge_headers: called CC=8 SCAR=20.8
87 19 my @out;
88 my %ov;
89 19 ▶ if ($over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($over) 1 18 19
90 1 $ov{lc $_} = [ $_, $over->{$_} ] for keys %$over;
91 }
92 19 ▶ for (my $i = 0; $i < @HEADERS; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @HEADERS) 95 19 114
93 95 my ($k, $v) = @HEADERS[$i, $i + 1];
94 95 ▶ if (my $o = delete $ov{lc $k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = delete $ov{lc $k}) { } 2 93 95
95 2 ▶ push @out, $k, $o->[1] if defined $o->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $o->[1] 1 1 2
96 } else {
97 93 push @out, $k, $v;
98 }
99 }
100 19 for my $k (sort keys %ov) {
101 1 ▶ push @out, $ov{$k}[0], $ov{$k}[1] if defined $ov{$k}[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $ov{$k}[1] 1 0 1
102 }
103 19 return \@out;
104 }
105  
106 sub _build_data {
107 19 ▶ my ($self) = @_;
Subroutine _build_data: called CC=35 SCAR=47.9
108 19 my $spec = $self->{api}->spec;
109 19 ▶ my $info = $spec->{info} || {};
Condition50%1/2
$spec->{'info'} || {}
l!l
190
MC/DC0%0/1
$spec->{'info'} || {}
$spec->{'info'}
Truth table50%1/2
A
A= $spec->{'info'}
Aresult
11
00
110  
111 # OpenAPI descriptions are CommonMark; render the ones the shell owns
112 # (info, tags, operations) once, at boot. Markdown::Simple's GFM
113 # defaults strip raw HTML and dangerous URLs, so the output is safe to
114 # inject with {% raw %}.
115 19 my $md = Markdown::Simple->new;
116 my $mdh = sub {
117 105 ▶ my ($text) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
118 105 ▶ return '' unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 102 3 105
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
10203
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
119 3 return $md->render($text);
120 19 };
121  
122 1 ▶ my %tag_desc = map { ($_->{name} // '') => ($_->{description} // '') }
Condition50%2/4
$_->{"name"} // ""
l!l
10
$_->{"description"} // ""
l!l
10
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
MC/DC0%0/1
$_->{"description"} // ""
$_->{"description"}
Truth table50%1/2
A
A= $_->{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"description"}
Aresult
11
00
123 19 ▶ grep { ref $_ eq 'HASH' } @{ $spec->{tags} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$spec->{"tags"} or [] 18 1 19
124  
125 19 my (@tags, %tag_by_name);
126 19 for my $o (@{ $self->{api}->operations }) {
127 67 ▶ my $raw = $spec->{paths}{ $o->{path} }{ lc $o->{method} } || {};
Condition50%1/2
$spec->{'paths'}{$o->{'path'}}{lc $o->{'method'}} || {}
l!l
670
MC/DC0%0/1
$spec->{'paths'}{$o->{'path'}}{lc $o->{'method'}} || {}
$spec->{'paths'}{$o->{'path'}}{lc $o->{'method'}}
Truth table50%1/2
A
A= $spec->{'paths'}{$o->{'path'}}{lc $o->{'method'}}
Aresult
11
00
128 1 my $tname = (ref $raw->{tags} eq 'ARRAY' && @{ $raw->{tags} })
129 67 ▶ ? $raw->{tags}[0] : 'default';
Branch: 1 branch
BranchTrueFalseTotal
ref $raw->{'tags'} eq 'ARRAY' && @{$raw->{'tags'};} ? : 1 66 67
Condition66%2/3
ref $raw->{"tags"} eq "ARRAY" and @{$raw->{"tags"};}
!ll&&!rl&&r
6601
MC/DC50%1/2
ref $raw->{"tags"} eq "ARRAY" and @{$raw->{"tags"};}
ref $raw->{"tags"} eq "ARRAY" @{$raw->{"tags"};}
Truth table66%2/3
A and B
A= ref $raw->{"tags"} eq "ARRAY"B= @{$raw->{"tags"};}
ABresult
0X0
100
111
130 67 ▶ my $tag = $tag_by_name{$tname} ||= do {
Condition66%2/3
$tag_by_name{$tname} ||= do { (my $slug = lc $tname) =~ s/[^a-z0-9]+/-/g; $slug =~ s/\A-+|-+\z//g; my $t = {"name", $tname, "slug", $slug || "default", "description_html", &$mdh($tag_desc{$tname}), "ops", []}; push @tags, $t; $t }
l!l&&r!l&&!r
48190
MC/DC0%0/2
$tag_by_name{$tname} ||= do { (my $slug = lc $tname) =~ s/[^a-z0-9]+/-/g; $slug =~ s/\A-+|-+\z//g; my $t = {"name", $tname, "slug", $slug || "default", "description_html", &$mdh($tag_desc{$tname}), "ops", []}; push @tags, $t; $t }
$tag_by_name{$tname} do { (my $slug = lc $tname) =~ s/[^a-z0-9]+/-/g; $slug =~ s/\A-+|-+\z//g; my $t = {"name", $tname, "slug", $slug || "default", "description_html", &$mdh($tag_desc{$tname}), "ops", []}; push @tags, $t; $t }
Truth table66%2/3
A ||= B
A= $tag_by_name{$tname}B= do { (my $slug = lc $tname) =~ s/[^a-z0-9]+/-/g; $slug =~ s/\A-+|-+\z//g; my $t = {"name", $tname, "slug", $slug || "default", "description_html", &$mdh($tag_desc{$tname}), "ops", []}; push @tags, $t; $t }
ABresult
1X1
011
000
131 19 (my $slug = lc $tname) =~ s/[^a-z0-9]+/-/g;
132 19 $slug =~ s/\A-+|-+\z//g;
133 my $t = { name => $tname, slug => ($slug || 'default'),
134 19 ▶ description_html => $mdh->($tag_desc{$tname}),
Condition50%1/2
$slug || 'default'
l!l
190
MC/DC0%0/1
$slug || 'default'
$slug
Truth table50%1/2
A
A= $slug
Aresult
11
00
135 ops => [] };
136 19 push @tags, $t;
137 19 $t;
138 };
139 67 ▶ push @{ $tag->{ops} }, {
Branch: 1 branch
BranchTrueFalseTotal
$raw->{'deprecated'} ? : 0 67 67
Condition50%1/2
$raw->{"summary"} // ""
l!l
067
MC/DC0%0/1
$raw->{"summary"} // ""
$raw->{"summary"}
Truth table50%1/2
A
A= $raw->{"summary"}
Aresult
11
00
140 id => $o->{operationId},
141 method => uc $o->{method},
142 method_lc => lc $o->{method},
143 path => $o->{path},
144 summary => $raw->{summary} // '',
145 deprecated => $raw->{deprecated} ? 1 : 0,
146 67 description_html => $mdh->($raw->{description}),
147 };
148 }
149  
150 my @servers = map {
151 0 ▶ { url => $_->{url} // '', description => $_->{description} // '' }
Condition0%0/4
$_->{"url"} // ""
l!l
00
$_->{"description"} // ""
l!l
00
MC/DC0%0/1
$_->{"url"} // ""
$_->{"url"}
MC/DC0%0/1
$_->{"description"} // ""
$_->{"description"}
Truth table0%0/2
A
A= $_->{"url"}
Aresult
11
00
Truth table0%0/2
A
A= $_->{"description"}
Aresult
11
00
152 19 ▶ } grep { ref $_ eq 'HASH' } @{ $spec->{servers} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$spec->{"servers"} or [] 19 0 19
153  
154 19 my @schemes;
155 19 ▶ my $ss = ($spec->{components} || {})->{securitySchemes} || {};
Condition75%3/4
+($spec->{'components'} || {})->{'securitySchemes'} || {}
l!l
019
$spec->{'components'} || {}
l!l
163
MC/DC100%1/1
+($spec->{'components'} || {})->{'securitySchemes'} || {}
$spec->{'components'}
Truth table100%2/2
A
A= $spec->{'components'}
Aresult
11
00
156 19 for my $name (sort keys %$ss) {
157 0 my $s = $ss->{$name};
158 0 ▶ next unless ref $s eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $s eq "HASH" 0 0 0
159 0 ▶ my $type = $s->{type} // '';
Condition0%0/2
$s->{"type"} // ""
l!l
00
MC/DC0%0/1
$s->{"type"} // ""
$s->{"type"}
Truth table0%0/2
A
A= $s->{"type"}
Aresult
11
00
160 my $label =
161 $type eq 'apiKey' ? "apiKey in $s->{in} ($s->{name})"
162 0 ▶ : $type eq 'http' ? "http " . ($s->{scheme} // '')
Branch: 4 branches
BranchTrueFalseTotal
$type eq 'apiKey' ? : 0 0 0
$type eq 'http' ? : 0 0 0
$type eq 'oauth2' ? : 0 0 0
$type eq 'openIdConnect' ? : 0 0 0
Condition0%0/2
$s->{"scheme"} // ""
l!l
00
MC/DC0%0/1
$s->{"scheme"} // ""
$s->{"scheme"}
Truth table0%0/2
A
A= $s->{"scheme"}
Aresult
11
00
163 : $type eq 'oauth2' ? 'oauth2 (paste a bearer token)'
164 : $type eq 'openIdConnect' ? 'openIdConnect (paste a bearer token)'
165 : $type;
166 push @schemes, {
167 name => $name,
168 type => $type,
169 scheme => $s->{scheme} // '',
170 in => $s->{in} // '',
171 0 ▶ key => $s->{name} // '',
Condition0%0/6
$s->{"scheme"} // ""
l!l
00
$s->{"in"} // ""
l!l
00
$s->{"name"} // ""
l!l
00
MC/DC0%0/1
$s->{"scheme"} // ""
$s->{"scheme"}
MC/DC0%0/1
$s->{"in"} // ""
$s->{"in"}
MC/DC0%0/1
$s->{"name"} // ""
$s->{"name"}
Truth table0%0/2
A
A= $s->{"scheme"}
Aresult
11
00
Truth table0%0/2
A
A= $s->{"in"}
Aresult
11
00
Truth table0%0/2
A
A= $s->{"name"}
Aresult
11
00
172 label => $label,
173 };
174 }
175  
176 my $config_json = File::Raw::JSON::file_json_encode({
177 basePath => $self->{path},
178 specPath => $self->{spec_path},
179 tryIt => $self->{try_it} ? 1 : 0,
180 19 ▶ csrf => $self->{csrf} ? { %{ $self->{csrf} } } : 0,
Branch: 2 branches
BranchTrueFalseTotal
$self->{'try_it'} ? : 18 1 19
$self->{'csrf'} ? : 10 9 19
181 });
182 # The config rides inside a <script type="application/json"> block;
183 # a '<' in any value must never be able to close it.
184 19 $config_json =~ s/</\\u003c/g;
185  
186 return {
187 title => $self->{title} // $info->{title} // 'API',
188 base => $self->{path},
189 config_json => $config_json,
190 info => {
191 title => $info->{title} // 'API',
192 version => $info->{version} // '',
193 19 ▶ description_html => $mdh->($info->{description}),
Condition55%5/9
($self->{"title"} // $info->{"title"}) // "API"
l!l
190
$self->{"title"} // $info->{"title"}
l!l&&r!l&&!r
1180
$info->{"title"} // "API"
l!l
190
$info->{"version"} // ""
l!l
190
MC/DC0%0/2
($self->{"title"} // $info->{"title"}) // "API"
$self->{"title"} $info->{"title"}
MC/DC0%0/1
$info->{"title"} // "API"
$info->{"title"}
MC/DC0%0/1
$info->{"version"} // ""
$info->{"version"}
Truth table66%2/3
A // B
A= $self->{"title"}B= $info->{"title"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $info->{"title"}
Aresult
11
00
Truth table50%1/2
A
A= $info->{"version"}
Aresult
11
00
194 },
195 servers => \@servers,
196 schemes => \@schemes,
197 tags => \@tags,
198 };
199 }
200  
201 sub _render {
202 19 ▶ my ($self) = @_;
Subroutine _render: called CC=1 SCAR=0.0
203 19 my $stencil = Template::Stencil->new(
204 template_dir => _base_dir() . '/templates',
205 wrapper => 'wrapper.tmpl',
206 );
207 19 return $stencil->render('index', $self->_build_data);
208 }
209  
210 16 ▶sub index_html { $_[0]{html} }
Subroutine index_html: called CC=1 SCAR=0.0
Pod documented
211  
212 # The same page as a fragment, for a host page that already has a
213 # <head> of its own - a product page showing the docs for a spec
214 # somebody just pasted, rather than a documentation site.
215 #
216 # It carries the config block, because app.js reads its settings from
217 # there and a fragment without it is inert. It does not carry the
218 # stylesheet or script tags: the host is serving those assets from
219 # wherever it serves assets, and only the host knows where that is.
220 sub embed_html {
221 0 ▶ my ($self) = @_;
Subroutine embed_html: not called CC=2 SCAR=17.9
Pod documented
222 0 ▶ return $self->{embed} if defined $self->{embed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"embed"} 0 0 0
223 0 my $data = $self->_build_data;
224 0 my $stencil = Template::Stencil->new(
225 template_dir => _base_dir() . '/templates',
226 );
227 return $self->{embed} =
228 qq{<script type="application/json" id="oa-config">}
229 . $data->{config_json}
230 0 . qq{</script>\n}
231 . $stencil->render('index', $data);
232 }
233  
234 sub spec_json {
235 12 ▶ my ($self) = @_;
Subroutine spec_json: called CC=2 SCAR=8.1
Pod documented
236 $self->{spec_json} //=
237 12 ▶ File::Raw::JSON::file_json_encode($self->{api}->spec);
Condition33%1/3
$self->{"spec_json"} //= File::Raw::JSON::file_json_encode($self->{"api"}->spec)
l!l&&r!l&&!r
0120
MC/DC0%0/2
$self->{"spec_json"} //= File::Raw::JSON::file_json_encode($self->{"api"}->spec)
$self->{"spec_json"} File::Raw::JSON::file_json_encode($self->{"api"}->spec)
Truth table33%1/3
A //= B
A= $self->{"spec_json"}B= File::Raw::JSON::file_json_encode($self->{"api"}->spec)
ABresult
1X1
011
000
238 }
239  
240 sub asset {
241 26 ▶ my ($self, $name) = @_;
Subroutine asset: called CC=5 SCAR=16.4
Pod documented
242 26 ▶ return unless defined $name && $ASSETS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $ASSETS{$name} 2 24 26
Condition66%2/3
defined $name and $ASSETS{$name}
!ll&&!rl&&r
0224
MC/DC50%1/2
defined $name and $ASSETS{$name}
defined $name $ASSETS{$name}
Truth table66%2/3
A and B
A= defined $nameB= $ASSETS{$name}
ABresult
0X0
100
111
243 24 ▶ unless (defined $self->{assets}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"assets"}{$name}) 24 0 24
244 24 my $file = _base_dir() . "/static/$name";
245 24 ▶ open my $fh, '<:raw', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or Carp::croak("Open::API::UI: cannot read ${file}: $!") 0 24 24
246 or Carp::croak("Open::API::UI: cannot read $file: $!");
247 24 local $/;
248 24 $self->{assets}{$name} = <$fh>;
249 }
250 24 return ($ASSETS{$name}, $self->{assets}{$name});
251 }
252  
253 2 ▶sub headers { [ @{ $_[0]{header_pairs} } ] }
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
254  
255 sub routes {
256 19 ▶ my ($self) = @_;
Subroutine routes: called CC=2 SCAR=6.9
Pod documented
257 19 ▶ return $self->{routes} if $self->{routes};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"routes"} 8 11 19
258 11 my $hdrs = $self->{header_pairs};
259 my $static = sub {
260 44 ▶ my ($ct, $bytes) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
261 return sub {
262 23 ▶ [ 200,
Subroutine __ANON__: called CC=1 SCAR=0.0
263 [ 'Content-Type' => $ct,
264 'Content-Length' => length $bytes,
265 @$hdrs ],
266 [ $bytes ] ];
267 44 };
268 11 };
269 11 my $html = $static->('text/html; charset=utf-8', $self->index_html);
270 11 my ($css_ct, $css) = $self->asset('app.css');
271 11 my ($js_ct, $js) = $self->asset('app.js');
272 11 my $p = $self->{path};
273 return $self->{routes} = [
274 { method => 'GET', path => $p, response => $html },
275 { method => 'GET', path => "$p/", response => $html },
276 { method => 'GET', path => "$p/app.js", response => $static->($js_ct, $js) },
277 { method => 'GET', path => "$p/app.css", response => $static->($css_ct, $css) },
278 { method => 'GET', path => $self->{spec_path},
279 11 response => $static->('application/json', $self->spec_json) },
280 ];
281 }
282  
283 sub _table {
284 9 ▶ my ($self) = @_;
Subroutine _table: called CC=1 SCAR=0.0
285 9 return { map { $_->{path} => $_->{response} } @{ $self->routes } };
286 }
287  
288 sub to_app {
289 1 ▶ my ($self) = @_;
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
290 1 my $table = $self->_table;
291 return sub {
292 4 ▶ my $env = shift;
Subroutine __ANON__: called CC=7 SCAR=19.5
293 4 ▶ my $m = $env->{REQUEST_METHOD} || 'GET';
Condition50%1/2
$env->{'REQUEST_METHOD'} || 'GET'
l!l
40
MC/DC0%0/1
$env->{'REQUEST_METHOD'} || 'GET'
$env->{'REQUEST_METHOD'}
Truth table50%1/2
A
A= $env->{'REQUEST_METHOD'}
Aresult
11
00
294 4 ▶ if (($m eq 'GET' || $m eq 'HEAD')
Branch: 1 branch
BranchTrueFalseTotal
if ($m eq "GET" || $m eq "HEAD" and my $r = $$table{$env->{"PATH_INFO"} or "/"}) 2 2 4
Condition87%7/8
$m eq "GET" || $m eq "HEAD" and my $r = $$table{$env->{"PATH_INFO"} or "/"}
!ll&&!rl&&r
112
$m eq "GET" or $m eq "HEAD"
l!l&&r!l&&!r
211
$env->{'PATH_INFO'} || '/'
l!l
30
MC/DC100%3/3
$m eq "GET" || $m eq "HEAD" and my $r = $$table{$env->{"PATH_INFO"} or "/"}
$m eq "GET" $m eq "HEAD" my $r = $$table{$env->{"PATH_INFO"} or "/"}
MC/DC0%0/1
$env->{'PATH_INFO'} || '/'
$env->{'PATH_INFO'}
Truth table80%4/5
A or B and C
A= $m eq "GET"B= $m eq "HEAD"C= my $r = $$table{$env->{"PATH_INFO"} or "/"}
ABCresult
00X0
1X00
0100
1X11
0111
Truth table50%1/2
A
A= $env->{'PATH_INFO'}
Aresult
11
00
295 and my $r = $table->{ $env->{PATH_INFO} || '/' }) {
296 2 my $resp = $r->();
297 2 ▶ $resp->[2] = [''] if $m eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "HEAD" 1 1 2
298 2 return $resp;
299 }
300 2 return [ 404, [ 'Content-Type' => 'text/plain' ], ['Not Found'] ];
301 1 };
302 }
303  
304 sub _wrap_psgi {
305 9 ▶ my ($plack, $app) = @_;
Subroutine _wrap_psgi: called CC=9 SCAR=22.1
306 9 my $ui_opt = $plack->ui;
307 9 ▶ my %opts = ref $ui_opt eq 'HASH' ? %$ui_opt : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $ui_opt eq 'HASH' ? : 2 7 9
308  
309 9 ▶ my $api = $plack->api
Branch: 1 branch
BranchTrueFalseTotal
my $api = $plack->api or Carp::croak("Open::API::UI: the Plack object has no api") 0 9 9
310 or Carp::croak("Open::API::UI: the Plack object has no api");
311  
312 # The JS must speak the server's actual CSRF dialect, so the names come
313 # from the Plack csrf option, never from ui options.
314 9 my $csrf = $plack->csrf;
315 9 ▶ if (ref $csrf eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $csrf eq 'HASH') { } 1 8 9
316 $opts{csrf} = {
317 header => defined $csrf->{header} ? $csrf->{header} : 'X-CSRF-Token',
318 1 ▶ cookie => defined $csrf->{cookie} ? $csrf->{cookie} : 'csrf',
Branch: 2 branches
BranchTrueFalseTotal
defined $csrf->{'header'} ? : 1 0 1
defined $csrf->{'cookie'} ? : 1 0 1
319 };
320 } else {
321 8 $opts{csrf} = 0;
322 }
323  
324 9 my $ui = Open::API::UI->new(api => $api, %opts);
325  
326 9 for my $r (@{ $ui->routes }) {
327 41 my @m = $api->match($r->{method}, $r->{path});
328 41 ▶ next unless @m;
Branch: 1 branch
BranchTrueFalseTotal
unless @m 40 1 41
329 1 ▶ Carp::croak(defined $m[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $m[0] ? : 1 0 1
330 ? "Open::API::UI: path '$r->{path}' is declared by spec "
331 . "operation '$m[0]'; choose another ui path"
332 : "Open::API::UI: path '$r->{path}' is declared by the spec; "
333 . "choose another ui path");
334 }
335  
336 8 my $table = $ui->_table;
337 return sub {
338 20 ▶ my $env = shift;
Subroutine __ANON__: called CC=7 SCAR=19.5
339 20 ▶ my $m = $env->{REQUEST_METHOD} || 'GET';
Condition50%1/2
$env->{'REQUEST_METHOD'} || 'GET'
l!l
200
MC/DC0%0/1
$env->{'REQUEST_METHOD'} || 'GET'
$env->{'REQUEST_METHOD'}
Truth table50%1/2
A
A= $env->{'REQUEST_METHOD'}
Aresult
11
00
340 20 ▶ if (($m eq 'GET' || $m eq 'HEAD')
Branch: 1 branch
BranchTrueFalseTotal
if ($m eq "GET" || $m eq "HEAD" and my $r = $$table{$env->{"PATH_INFO"} or "/"}) 14 6 20
Condition87%7/8
$m eq "GET" || $m eq "HEAD" and my $r = $$table{$env->{"PATH_INFO"} or "/"}
!ll&&!rl&&r
1514
$m eq "GET" or $m eq "HEAD"
l!l&&r!l&&!r
1811
$env->{'PATH_INFO'} || '/'
l!l
190
MC/DC100%3/3
$m eq "GET" || $m eq "HEAD" and my $r = $$table{$env->{"PATH_INFO"} or "/"}
$m eq "GET" $m eq "HEAD" my $r = $$table{$env->{"PATH_INFO"} or "/"}
MC/DC0%0/1
$env->{'PATH_INFO'} || '/'
$env->{'PATH_INFO'}
Truth table80%4/5
A or B and C
A= $m eq "GET"B= $m eq "HEAD"C= my $r = $$table{$env->{"PATH_INFO"} or "/"}
ABCresult
00X0
1X00
0100
1X11
0111
Truth table50%1/2
A
A= $env->{'PATH_INFO'}
Aresult
11
00
341 and my $r = $table->{ $env->{PATH_INFO} || '/' }) {
342 14 my $resp = $r->();
343 14 ▶ $resp->[2] = [''] if $m eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "HEAD" 1 13 14
344 14 return $resp;
345 }
346 6 return $app->($env);
347 8 };
348 }
349  
350 3 1;
351  
352 __END__
↑ Summary blib/arch/Open/API/Install/Files.pm »