| 1 | # ABSTRACT: DAO request class | ||||||||||||||||||||||||||||
| 2 | package PONAPI::DAO::Request; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use PONAPI::Builder::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 8 | has repository => ( | |||||||||||||||||||||||||||
| 10 | is => 'ro', | ||||||||||||||||||||||||||||
| 11 | does => 'PONAPI::Repository', | ||||||||||||||||||||||||||||
| 12 | required => 1, | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | has document => ( | |||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||
| 17 | isa => 'PONAPI::Builder::Document', | ||||||||||||||||||||||||||||
| 18 | required => 1, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | has type => ( | |||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | isa => 'Str', | ||||||||||||||||||||||||||||
| 24 | required => 1, | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | has send_doc_self_link => ( | ||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 30 | 207 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 8 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has is_valid => ( | ||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 36 | 241 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 8 | writer => '_set_is_valid', | |||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | has json => ( | ||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||
| 42 | isa => 'JSON::XS', | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | default => sub { JSON::XS->new->allow_nonref->utf8->canonical }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 8 | ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub BUILDARGS { | ||||||||||||||||||||||||||||
| 47 | 243 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 243 | ▶ | my %args = @_ == 1 ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | die "[__PACKAGE__] missing arg `version`" | ||||||||||||||||||||||||||||
| 51 | 243 | ▶ | unless defined $args{version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | $args{document} = PONAPI::Builder::Document->new( | ||||||||||||||||||||||||||||
| 54 | version => $args{version}, | ||||||||||||||||||||||||||||
| 55 | req_path => $args{req_path} // '/', | ||||||||||||||||||||||||||||
| 56 | 243 | ▶ | req_base => $args{req_base} // '/', | ||||||||||||||||||||||||||
|
Condition100%4/4
$args{"req_path"} // "/"
$args{"req_base"} // "/"
MC/DC100%1/1
$args{"req_path"} // "/"
$args{"req_path"}
MC/DC100%1/1
$args{"req_base"} // "/"
$args{"req_base"}
Truth table100%2/2
A
A= $args{"req_path"}
Truth table100%2/2
A
A= $args{"req_base"}
| |||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 243 | return \%args; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub BUILD { | ||||||||||||||||||||||||||||
| 63 | 235 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=21 SCAR=30.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # `type` exists | ||||||||||||||||||||||||||||
| 66 | 235 | my $type = $self->type; | |||||||||||||||||||||||||||
| 67 | 235 | ▶ | return $self->_bad_request( "Type `$type` doesn't exist.", 404 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | unless $self->repository->has_type( $type ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # validate `id` parameter | ||||||||||||||||||||||||||||
| 71 | 225 | ▶ | if ( $self->does('PONAPI::DAO::Request::Role::HasID') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 72 | 169 | ▶ | $self->_bad_request( "`id` is missing for this request" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | unless $self->has_id; | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | elsif ( defined $args->{id} ) { | ||||||||||||||||||||||||||||
| 76 | 3 | $self->_bad_request( "`id` is not allowed for this request" ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # validate `rel_type` parameter | ||||||||||||||||||||||||||||
| 80 | 225 | ▶ | if ( $self->does('PONAPI::DAO::Request::Role::HasRelationshipType') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 81 | defined $args->{rel_type} | ||||||||||||||||||||||||||||
| 82 | 73 | ▶ | ? $self->_validate_rel_type | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | : $self->_bad_request( "`relationship type` is missing for this request" ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | elsif ( defined $args->{rel_type} ) { | ||||||||||||||||||||||||||||
| 86 | 11 | $self->_bad_request( "`relationship type` is not allowed for this request" ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # validate `include` parameter | ||||||||||||||||||||||||||||
| 90 | 225 | ▶ | if ( defined $args->{include} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 27 | ▶ | $self->does('PONAPI::DAO::Request::Role::HasInclude') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | ? $self->_validate_include | ||||||||||||||||||||||||||||
| 93 | : $self->_bad_request( "`include` is not allowed for this request" ); | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # validate `fields` parameter | ||||||||||||||||||||||||||||
| 97 | 225 | ▶ | if ( defined $args->{fields} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 21 | ▶ | $self->does('PONAPI::DAO::Request::Role::HasFields') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | ? $self->_validate_fields | ||||||||||||||||||||||||||||
| 100 | : $self->_bad_request( "`fields` is not allowed for this request" ); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # validate `filter` parameter | ||||||||||||||||||||||||||||
| 104 | 225 | ▶ | if ( defined $args->{filter} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 3 | ▶ | $self->does('PONAPI::DAO::Request::Role::HasFilter') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | ? $self->_validate_filter | ||||||||||||||||||||||||||||
| 107 | : $self->_bad_request( "`filter` is not allowed for this request" ); | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # validate `sort` parameter | ||||||||||||||||||||||||||||
| 111 | 225 | ▶ | if ( defined $args->{sort} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 15 | ▶ | $self->does('PONAPI::DAO::Request::Role::HasSort') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | ? $self->_validate_sort | ||||||||||||||||||||||||||||
| 114 | : $self->_bad_request( "`sort` is not allowed for this request" ); | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # validate `page` parameter | ||||||||||||||||||||||||||||
| 118 | 225 | ▶ | if ( defined $args->{page} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 14 | ▶ | $self->does('PONAPI::DAO::Request::Role::HasPage') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | ? $self->_validate_page | ||||||||||||||||||||||||||||
| 121 | : $self->_bad_request( "`page` is not allowed for this request" ); | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # validate `data` | ||||||||||||||||||||||||||||
| 125 | 225 | ▶ | if ( exists $args->{data} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | 97 | ▶ | if ( $self->can('data') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 87 | $self->_validate_data; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||
| 130 | 10 | $self->_bad_request( "request body is not allowed" ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | elsif ( $self->can('has_data') ) { | ||||||||||||||||||||||||||||
| 134 | 7 | $self->_bad_request( "request body is missing `data`" ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub response { | ||||||||||||||||||||||||||||
| 139 | 196 | ▶ | my ( $self, @headers ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
response: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 196 | my $doc = $self->document; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 196 | ▶ | $doc->add_self_link | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->send_doc_self_link and not $doc->has_link("self")
MC/DC50%1/2
$self->send_doc_self_link and not $doc->has_link("self")
$self->send_doc_self_link
not $doc->has_link("self")
Truth table66%2/3
A and not(B)
A= $self->send_doc_self_linkB= not $doc->has_link("self")
| |||||||||||||||||||||||||||||
| 143 | if $self->send_doc_self_link && !$doc->has_link('self'); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | return ( | ||||||||||||||||||||||||||||
| 146 | 196 | ▶ | $doc->status, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | \@headers, | ||||||||||||||||||||||||||||
| 148 | ( | ||||||||||||||||||||||||||||
| 149 | $doc->status != 204 | ||||||||||||||||||||||||||||
| 150 | ? $doc->build | ||||||||||||||||||||||||||||
| 151 | : () | ||||||||||||||||||||||||||||
| 152 | ), | ||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub _bad_request { | ||||||||||||||||||||||||||||
| 157 | 105 | ▶ | my ( $self, $detail, $status ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_bad_request: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 158 | 105 | ▶ | $self->document->raise_error( $status||400, { detail => $detail } ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$status || 400
MC/DC100%1/1
$status || 400
$status
Truth table100%2/2
A
A= $status
| |||||||||||||||||||||||||||||
| 159 | 105 | $self->_set_is_valid(0); | |||||||||||||||||||||||||||
| 160 | 105 | return; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 8 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 164 | 8 | ▶ | no Moose; 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =pod | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head1 NAME | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | PONAPI::DAO::Request - DAO request class | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head1 VERSION | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | version 0.002012 | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =over 4 | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item * | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Mickey Nasriachi <mickey@cpan.org> | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item * | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Stevan Little <stevan@cpan.org> | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =item * | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Brian Fraser <hugmeir@cpan.org> | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =back | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 203 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||