| 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'
MC/DC0%0/1
$self->{'ponapi.config_dir'} || 'conf'
$self->{'ponapi.config_dir'}
Truth table50%1/2
A
A= $self->{'ponapi.config_dir'}
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | )->read_config; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 9 | ▶ | $self->{$_} //= $conf{$_} for keys %conf; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{$_} //= $conf{$_}
MC/DC100%2/2
$self->{$_} //= $conf{$_}
$self->{$_}
$conf{$_}
Truth table100%3/3
A //= B
A= $self->{$_}B= $conf{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$self->{"ponapi.spec_version"} //= "1.0"
$self->{"ponapi.spec_version"}
Truth table50%1/2
A
A= $self->{"ponapi.spec_version"}
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 9 | ▶ | $self->{'ponapi.mediatype'} //= $default_media_type; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"ponapi.mediatype"} //= $default_media_type
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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")
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'};})
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 25 | ▶ | my $req_path = $self->{'ponapi.relative_links'} eq 'full' ? "".$req->uri : $req->request_uri; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $type and $type =~ /$qr_member_name_prefix/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | # validate `rel_type` | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 44 | ▶ | if ( defined $rel_type ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 7 | ▶ | $wr->(ERR_BAD_REQ) if $relationships ne 'relationships'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$def_rel_type and not $rel_type =~ /$qr_member_name_prefix/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | # set `action` | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 43 | my $action; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 43 | ▶ | if ( defined $id ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 32 | ▶ | $action = 'create_relationships' if $method eq 'POST' and $relationships and $def_rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$method eq "POST" and $relationships and $def_rel_type
$method eq "POST" and $relationships
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
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 32 | ▶ | $action = 'retrieve' if $method eq 'GET' and !$relationships and !$def_rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$method eq "GET" and not $relationships and not $def_rel_type
$method eq "GET" and not $relationships
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
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 32 | ▶ | $action = 'retrieve_by_relationship' if $method eq 'GET' and !$relationships and $def_rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$method eq "GET" and not $relationships and $def_rel_type
$method eq "GET" and not $relationships
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
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 32 | ▶ | $action = 'retrieve_relationships' if $method eq 'GET' and $relationships and $def_rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$method eq "GET" and $relationships and $def_rel_type
$method eq "GET" and $relationships
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
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 32 | ▶ | $action = 'update' if $method eq 'PATCH' and !$relationships and !$def_rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$method eq "PATCH" and not $relationships and not $def_rel_type
$method eq "PATCH" and not $relationships
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
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 32 | ▶ | $action = 'update_relationships' if $method eq 'PATCH' and $relationships and $def_rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$method eq "PATCH" and $relationships and $def_rel_type
$method eq "PATCH" and $relationships
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
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 32 | ▶ | $action = 'delete' if $method eq 'DELETE' and !$relationships and !$def_rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$method eq "DELETE" and not $relationships and not $def_rel_type
$method eq "DELETE" and not $relationships
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
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 32 | ▶ | $action = 'delete_relationships' if $method eq 'DELETE' and $relationships and $def_rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$method eq "DELETE" and $relationships and $def_rel_type
$method eq "DELETE" and $relationships
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
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 11 | ▶ | $action = 'retrieve_all' if $method eq 'GET'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 11 | ▶ | $action = 'create' if $method eq 'POST'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 43 | ▶ | $wr->(ERR_NO_MATCHING_ROUTE) unless $action; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 41 | ▶ | $def_rel_type and push @ret => rel_type => $rel_type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 49 | ▶ | $wr->(ERR_WRONG_CONTENT_TYPE) unless $content_type eq $mt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 48 | $has_mediatype++; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 49 | ▶ | $wr->(ERR_MISSING_CONTENT_TYPE) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$req->content_length and not $content_type
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
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | if $req->content_length and !$content_type; | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # check Accept | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 49 | ▶ | if ( my $accept = $req->headers->header('Accept') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | my @jsonapi_accept = | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | map { ( $_->[1]->type eq $mt ) ? $_->[1] : () } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | $pack->create_header( 'Accept' => $accept )->iterable; | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 2 | ▶ | if ( @jsonapi_accept ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | $wr->(ERR_WRONG_HEADER_ACCEPT) | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | unless grep { $_->params_are_empty } @jsonapi_accept; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | $has_mediatype++; | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 48 | ▶ | $wr->(ERR_MISSING_MEDIA_TYPE) unless $has_mediatype; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $f and grep {$p eq $_;} "page", "fields", "filter"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$p eq "sort" and not $self->{"ponapi.sort_allowed"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | # values can be passed as CSV | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 11 | ▶ | my @values = map { $unesacpe_values ? uri_unescape($_) : $_ } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$p ne "fields" and exists $query_params->{$k} and not @values
$p ne "fields" and exists $query_params->{$k}
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | # page info has one value per request | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 10 | ▶ | $p eq 'page' and $params{$p}{$f} = $values[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # values passed on in hash-ref | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 10 | ▶ | $p eq 'include' and $params{include} = \@values; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$req->content_length and $req->content_length > 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 12 | ▶ | $wr->(ERR_BAD_REQ) if $req->method eq 'GET'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$body and ref $body eq "HASH" and exists $body->{"data"}
$body and ref $body eq "HASH"
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $data or ref($data) =~ /^(?:ARRAY|HASH)$/
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)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 10 | ▶ | $self->_validate_data_members( $wr, $data ) if defined $data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 10 | for my $r ( @recs ) { | |||||||||||||||||||||||||||||||||||||||||
| 286 | 10 | ▶ | return unless keys %{$r}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | # `type` | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 10 | ▶ | $wr->(ERR_BAD_REQ) unless $r->{type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 10 | ▶ | $wr->(ERR_BAD_REQ_INVALID_NAME) unless check_name( $r->{type} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | # `attributes` | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 9 | ▶ | if ( exists $r->{attributes} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | ▶ | $wr->(ERR_BAD_REQ) unless ref( $r->{attributes} ) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | $wr->(ERR_BAD_REQ_INVALID_NAME) | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 2 | ▶ | if grep { !check_name($_) } keys %{ $r->{attributes} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | # `relationships` | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 7 | ▶ | if ( exists $r->{relationships} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | ▶ | $wr->(ERR_BAD_REQ) unless ref( $r->{relationships} ) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | for my $k ( keys %{ $r->{relationships} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 304 | 2 | ▶ | $wr->(ERR_BAD_REQ_INVALID_NAME) unless check_name($k); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | my $rel = $r->{relationships}{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 1 | ▶ | my @rels = ref($rel||'') eq 'ARRAY' ? @$rel : $rel; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$rel || ''
MC/DC0%0/1
$rel || ''
$rel
Truth table50%1/2
A
A= $rel
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 1 | foreach my $relationship ( @rels ) { | |||||||||||||||||||||||||||||||||||||||||
| 309 | 1 | ▶ | next if !defined $relationship; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | 1 | ▶ | $wr->(ERR_BAD_REQ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | if ref( $relationship ) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 317 | $wr->(ERR_BAD_REQ) | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 1 | ▶ | if !exists $relationship->{type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
not check_name($relationship->{"type"}) or grep {not check_name($_);} keys %$relationship
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$status || 200
$status
Truth table50%1/2
A
A= $status
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 50 | ▶ | if ( ref $content ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||