Change to Moose
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Request / REST.pm
CommitLineData
256c894f 1package Catalyst::Request::REST;
930013e6 2use Moose;
37694e6c 3
4use Catalyst::Utils;
9a76221e 5use HTTP::Headers::Util qw(split_header_words);
256c894f 6
f168fa3e 7use namespace::autoclean;
8
9extends 'Catalyst::Request';
10
11has [qw/ data accept_only /] => ( is => 'rw' );
12
5132f5e4 13sub _insert_self_into {
37694e6c 14 my ($class, $app_class ) = @_;
27beb190 15 # the fallback to $app_class is for the (rare and deprecated) case when
16 # people are defining actions in MyApp.pm instead of in a controller.
2998eff6 17 my $app = (blessed($app_class) && $app_class->can('_application'))
18 ? $app_class->_application : Catalyst::Utils::class2appclass( $app_class ) || $app_class;
37694e6c 19
5132f5e4 20 my $req_class = $app->request_class;
21 return if $req_class->isa($class);
22 if ($req_class eq 'Catalyst::Request') {
23 $app->request_class($class);
24 } else {
25 die "$app has a custom request class $req_class, "
26 . "which is not a $class; see Catalyst::Request::REST";
27 }
28}
256c894f 29
bd8fc54e 30sub accepted_content_types {
31 my $self = shift;
32
33 return $self->{content_types} if $self->{content_types};
34
35 my %types;
36
37 # First, we use the content type in the HTTP Request. It wins all.
38 $types{ $self->content_type } = 3
39 if $self->content_type;
40
41 if ($self->method eq "GET" && $self->param('content-type')) {
42 $types{ $self->param('content-type') } = 2;
43 }
44
45 # Third, we parse the Accept header, and see if the client
46 # takes a format we understand.
47 #
48 # This is taken from chansen's Apache2::UploadProgress.
49 if ( $self->header('Accept') ) {
50 $self->accept_only(1) unless keys %types;
51
52 my $accept_header = $self->header('Accept');
53 my $counter = 0;
54
55 foreach my $pair ( split_header_words($accept_header) ) {
56 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
57 next if $types{$type};
58
59 # cope with invalid (missing required q parameter) header like:
60 # application/json; charset="utf-8"
61 # http://tools.ietf.org/html/rfc2616#section-14.1
62 unless ( defined $pair->[2] && lc $pair->[2] eq 'q' ) {
63 $qvalue = undef;
64 }
65
66 unless ( defined $qvalue ) {
67 $qvalue = 1 - ( ++$counter / 1000 );
68 }
69
70 $types{$type} = sprintf( '%.3f', $qvalue );
71 }
72 }
73
74 return $self->{content_types} =
75 [ sort { $types{$b} <=> $types{$a} } keys %types ];
76}
77
78sub preferred_content_type { $_[0]->accepted_content_types->[0] }
79
80sub accepts {
81 my $self = shift;
82 my $type = shift;
83
84 return grep { $_ eq $type } @{ $self->accepted_content_types };
85}
86
f168fa3e 87__PACKAGE__->meta->make_immutable;
88__END__
89
9a76221e 90=head1 NAME
91
92Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
93
94=head1 SYNOPSIS
95
d6fb033c 96 if ( $c->request->accepts('application/json') ) {
9a76221e 97 ...
98 }
99
100 my $types = $c->request->accepted_content_types();
101
102=head1 DESCRIPTION
103
104This is a subclass of C<Catalyst::Request> that adds a few methods to
105the request object to faciliate writing REST-y code. Currently, these
106methods are all related to the content types accepted by the client.
107
5132f5e4 108Note that if you have a custom request class in your application, and it does
109not inherit from C<Catalyst::Request::REST>, your application will fail with an
110error indicating a conflict the first time it tries to use
111C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
112custom request class inherits from C<Catalyst::Request::REST>.
9a76221e 113
114=head1 METHODS
115
bd8fc54e 116=over
9a76221e 117
bd8fc54e 118=item data
9a76221e 119
bd8fc54e 120If the request went through the Deserializer action, this method will
121return the deserialized data structure.
fec6d454 122
9a76221e 123=item accepted_content_types
124
125Returns an array reference of content types accepted by the
126client.
127
128The list of types is created by looking at the following sources:
129
130=over 8
131
132=item * Content-type header
133
134If this exists, this will always be the first type in the list.
135
136=item * content-type parameter
137
138If the request is a GET request and there is a "content-type"
139parameter in the query string, this will come before any types in the
140Accept header.
141
142=item * Accept header
143
144This will be parsed and the types found will be ordered by the
145relative quality specified for each type.
146
147=back
148
149If a type appears in more than one of these places, it is ordered based on
150where it is first found.
151
9a76221e 152=item preferred_content_type
153
154This returns the first content type found. It is shorthand for:
155
156 $request->accepted_content_types->[0]
157
9a76221e 158=item accepts($type)
159
160Given a content type, this returns true if the type is accepted.
161
162Note that this does not do any wildcard expansion of types.
163
fec6d454 164=back
165
6e6e0bf9 166=head1 AUTHORS
9a76221e 167
6e6e0bf9 168See L<Catalyst::Action::REST> for authors.
fec6d454 169
9a76221e 170=head1 LICENSE
171
172You may distribute this code under the same terms as Perl itself.
173
174=cut