File Coverage

blib/lib/PONAPI/DAO/Request.pm
Criterion Covered Total %
statement 50 50 100.0
branch 45 48 93.7
condition 8 9 88.8
subroutine 8 8 100.0
pod 1 3 33.3
total 112 118 94.9


line stmt bran cond sub pod time code
1             # ABSTRACT: DAO request class
2             package PONAPI::DAO::Request;
3              
4 8     8   5538 use Moose;
  8         15  
  8         47  
5 8     8   52318 use JSON::XS;
  8         12452  
  8         534  
6              
7 8     8   4841 use PONAPI::Builder::Document;
  8         29  
  8         6325  
8              
9             has repository => (
10             is => 'ro',
11             does => 'PONAPI::Repository',
12             required => 1,
13             );
14              
15             has document => (
16             is => 'ro',
17             isa => 'PONAPI::Builder::Document',
18             required => 1,
19             );
20              
21             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             default => sub { 0 },
31             );
32              
33             has is_valid => (
34             is => 'ro',
35             isa => 'Bool',
36             default => sub { 1 },
37             writer => '_set_is_valid',
38             );
39              
40             has json => (
41             is => 'ro',
42             isa => 'JSON::XS',
43             default => sub { JSON::XS->new->allow_nonref->utf8->canonical },
44             );
45              
46             sub BUILDARGS {
47 243     243 1 569 my $class = shift;
48 243 50       728 my %args = @_ == 1 ? %{ $_[0] } : @_;
  243         1765  
49              
50             die "[__PACKAGE__] missing arg `version`"
51 243 50       988 unless defined $args{version};
52              
53             $args{document} = PONAPI::Builder::Document->new(
54             version => $args{version},
55             req_path => $args{req_path} // '/',
56 243   100     12313 req_base => $args{req_base} // '/',
      100        
57             );
58              
59 243         10867 return \%args;
60             }
61              
62             sub BUILD {
63 235     235 0 517 my ( $self, $args ) = @_;
64              
65             # `type` exists
66 235         9311 my $type = $self->type;
67 235 100       9102 return $self->_bad_request( "Type `$type` doesn't exist.", 404 )
68             unless $self->repository->has_type( $type );
69              
70             # validate `id` parameter
71 225 100       1397 if ( $self->does('PONAPI::DAO::Request::Role::HasID') ) {
    100          
72 169 100       36740 $self->_bad_request( "`id` is missing for this request" )
73             unless $self->has_id;
74             }
75             elsif ( defined $args->{id} ) {
76 3         943 $self->_bad_request( "`id` is not allowed for this request" );
77             }
78              
79             # validate `rel_type` parameter
80 225 100       8427 if ( $self->does('PONAPI::DAO::Request::Role::HasRelationshipType') ) {
    100          
81             defined $args->{rel_type}
82 73 100       3436 ? $self->_validate_rel_type
83             : $self->_bad_request( "`relationship type` is missing for this request" );
84             }
85             elsif ( defined $args->{rel_type} ) {
86 11         475 $self->_bad_request( "`relationship type` is not allowed for this request" );
87             }
88              
89             # validate `include` parameter
90 225 100       7833 if ( defined $args->{include} ) {
91 27 100       119 $self->does('PONAPI::DAO::Request::Role::HasInclude')
92             ? $self->_validate_include
93             : $self->_bad_request( "`include` is not allowed for this request" );
94             }
95              
96             # validate `fields` parameter
97 225 100       722 if ( defined $args->{fields} ) {
98 21 100       86 $self->does('PONAPI::DAO::Request::Role::HasFields')
99             ? $self->_validate_fields
100             : $self->_bad_request( "`fields` is not allowed for this request" );
101             }
102              
103             # validate `filter` parameter
104 225 100       637 if ( defined $args->{filter} ) {
105 3 50       16 $self->does('PONAPI::DAO::Request::Role::HasFilter')
106             ? $self->_validate_filter
107             : $self->_bad_request( "`filter` is not allowed for this request" );
108             }
109              
110             # validate `sort` parameter
111 225 100       687 if ( defined $args->{sort} ) {
112 15 100       64 $self->does('PONAPI::DAO::Request::Role::HasSort')
113             ? $self->_validate_sort
114             : $self->_bad_request( "`sort` is not allowed for this request" );
115             }
116              
117             # validate `page` parameter
118 225 100       628 if ( defined $args->{page} ) {
119 14 100       63 $self->does('PONAPI::DAO::Request::Role::HasPage')
120             ? $self->_validate_page
121             : $self->_bad_request( "`page` is not allowed for this request" );
122             }
123              
124             # validate `data`
125 225 100       6519 if ( exists $args->{data} ) {
    100          
126 97 100       624 if ( $self->can('data') ) {
127 87         429 $self->_validate_data;
128             }
129             else {
130 10         42 $self->_bad_request( "request body is not allowed" );
131             }
132             }
133             elsif ( $self->can('has_data') ) {
134 7         27 $self->_bad_request( "request body is missing `data`" );
135             }
136             }
137              
138             sub response {
139 196     196 0 492 my ( $self, @headers ) = @_;
140 196         7314 my $doc = $self->document;
141              
142 196 100 66     8050 $doc->add_self_link
143             if $self->send_doc_self_link && !$doc->has_link('self');
144              
145             return (
146 196 100       7783 $doc->status,
147             \@headers,
148             (
149             $doc->status != 204
150             ? $doc->build
151             : ()
152             ),
153             );
154             }
155              
156             sub _bad_request {
157 105     105   1461 my ( $self, $detail, $status ) = @_;
158 105   100     3896 $self->document->raise_error( $status||400, { detail => $detail } );
159 105         4521 $self->_set_is_valid(0);
160 105         1715 return;
161             }
162              
163             __PACKAGE__->meta->make_immutable;
164 8     8   78 no Moose; 1;
  8         19  
  8         68  
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.002005
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