PONAPI/Server.pm

filter: Statementstmt 99.4% 193 / 194 Branchbran 79.6% 102 / 128 Conditioncond 74.0% 74 / 100 MC/DCmcdc 50.9% 28 / 55 Subroutinesub 100% 25 / 25 Podpod 100% 2 / 2 totaltotal 84.1% 424 / 504 CC 105 SCAR 30.3
CC
105
Cov
87%
_ponapi_route_match
40.4
_validate_data_members
25.2
_ponapi_query_params
15.8
CRAP 20.7
« PONAPI/CLI/Command/demo.pm ↑ Summary PONAPI/CLI/Command/gen.pm »
1 # ABSTRACT: PONAPI - Perl implementation of {JSON:API} (http://jsonapi.org/) v1.0
2 package PONAPI::Server;
3  
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  
7 3 our $VERSION = '0.002012';
8  
9 3 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use HTTP::Headers::ActionPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Return::MultiLevel ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use URI::Escape qw( uri_unescape );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use PONAPI::Server::ConfigReader;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use PONAPI::Utils::Names qw( check_name );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use parent 'Plack::Component';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 use constant {
23 3 ERR_MISSING_MEDIA_TYPE => +{ __error__ => +[ 415, "{JSON:API} No {json:api} Media-Type (Content-Type / Accept)" ] },
24 ERR_MISSING_CONTENT_TYPE => +{ __error__ => +[ 415, "{JSON:API} Missing Content-Type header" ] },
25 ERR_WRONG_CONTENT_TYPE => +{ __error__ => +[ 415, "{JSON:API} Invalid Content-Type header" ] },
26 ERR_WRONG_HEADER_ACCEPT => +{ __error__ => +[ 406, "{JSON:API} Invalid Accept header" ] },
27 ERR_BAD_REQ => +{ __error__ => +[ 400, "{JSON:API} Bad request" ] },
28 ERR_BAD_REQ_INVALID_NAME => +{ __error__ => +[ 400, "{JSON:API} Bad request (invalid member-name)" ] },
29 ERR_BAD_REQ_PARAMS => +{ __error__ => +[ 400, "{JSON:API} Bad request (unsupported parameters)" ] },
30 ERR_SORT_NOT_ALLOWED => +{ __error__ => +[ 400, "{JSON:API} Server-side sorting not allowed" ] },
31 ERR_NO_MATCHING_ROUTE => +{ __error__ => +[ 404, "{JSON:API} No matching route" ] },
32 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 3 my $qr_member_name_prefix = qr/^[a-zA-Z0-9]/;
35  
36 sub prepare_app {
37 9 ▶ my $self = shift;
Subroutine prepare_app: called CC=6 SCAR=18.4
Pod documented
38  
39 9 my %conf;
40 9 local $@;
41 9 eval {
42 %conf = PONAPI::Server::ConfigReader->new(
43 9 ▶ dir => $self->{'ponapi.config_dir'} || 'conf'
Condition50%1/2
$self->{'ponapi.config_dir'} || 'conf'
l!l
09
MC/DC0%0/1
$self->{'ponapi.config_dir'} || 'conf'
$self->{'ponapi.config_dir'}
Truth table50%1/2
A
A= $self->{'ponapi.config_dir'}
Aresult
11
00
44 )->read_config;
45 };
46 9 ▶ $self->{$_} //= $conf{$_} for keys %conf;
Condition100%3/3
$self->{$_} //= $conf{$_}
l!l&&r!l&&!r
2799
MC/DC100%2/2
$self->{$_} //= $conf{$_}
$self->{$_} $conf{$_}
Truth table100%3/3
A //= B
A= $self->{$_}B= $conf{$_}
ABresult
1X1
011
000
47  
48 # Some defaults
49 9 my $default_media_type = 'application/vnd.api+json';
50 9 ▶ $self->{'ponapi.spec_version'} //= '1.0';
Condition50%1/2
$self->{"ponapi.spec_version"} //= "1.0"
l!l
90
MC/DC0%0/1
$self->{"ponapi.spec_version"} //= "1.0"
$self->{"ponapi.spec_version"}
Truth table50%1/2
A
A= $self->{"ponapi.spec_version"}
Aresult
11
00
51 9 ▶ $self->{'ponapi.mediatype'} //= $default_media_type;
Condition33%1/3
$self->{"ponapi.mediatype"} //= $default_media_type
l!l&&r!l&&!r
900
MC/DC0%0/2
$self->{"ponapi.mediatype"} //= $default_media_type
$self->{"ponapi.mediatype"} $default_media_type
Truth table33%1/3
A //= B
A= $self->{"ponapi.mediatype"}B= $default_media_type
ABresult
1X1
011
000
52  
53 9 $self->_load_dao();
54 }
55  
56 sub call {
57 50 ▶ my ( $self, $env ) = @_;
Subroutine call: called CC=2 SCAR=6.9
Pod documented
58 50 my $req = Plack::Request->new($env);
59  
60 my $ponapi_params = Return::MultiLevel::with_return {
61 50 ▶ $self->_ponapi_params( shift, $req )
Subroutine __ANON__: called CC=1 SCAR=0.0
62 50 };
63  
64 return $self->_error_response( $ponapi_params->{__error__} )
65 50 ▶ if $ponapi_params->{__error__};
Branch: 1 branch
BranchTrueFalseTotal
if $ponapi_params->{"__error__"} 25 25 50
66  
67 25 my $action = delete $ponapi_params->{action};
68 25 my ( $status, $headers, $res ) = $self->{'ponapi.DAO'}->$action($ponapi_params);
69 25 return $self->_response( $status, $headers, $res );
70 }
71  
72  
73 ### ...
74  
75 sub _load_dao {
76 9 ▶ my $self = shift;
Subroutine _load_dao: called CC=2 SCAR=7.0
77  
78 my $repository =
79 9 ▶ Module::Runtime::use_module( $self->{'repository.class'} )->new( @{ $self->{'repository.args'} } )
Condition50%1/2
&Module::Runtime::use_module($self->{'repository.class'})->new(@{$$self{'repository.args'};}) || die("[PONAPI Server] failed to create a repository object\n")
l!l
80
MC/DC0%0/1
&Module::Runtime::use_module($self->{'repository.class'})->new(@{$$self{'repository.args'};}) || die("[PONAPI Server] failed to create a repository object\n")
&Module::Runtime::use_module($self->{'repository.class'})->new(@{$$self{'repository.args'};})
Truth table50%1/2
A
A= &Module::Runtime::use_module($self->{'repository.class'})->new(@{$$self{'repository.args'};})
Aresult
11
00
80 || die "[PONAPI Server] failed to create a repository object\n";
81  
82 $self->{'ponapi.DAO'} = PONAPI::DAO->new(
83 repository => $repository,
84 8 version => $self->{'ponapi.spec_version'},
85 );
86 }
87  
88 sub _ponapi_params {
89 50 ▶ my ( $self, $wr, $req ) = @_;
Subroutine _ponapi_params: called CC=4 SCAR=13.9
90  
91 # THE HEADERS
92 50 $self->_ponapi_check_headers($wr, $req);
93  
94 # THE PATH --> route matching
95 47 my @ponapi_route_params = $self->_ponapi_route_match($wr, $req);
96  
97 # THE QUERY
98 41 my @ponapi_query_params = $self->_ponapi_query_params($wr, $req);
99  
100 # THE BODY CONTENT
101 33 my @ponapi_data = $self->_ponapi_data($wr, $req);
102  
103 # misc.
104 25 ▶ my $req_base = $self->{'ponapi.relative_links'} eq 'full' ? "".$req->base : '/';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ponapi.relative_links'} eq 'full' ? : 0 25 25
105 25 ▶ my $req_path = $self->{'ponapi.relative_links'} eq 'full' ? "".$req->uri : $req->request_uri;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ponapi.relative_links'} eq 'full' ? : 0 25 25
106 25 my $update_200 = !!$self->{'ponapi.respond_to_updates_with_200'};
107 25 ▶ my $doc_self_link = ($req->method eq 'GET') ? !!$self->{'ponapi.doc_auto_self_link'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$req->method eq 'GET' ? : 18 7 25
108  
109 25 my %params = (
110 @ponapi_route_params,
111 @ponapi_query_params,
112 @ponapi_data,
113 req_base => $req_base,
114 req_path => $req_path,
115 respond_to_updates_with_200 => $update_200,
116 send_doc_self_link => $doc_self_link,
117 );
118  
119 25 return \%params;
120 }
121  
122 sub _ponapi_route_match {
123 47 ▶ my ( $self, $wr, $req ) = @_;
Subroutine _ponapi_route_match: called CC=39 SCAR=37.0
124 47 my $method = $req->method;
125  
126 47 ▶ $wr->(ERR_BAD_REQ) unless grep { $_ eq $method } qw< GET POST PATCH DELETE >;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $method;} "GET", "POST", "PATCH", "DELETE" 0 47 47
127  
128 47 my ( $type, $id, $relationships, $rel_type ) = split '/' => substr($req->path_info,1);
129  
130 # validate `type`
131 47 ▶ $wr->(ERR_BAD_REQ) unless defined $type and $type =~ /$qr_member_name_prefix/ ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and $type =~ /$qr_member_name_prefix/ 3 44 47
Condition100%3/3
defined $type and $type =~ /$qr_member_name_prefix/
!ll&&!rl&&r
2144
MC/DC100%2/2
defined $type and $type =~ /$qr_member_name_prefix/
defined $type $type =~ /$qr_member_name_prefix/
Truth table100%3/3
A and B
A= defined $typeB= $type =~ /$qr_member_name_prefix/
ABresult
0X0
100
111
132  
133 # validate `rel_type`
134 44 ▶ if ( defined $rel_type ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rel_type) { } 7 37 44
elsif ($relationships) { } 1 36 37
135 7 ▶ $wr->(ERR_BAD_REQ) if $relationships ne 'relationships';
Branch: 1 branch
BranchTrueFalseTotal
if $relationships ne "relationships" 0 7 7
136 }
137 elsif ( $relationships ) {
138 1 $rel_type = $relationships;
139 1 undef $relationships;
140 }
141  
142 44 my $def_rel_type = defined $rel_type;
143  
144 44 ▶ $wr->(ERR_BAD_REQ) if $def_rel_type and $rel_type !~ /$qr_member_name_prefix/;
Branch: 1 branch
BranchTrueFalseTotal
if $def_rel_type and not $rel_type =~ /$qr_member_name_prefix/ 1 43 44
Condition100%3/3
$def_rel_type and not $rel_type =~ /$qr_member_name_prefix/
!ll&&!rl&&r
3671
MC/DC100%2/2
$def_rel_type and not $rel_type =~ /$qr_member_name_prefix/
$def_rel_type not $rel_type =~ /$qr_member_name_prefix/
Truth table100%3/3
A and not(B)
A= $def_rel_typeB= not $rel_type =~ /$qr_member_name_prefix/
ABresult
0X0
100
111
145  
146 # set `action`
147 43 my $action;
148 43 ▶ if ( defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 32 11 43
149 32 ▶ $action = 'create_relationships' if $method eq 'POST' and $relationships and $def_rel_type;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "POST" and $relationships and $def_rel_type 2 30 32
Condition83%5/6
$method eq "POST" and $relationships and $def_rel_type
!ll&&!rl&&r
3002
$method eq "POST" and $relationships
!ll&&!rl&&r
2822
MC/DC66%2/3
$method eq "POST" and $relationships and $def_rel_type
$method eq "POST" $relationships $def_rel_type
Truth table75%3/4
A and B and C
A= $method eq "POST"B= $relationshipsC= $def_rel_type
ABCresult
0XX0
10X0
1100
1111
150 32 ▶ $action = 'retrieve' if $method eq 'GET' and !$relationships and !$def_rel_type;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "GET" and not $relationships and not $def_rel_type 21 11 32
Condition100%6/6
$method eq "GET" and not $relationships and not $def_rel_type
!ll&&!rl&&r
10121
$method eq "GET" and not $relationships
!ll&&!rl&&r
8222
MC/DC100%3/3
$method eq "GET" and not $relationships and not $def_rel_type
$method eq "GET" not $relationships not $def_rel_type
Truth table100%4/4
A and not(B) and not(C)
A= $method eq "GET"B= not $relationshipsC= not $def_rel_type
ABCresult
0XX0
10X0
1100
1111
151 32 ▶ $action = 'retrieve_by_relationship' if $method eq 'GET' and !$relationships and $def_rel_type;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "GET" and not $relationships and $def_rel_type 1 31 32
Condition100%6/6
$method eq "GET" and not $relationships and $def_rel_type
!ll&&!rl&&r
10211
$method eq "GET" and not $relationships
!ll&&!rl&&r
8222
MC/DC100%3/3
$method eq "GET" and not $relationships and $def_rel_type
$method eq "GET" not $relationships $def_rel_type
Truth table100%4/4
A and not(B) and C
A= $method eq "GET"B= not $relationshipsC= $def_rel_type
ABCresult
0XX0
10X0
1100
1111
152 32 ▶ $action = 'retrieve_relationships' if $method eq 'GET' and $relationships and $def_rel_type;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "GET" and $relationships and $def_rel_type 2 30 32
Condition83%5/6
$method eq "GET" and $relationships and $def_rel_type
!ll&&!rl&&r
3002
$method eq "GET" and $relationships
!ll&&!rl&&r
8222
MC/DC66%2/3
$method eq "GET" and $relationships and $def_rel_type
$method eq "GET" $relationships $def_rel_type
Truth table75%3/4
A and B and C
A= $method eq "GET"B= $relationshipsC= $def_rel_type
ABCresult
0XX0
10X0
1100
1111
153 32 ▶ $action = 'update' if $method eq 'PATCH' and !$relationships and !$def_rel_type;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "PATCH" and not $relationships and not $def_rel_type 0 32 32
Condition50%3/6
$method eq "PATCH" and not $relationships and not $def_rel_type
!ll&&!rl&&r
3200
$method eq "PATCH" and not $relationships
!ll&&!rl&&r
3110
MC/DC0%0/3
$method eq "PATCH" and not $relationships and not $def_rel_type
$method eq "PATCH" not $relationships not $def_rel_type
Truth table50%2/4
A and not(B) and not(C)
A= $method eq "PATCH"B= not $relationshipsC= not $def_rel_type
ABCresult
0XX0
10X0
1100
1111
154 32 ▶ $action = 'update_relationships' if $method eq 'PATCH' and $relationships and $def_rel_type;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "PATCH" and $relationships and $def_rel_type 1 31 32
Condition66%4/6
$method eq "PATCH" and $relationships and $def_rel_type
!ll&&!rl&&r
3101
$method eq "PATCH" and $relationships
!ll&&!rl&&r
3101
MC/DC33%1/3
$method eq "PATCH" and $relationships and $def_rel_type
$method eq "PATCH" $relationships $def_rel_type
Truth table50%2/4
A and B and C
A= $method eq "PATCH"B= $relationshipsC= $def_rel_type
ABCresult
0XX0
10X0
1100
1111
155 32 ▶ $action = 'delete' if $method eq 'DELETE' and !$relationships and !$def_rel_type;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DELETE" and not $relationships and not $def_rel_type 2 30 32
Condition83%5/6
$method eq "DELETE" and not $relationships and not $def_rel_type
!ll&&!rl&&r
3002
$method eq "DELETE" and not $relationships
!ll&&!rl&&r
2912
MC/DC66%2/3
$method eq "DELETE" and not $relationships and not $def_rel_type
$method eq "DELETE" not $relationships not $def_rel_type
Truth table75%3/4
A and not(B) and not(C)
A= $method eq "DELETE"B= not $relationshipsC= not $def_rel_type
ABCresult
0XX0
10X0
1100
1111
156 32 ▶ $action = 'delete_relationships' if $method eq 'DELETE' and $relationships and $def_rel_type;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DELETE" and $relationships and $def_rel_type 1 31 32
Condition83%5/6
$method eq "DELETE" and $relationships and $def_rel_type
!ll&&!rl&&r
3101
$method eq "DELETE" and $relationships
!ll&&!rl&&r
2921
MC/DC66%2/3
$method eq "DELETE" and $relationships and $def_rel_type
$method eq "DELETE" $relationships $def_rel_type
Truth table75%3/4
A and B and C
A= $method eq "DELETE"B= $relationshipsC= $def_rel_type
ABCresult
0XX0
10X0
1100
1111
157 }
158 else {
159 11 ▶ $action = 'retrieve_all' if $method eq 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "GET" 2 9 11
160 11 ▶ $action = 'create' if $method eq 'POST';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "POST" 9 2 11
161 }
162  
163 43 ▶ $wr->(ERR_NO_MATCHING_ROUTE) unless $action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 2 41 43
164  
165 # return ( action, type, id?, rel_type? )
166 41 my @ret = ( action => $action, type => $type );
167 41 ▶ defined $id and push @ret => id => $id;
Branch: 1 branch
BranchTrueFalseTotal
defined $id and push @ret, "id", $id 30 11 41
168 41 ▶ $def_rel_type and push @ret => rel_type => $rel_type;
Branch: 1 branch
BranchTrueFalseTotal
$def_rel_type and push @ret, "rel_type", $rel_type 7 34 41
169 41 return @ret;
170 }
171  
172 sub _ponapi_check_headers {
173 50 ▶ my ( $self, $wr, $req ) = @_;
Subroutine _ponapi_check_headers: called CC=10 SCAR=23.2
174  
175 50 my $pack = HTTP::Headers::ActionPack->new;
176 50 my $mt = $self->{'ponapi.mediatype'};
177  
178 50 my $has_mediatype = 0;
179  
180 # check Content-Type
181 50 my $content_type = $req->headers->header('Content-Type');
182 50 ▶ if ( $content_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type) 49 1 50
183 49 ▶ $wr->(ERR_WRONG_CONTENT_TYPE) unless $content_type eq $mt;
Branch: 1 branch
BranchTrueFalseTotal
unless $content_type eq $mt 1 48 49
184 48 $has_mediatype++;
185 }
186  
187 49 ▶ $wr->(ERR_MISSING_CONTENT_TYPE)
Branch: 1 branch
BranchTrueFalseTotal
if $req->content_length and not $content_type 0 49 49
Condition66%2/3
$req->content_length and not $content_type
!ll&&!rl&&r
37120
MC/DC0%0/2
$req->content_length and not $content_type
$req->content_length not $content_type
Truth table66%2/3
A and not(B)
A= $req->content_lengthB= not $content_type
ABresult
0X0
100
111
188 if $req->content_length and !$content_type;
189  
190 # check Accept
191 49 ▶ if ( my $accept = $req->headers->header('Accept') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $accept = $req->headers->header("Accept")) 2 47 49
192 my @jsonapi_accept =
193 2 ▶ map { ( $_->[1]->type eq $mt ) ? $_->[1] : () }
Branch: 1 branch
BranchTrueFalseTotal
$_->[1]->type eq $mt ? : 2 0 2
194 $pack->create_header( 'Accept' => $accept )->iterable;
195  
196 2 ▶ if ( @jsonapi_accept ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@jsonapi_accept) 2 0 2
197 $wr->(ERR_WRONG_HEADER_ACCEPT)
198 2 ▶ unless grep { $_->params_are_empty } @jsonapi_accept;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->params_are_empty;} @jsonapi_accept 1 1 2
199  
200 1 $has_mediatype++;
201 }
202 }
203  
204 48 ▶ $wr->(ERR_MISSING_MEDIA_TYPE) unless $has_mediatype;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_mediatype 1 47 48
205 }
206  
207 sub _ponapi_query_params {
208 41 ▶ my ( $self, $wr, $req ) = @_;
Subroutine _ponapi_query_params: called CC=15 SCAR=27.6
209  
210 41 my %params;
211 41 my $query_params = $req->query_parameters;
212  
213 41 my $unesacpe_values = !!$req->headers->header('X-PONAPI-Escaped-Values');
214  
215 # loop over query parameters (unique keys)
216 41 for my $k ( keys %{ $query_params } ) {
217 18 my ( $p, $f ) = $k =~ /^ (\w+?) (?:\[(\w+)\])? $/x;
218  
219 # valid parameter names
220 $wr->(ERR_BAD_REQ_PARAMS)
221 18 ▶ unless grep { $p eq $_ } qw< fields filter page include sort >;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$p eq $_;} "fields", "filter", "page", "include", "sort" 0 18 18
222  
223 # "complex" parameters have the correct structre
224 $wr->(ERR_BAD_REQ)
225 18 ▶ if !defined $f and grep { $p eq $_ } qw< page fields filter >;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $f and grep {$p eq $_;} "page", "fields", "filter" 4 14 18
Condition100%3/3
not defined $f and grep {$p eq $_;} "page", "fields", "filter"
!ll&&!rl&&r
4104
MC/DC100%2/2
not defined $f and grep {$p eq $_;} "page", "fields", "filter"
not defined $f grep {$p eq $_;} "page", "fields", "filter"
Truth table100%3/3
not(A) and B
A= not defined $fB= grep {$p eq $_;} "page", "fields", "filter"
ABresult
0X0
100
111
226  
227 # 'sort' requested but not supported
228 $wr->(ERR_SORT_NOT_ALLOWED)
229 14 ▶ if $p eq 'sort' and !$self->{'ponapi.sort_allowed'};
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "sort" and not $self->{"ponapi.sort_allowed"} 0 14 14
Condition33%1/3
$p eq "sort" and not $self->{"ponapi.sort_allowed"}
!ll&&!rl&&r
1400
MC/DC0%0/2
$p eq "sort" and not $self->{"ponapi.sort_allowed"}
$p eq "sort" not $self->{"ponapi.sort_allowed"}
Truth table33%1/3
A and not(B)
A= $p eq "sort"B= not $self->{"ponapi.sort_allowed"}
ABresult
0X0
100
111
230  
231 # values can be passed as CSV
232 11 ▶ my @values = map { $unesacpe_values ? uri_unescape($_) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$unesacpe_values ? : 0 11 11
233 14 map { split /,/ } $query_params->get_all($k);
234  
235 # check we have values for a given key
236 # (for 'fields' an empty list is valid)
237 $wr->(ERR_BAD_REQ)
238 14 ▶ if $p ne 'fields' and exists $query_params->{$k} and !@values;
Branch: 1 branch
BranchTrueFalseTotal
if $p ne "fields" and exists $query_params->{$k} and not @values 4 10 14
Condition83%5/6
$p ne "fields" and exists $query_params->{$k} and not @values
!ll&&!rl&&r
374
$p ne "fields" and exists $query_params->{$k}
!ll&&!rl&&r
3011
MC/DC66%2/3
$p ne "fields" and exists $query_params->{$k} and not @values
$p ne "fields" exists $query_params->{$k} not @values
Truth table75%3/4
A and B and not(C)
A= $p ne "fields"B= exists $query_params->{$k}C= not @values
ABCresult
0XX0
10X0
1100
1111
239  
240 # values passed on in array-ref
241 20 grep { $p eq $_ } qw< fields filter >
242 10 ▶ and $params{$p}{$f} = \@values;
Branch: 1 branch
BranchTrueFalseTotal
grep {$p eq $_;} "fields", "filter" and $params{$p}{$f} = \@values 3 7 10
243  
244 # page info has one value per request
245 10 ▶ $p eq 'page' and $params{$p}{$f} = $values[0];
Branch: 1 branch
BranchTrueFalseTotal
$p eq "page" and $params{$p}{$f} = $values[0] 0 10 10
246  
247 # values passed on in hash-ref
248 10 ▶ $p eq 'include' and $params{include} = \@values;
Branch: 1 branch
BranchTrueFalseTotal
$p eq "include" and $params{"include"} = \@values 7 3 10
249  
250 # sort values: indicate direction
251 # Not doing any processing here to allow repos to support
252 # complex sorting, if they want to.
253 10 ▶ $p eq 'sort' and $params{'sort'} = \@values;
Branch: 1 branch
BranchTrueFalseTotal
$p eq "sort" and $params{"sort"} = \@values 0 10 10
254 }
255  
256 33 return %params;
257 }
258  
259 sub _ponapi_data {
260 33 ▶ my ( $self, $wr, $req ) = @_;
Subroutine _ponapi_data: called CC=10 SCAR=23.9
261  
262 33 ▶ return unless $req->content_length and $req->content_length > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $req->content_length and $req->content_length > 0 21 12 33
Condition66%2/3
$req->content_length and $req->content_length > 0
!ll&&!rl&&r
21012
MC/DC50%1/2
$req->content_length and $req->content_length > 0
$req->content_length $req->content_length > 0
Truth table66%2/3
A and B
A= $req->content_lengthB= $req->content_length > 0
ABresult
0X0
100
111
263  
264 12 ▶ $wr->(ERR_BAD_REQ) if $req->method eq 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $req->method eq "GET" 0 12 12
265  
266 12 my $body;
267 12 eval { $body = JSON::XS::decode_json( $req->content ); 1 };
268  
269 12 ▶ $wr->(ERR_BAD_REQ) unless $body and ref $body eq 'HASH' and exists $body->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless $body and ref $body eq "HASH" and exists $body->{"data"} 0 11 11
Condition66%4/6
$body and ref $body eq "HASH" and exists $body->{"data"}
!ll&&!rl&&r
1011
$body and ref $body eq "HASH"
!ll&&!rl&&r
1011
MC/DC33%1/3
$body and ref $body eq "HASH" and exists $body->{"data"}
$body ref $body eq "HASH" exists $body->{"data"}
Truth table50%2/4
A and B and C
A= $bodyB= ref $body eq "HASH"C= exists $body->{"data"}
ABCresult
0XX0
10X0
1100
1111
270  
271 11 my $data = $body->{data};
272  
273 11 ▶ $wr->(ERR_BAD_REQ) unless !defined $data or ref($data) =~ /^(?:ARRAY|HASH)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $data or ref($data) =~ /^(?:ARRAY|HASH)$/ 1 10 11
Condition66%2/3
not defined $data or ref($data) =~ /^(?:ARRAY|HASH)$/
l!l&&r!l&&!r
0101
MC/DC50%1/2
not defined $data or ref($data) =~ /^(?:ARRAY|HASH)$/
not defined $data ref($data) =~ /^(?:ARRAY|HASH)$/
Truth table66%2/3
not(A) or B
A= not defined $dataB= ref($data) =~ /^(?:ARRAY|HASH)$/
ABresult
1X1
011
000
274  
275 10 ▶ $self->_validate_data_members( $wr, $data ) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 10 0 10
276  
277 4 return ( data => $data );
278 }
279  
280 sub _validate_data_members {
281 10 ▶ my ( $self, $wr, $data ) = @_;
Subroutine _validate_data_members: called CC=22 SCAR=32.3
282  
283 10 ▶ my @recs = ref $data eq 'ARRAY' ? @{$data} : $data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 2 8 10
284  
285 10 for my $r ( @recs ) {
286 10 ▶ return unless keys %{$r};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$r;} 0 10 10
287  
288 # `type`
289 10 ▶ $wr->(ERR_BAD_REQ) unless $r->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"type"} 0 10 10
290 10 ▶ $wr->(ERR_BAD_REQ_INVALID_NAME) unless check_name( $r->{type} );
Branch: 1 branch
BranchTrueFalseTotal
unless check_name($r->{"type"}) 1 9 10
291  
292 # `attributes`
293 9 ▶ if ( exists $r->{attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $r->{"attributes"}) 3 6 9
294 3 ▶ $wr->(ERR_BAD_REQ) unless ref( $r->{attributes} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r->{"attributes"} eq "HASH" 1 2 3
295 $wr->(ERR_BAD_REQ_INVALID_NAME)
296 2 ▶ if grep { !check_name($_) } keys %{ $r->{attributes} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {not check_name($_);} keys %{$$r{"attributes"};} 1 1 2
297 }
298  
299 # `relationships`
300 7 ▶ if ( exists $r->{relationships} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $r->{"relationships"}) 3 4 7
301 3 ▶ $wr->(ERR_BAD_REQ) unless ref( $r->{relationships} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r->{"relationships"} eq "HASH" 1 2 3
302  
303 2 for my $k ( keys %{ $r->{relationships} } ) {
304 2 ▶ $wr->(ERR_BAD_REQ_INVALID_NAME) unless check_name($k);
Branch: 1 branch
BranchTrueFalseTotal
unless check_name($k) 1 1 2
305  
306 1 my $rel = $r->{relationships}{$k};
307 1 ▶ my @rels = ref($rel||'') eq 'ARRAY' ? @$rel : $rel;
Branch: 1 branch
BranchTrueFalseTotal
ref($rel || '') eq 'ARRAY' ? : 0 1 1
Condition50%1/2
$rel || ''
l!l
10
MC/DC0%0/1
$rel || ''
$rel
Truth table50%1/2
A
A= $rel
Aresult
11
00
308 1 foreach my $relationship ( @rels ) {
309 1 ▶ next if !defined $relationship;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $relationship 0 1 1
310 # Some requests have relationships => { blah },
311 # others have relationships => { data => { blah } }
312 $relationship = $relationship->{data}
313 1 ▶ if exists $relationship->{data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $relationship->{"data"} 1 0 1
314  
315 1 ▶ $wr->(ERR_BAD_REQ)
Branch: 1 branch
BranchTrueFalseTotal
if ref $relationship ne "HASH" 0 1 1
316 if ref( $relationship ) ne 'HASH';
317 $wr->(ERR_BAD_REQ)
318 1 ▶ if !exists $relationship->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $relationship->{"type"} 0 1 1
319  
320 $wr->(ERR_BAD_REQ_INVALID_NAME)
321 if !check_name( $relationship->{type} )
322 1 ▶ or grep { !check_name($_) } keys %$relationship;
Branch: 1 branch
BranchTrueFalseTotal
if not check_name($relationship->{"type"}) or grep {not check_name($_);} keys %$relationship 1 0 1
Condition33%1/3
not check_name($relationship->{"type"}) or grep {not check_name($_);} keys %$relationship
l!l&&r!l&&!r
100
MC/DC0%0/2
not check_name($relationship->{"type"}) or grep {not check_name($_);} keys %$relationship
not check_name($relationship->{"type"}) grep {not check_name($_);} keys %$relationship
Truth table33%1/3
not(A) or B
A= not check_name($relationship->{"type"})B= grep {not check_name($_);} keys %$relationship
ABresult
1X1
011
000
323 }
324 }
325 }
326 }
327 }
328  
329 sub _response {
330 50 ▶ my ( $self, $status, $headers, $content ) = @_;
Subroutine _response: called CC=4 SCAR=14.1
331 50 ▶ my $res = Plack::Response->new( $status || 200 );
Condition50%1/2
$status || 200
l!l
500
MC/DC0%0/1
$status || 200
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
332  
333 50 $res->headers($headers);
334 50 $res->content_type( $self->{'ponapi.mediatype'} );
335 $res->header( 'X-PONAPI-Server-Version' => $self->{'ponapi.spec_version'} )
336 50 ▶ if $self->{'ponapi.send_version_header'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ponapi.send_version_header"} 50 0 50
337 50 ▶ if ( ref $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content) 50 0 50
338 50 my $enc_content = JSON::XS::encode_json $content;
339 50 $res->content($enc_content);
340 50 $res->content_length( length($enc_content) );
341 }
342 50 $res->finalize;
343 }
344  
345 sub _error_response {
346 25 ▶ my ( $self, $args ) = @_;
Subroutine _error_response: called CC=1 SCAR=0.0
347  
348 return $self->_response( $args->[0], [], +{
349 25 jsonapi => { version => $self->{'ponapi.spec_version'} },
350 errors => [ { detail => $args->[1], status => $args->[0] } ],
351 });
352 }
353  
354 3 1;
355  
356 __END__
357  
358 =pod
359  
360 =encoding UTF-8
361  
362 =head1 NAME
363  
364 PONAPI::Server - PONAPI - Perl implementation of {JSON:API} (http://jsonapi.org/) v1.0
365  
366 =head1 VERSION
367  
368 version 0.002012
369  
370 =head1 SYNOPSIS
371  
372 # Run the server
373 $ plackup -MPONAPI::Server -e 'PONAPI::Server->new("repository.class" => "Test::PONAPI::Repository::MockDB")->to_app'
374  
375 $ perl -MPONAPI::Client -E 'say Dumper(PONAPI::Client->new->retrieve(type => "people", id => 88))'
376  
377 # Or with cURL:
378 $ curl -X GET -H "Content-Type: application/vnd.api+json" 'http://0:5000/people/88'
379  
380 =head1 DESCRIPTION
381  
382 C<PONAPI::Server> is a small plack server that implements the
383 L<{json:api}|http://jsonapi.org/> specification.
384  
385 You'll have to set up a repository (to provide access to the data
386 you want to server) and tweak some server configurations, so
387 hop over to L<PONAPI::Manual> for the next steps!
388  
389 =head1 BUGS, CONTACT AND SUPPORT
390  
391 For reporting bugs or submitting patches, please use the github
392 bug tracker at L<https://github.com/mickeyn/PONAPI>.
393  
394 =head1 AUTHORS
395  
396 =over 4
397  
398 =item *
399  
400 Mickey Nasriachi <mickey@cpan.org>
401  
402 =item *
403  
404 Stevan Little <stevan@cpan.org>
405  
406 =item *
407  
408 Brian Fraser <hugmeir@cpan.org>
409  
410 =back
411  
412 =head1 COPYRIGHT AND LICENSE
413  
414 This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser.
415  
416 This is free software; you can redistribute it and/or modify it under
417 the same terms as the Perl 5 programming language system itself.
418  
419 =cut
« PONAPI/CLI/Command/demo.pm ↑ Summary PONAPI/CLI/Command/gen.pm »