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