blib/lib/Plack/App/DAIA.pm

filter: Statementstmt 99.0% 109 / 110 Branchbran 94.8% 55 / 58 Conditioncond 81.5% 31 / 38 MC/DCmcdc 66.6% 14 / 21 Subroutinesub 100% 23 / 23 Podpod 83.3% 5 / 6 totaltotal 92.5% 237 / 256 CC 44 SCAR 23.4
CC
44
Cov
95%
call
20.1
prepare_app
11.1
as_psgi
11.1
CRAP 10.4
« blib/lib/Plack/App/DAIA/Test/Suite.pm ↑ Summary blib/lib/Plack/App/DAIA/Test.pm »
1 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Plack::App::DAIA;
4 #ABSTRACT: DAIA Server as Plack application
5 9 our $VERSION = '0.55'; #VERSION
6 9 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use parent 'Plack::Component';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use LWP::Simple qw(get);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use DAIA;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Plack::Util::Accessor qw(xslt root warnings errors code idformat initialized safe);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Plack::Middleware::Static;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 our %FORMATS = DAIA->formats;
23  
24 27 ▶sub prepare_app {
Subroutine prepare_app: called CC=11 SCAR=24.1
Pod documented
25 27 my $self = shift;
26 27 ▶ return if $self->initialized;
Branch: 1 branch
BranchTrueFalseTotal
if $self->initialized 11 16 27
27  
28 16 $self->init;
29 16 ▶ $self->errors(0) unless defined $self->errors;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->errors 15 1 16
30 16 ▶ $self->warnings(1) if $self->errors or not defined $self->warnings;
Branch: 1 branch
BranchTrueFalseTotal
if $self->errors or not defined $self->warnings 14 2 16
Condition100%3/3
$self->errors or not defined $self->warnings
l!l&&r!l&&!r
1132
MC/DC100%2/2
$self->errors or not defined $self->warnings
$self->errors not defined $self->warnings
Truth table100%3/3
A or not(B)
A= $self->errorsB= not defined $self->warnings
ABresult
1X1
011
000
31 16 ▶ $self->idformat( qr{^.*$} ) unless defined $self->idformat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->idformat 9 7 16
32 16 ▶ $self->safe(1) unless defined $self->safe;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->safe 16 0 16
33 16 ▶ $self->xslt('daia.xsl') if ($self->xslt // 1) eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->xslt // 1) eq 1 14 2 16
Condition100%2/2
$self->xslt // 1
l!l
79
MC/DC100%1/1
$self->xslt // 1
$self->xslt
Truth table100%2/2
A
A= $self->xslt
Aresult
11
00
34  
35 16 ▶ $self->{client} = Plack::Middleware::Static->new(
Branch: 1 branch
BranchTrueFalseTotal
if $self->xslt 15 1 16
Condition33%1/3
$self->root || dist_dir('Plack-App-DAIA')
l!l&&r!l&&!r
0150
MC/DC0%0/2
$self->root || dist_dir('Plack-App-DAIA')
$self->root dist_dir('Plack-App-DAIA')
Truth table33%1/3
A || B
A= $self->rootB= dist_dir('Plack-App-DAIA')
ABresult
1X1
011
000
36 path => qr{daia\.(xsl|css|xsd)$|xmlverbatim\.xsl$|icons/[a-z0-9_-]+\.png$},
37 root => ($self->root || dist_dir('Plack-App-DAIA'))
38 ) if $self->xslt;
39  
40 16 $self->initialized(1);
41 }
42  
43 9 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
44 # initialization hook
45 }
46  
47 29 ▶sub call_client {
Subroutine call_client: called CC=4 SCAR=13.9
Pod undocumented
48 29 my ($self, $req) = @_;
49  
50 29 ▶ if ( $self->{client} and $req->path ne '/' and !keys %{$req->parameters} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'client'} and $req->path ne '/' and not keys %{$req->parameters;}) { } 3 26 29
Condition83%5/6
$self->{"client"} and $req->path ne "/" and not keys %{$req->parameters;}
!ll&&!rl&&r
2603
$self->{"client"} and $req->path ne "/"
!ll&&!rl&&r
1253
MC/DC66%2/3
$self->{"client"} and $req->path ne "/" and not keys %{$req->parameters;}
$self->{"client"} $req->path ne "/" not keys %{$req->parameters;}
Truth table75%3/4
A and B and not(C)
A= $self->{"client"}B= $req->path ne "/"C= not keys %{$req->parameters;}
ABCresult
0XX0
10X0
1100
1111
51 3 return $self->{client}->_handle_static( $req->env );
52 } else {
53 26 return;
54 }
55 }
56  
57 27 ▶sub call {
Subroutine call: called CC=20 SCAR=30.0
Pod documented
58 27 my ($self, $env) = @_;
59 27 my $req = Plack::Request->new($env);
60  
61 27 ▶ my $id = $req->param('id') // '';
Condition100%2/2
$req->param("id") // ""
l!l
216
MC/DC100%1/1
$req->param("id") // ""
$req->param("id")
Truth table100%2/2
A
A= $req->param("id")
Aresult
11
00
62 27 ▶ my $format = lc($req->param('format') // '');
Condition100%2/2
$req->param("format") // ""
l!l
189
MC/DC100%1/1
$req->param("format") // ""
$req->param("format")
Truth table100%2/2
A
A= $req->param("format")
Aresult
11
00
63  
64 # serve parts of the XSLT client
65 27 my $res = $self->call_client($req);
66 27 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 1 26 27
67  
68 # validate identifier
69 26 my ($invalid_id, $error, %parts) = ('',undef);
70 26 ▶ if ( $id ne '' and ref $self->idformat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id ne "" and ref $self->idformat) 20 6 26
Condition66%2/3
$id ne "" and ref $self->idformat
!ll&&!rl&&r
6020
MC/DC50%1/2
$id ne "" and ref $self->idformat
$id ne "" ref $self->idformat
Truth table66%2/3
A and B
A= $id ne ""B= ref $self->idformat
ABresult
0X0
100
111
71 20 ▶ if ( ref $self->idformat eq 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->idformat eq "Regexp") 20 0 20
72 20 ▶ if ( $id =~ $self->idformat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ $self->idformat) { } 17 3 20
73 17 %parts = %+; # named capturing groups
74 } else {
75 3 $invalid_id = $id;
76 3 $id = "";
77 }
78 }
79 }
80  
81 26 ▶ if ( $self->warnings ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->warnings) 25 1 26
82 25 ▶ if ( $invalid_id ne '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($invalid_id ne '') { } 2 23 25
elsif ($id eq '') { } 6 17 23
83 2 $error = 'unknown identifier format';
84 } elsif ( $id eq '' ) {
85 6 $error = 'please provide a document identifier';
86 }
87 }
88  
89 # retrieve and construct response
90 26 my ($status, $daia) = (200, undef);
91 26 ▶ if ( $error and $self->errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error and $self->errors) { } 1 25 26
Condition100%3/3
$error and $self->errors
!ll&&!rl&&r
1871
MC/DC100%2/2
$error and $self->errors
$error $self->errors
Truth table100%3/3
A and B
A= $errorB= $self->errors
ABresult
0X0
100
111
92 1 $daia = DAIA::Response->new;
93 } else {
94 25 ▶ if ($self->safe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->safe) { } 24 1 25
95 24 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 24 $daia = $self->retrieve( $id, %parts );
97 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 1 chomp($error = "request method died: $_");
99 1 $status = 500;
100 }
101 24 } else {
102 1 $daia = $self->retrieve( $id, %parts );
103 }
104 24 ▶ if (!$daia or !blessed $daia or !$daia->isa('DAIA::Response')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($daia and builtin::blessed($daia) and $daia->isa("DAIA::Response")) 14 10 24
Condition83%5/6
$daia and builtin::blessed($daia) and $daia->isa("DAIA::Response")
!ll&&!rl&&r
14010
$daia and builtin::blessed($daia)
!ll&&!rl&&r
13110
MC/DC66%2/3
$daia and builtin::blessed($daia) and $daia->isa("DAIA::Response")
$daia builtin::blessed($daia) $daia->isa("DAIA::Response")
Truth table75%3/4
A and B and C
A= $daiaB= builtin::blessed($daia)C= $daia->isa("DAIA::Response")
ABCresult
0XX0
10X0
1100
1111
105 14 $daia = DAIA::Response->new;
106 14 ▶ $error = 'request method did not return a DAIA response'
Branch: 1 branch
BranchTrueFalseTotal
unless $error 6 8 14
107 unless $error;
108 14 $status = 500;
109 }
110 }
111  
112 25 ▶ if ( $error and $self->warnings ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error and $self->warnings) 15 10 25
Condition66%2/3
$error and $self->warnings
!ll&&!rl&&r
10015
MC/DC50%1/2
$error and $self->warnings
$error $self->warnings
Truth table66%2/3
A and B
A= $errorB= $self->warnings
ABresult
0X0
100
111
113 15 $daia->addMessage( 'en' => $error, errno => 400 );
114 }
115  
116 25 $self->as_psgi( $status, $daia, $format, $req->param('callback') );
117 }
118  
119 21 ▶sub retrieve {
Subroutine retrieve: called CC=2 SCAR=6.9
Pod documented
120 21 my $self = shift;
121 21 ▶ return $self->code ? $self->code->(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->code ? : 10 11 21
122 }
123  
124 25 ▶sub as_psgi {
Subroutine as_psgi: called CC=11 SCAR=24.1
Pod documented
125 25 my ($self, $status, $daia, $format, $callback) = @_;
126 25 my ($content, $type);
127  
128 25 ▶ $type = $FORMATS{$format} unless $format eq 'xml';
Branch: 1 branch
BranchTrueFalseTotal
unless $format eq "xml" 12 13 25
129 25 ▶ $content = $daia->serialize($format) if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 4 21 25
130  
131 25 ▶ if (!$content) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $content) { } 21 4 25
elsif ($type =~ qr"^application/javascript" and ($callback || '') =~ /^[\w\.\[\]]+$/) { } 1 3 4
132 21 $type = "application/xml; charset=utf-8";
133 21 ▶ if ( $self->warnings ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->warnings) 20 1 21
134 20 ▶ if ( not $format ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $format) { } 8 12 20
elsif ($format ne 'xml') { } 0 12 12
135 8 $daia->addMessage( 'en' => 'please provide an explicit parameter format=xml', 300 );
136 } elsif ( $format ne 'xml' ) {
137 0 $daia->addMessage( 'en' => 'unknown or unsupported format', 300 );
138 }
139 }
140 21 ▶ $content = $daia->xml( header => 1, xmlns => 1, ( $self->xslt ? (xslt => $self->xslt) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->xslt ? : 20 1 21
141 ▶ } elsif ( $type =~ qr{^application/javascript} and ($callback || '') =~ /^[\w\.\[\]]+$/ ) {
Condition80%4/5
$type =~ qr"^application/javascript" and ($callback || "") =~ /^[\w\.\[\]]+$/
!ll&&!rl&&r
031
$callback || ''
l!l
13
MC/DC50%1/2
$type =~ qr"^application/javascript" and ($callback || "") =~ /^[\w\.\[\]]+$/
$type =~ qr"^application/javascript" $callback
Truth table66%2/3
A and B
A= $type =~ qr"^application/javascript"B= $callback
ABresult
0X0
100
111
142 1 $content = "$callback($content)";
143 }
144  
145 25 return [ $status, [ "Content-Type" => $type ], [ encode('utf8',$content) ] ];
146 }
147  
148 9 1;
149  
150 __END__
« blib/lib/Plack/App/DAIA/Test/Suite.pm ↑ Summary blib/lib/Plack/App/DAIA/Test.pm »