Mangle the request class mangling stuff. It's still fugly, but it's no longer totally...
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Request / REST.pm
CommitLineData
256c894f 1package Catalyst::Request::REST;
930013e6 2use Moose;
37694e6c 3
4use Catalyst::Utils;
f168fa3e 5use namespace::autoclean;
6
7extends 'Catalyst::Request';
e623bdf2 8with 'Catalyst::TraitFor::Request::REST';
f168fa3e 9
5132f5e4 10sub _insert_self_into {
37694e6c 11 my ($class, $app_class ) = @_;
27beb190 12 # the fallback to $app_class is for the (rare and deprecated) case when
13 # people are defining actions in MyApp.pm instead of in a controller.
2998eff6 14 my $app = (blessed($app_class) && $app_class->can('_application'))
15 ? $app_class->_application : Catalyst::Utils::class2appclass( $app_class ) || $app_class;
37694e6c 16
5132f5e4 17 my $req_class = $app->request_class;
18 return if $req_class->isa($class);
149182b3 19 my $req_class_meta = Moose->init_meta( for_class => $req_class );
20 return if $req_class_meta->does_role('Catalyst::TraitFor::Request::REST');
5132f5e4 21 if ($req_class eq 'Catalyst::Request') {
22 $app->request_class($class);
149182b3 23 }
24 else {
25 my $meta = Moose::Meta::Class->create_anon_class(
26 superclasses => [$req_class],
27 roles => ['Catalyst::TraitFor::Request::REST'],
28 cache => 1
29 );
30 $meta->add_method(meta => sub { $meta });
31 $app->request_class($meta->name);
5132f5e4 32 }
33}
256c894f 34
f168fa3e 35__PACKAGE__->meta->make_immutable;
36__END__
37
9a76221e 38=head1 NAME
39
40Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
41
42=head1 SYNOPSIS
43
d6fb033c 44 if ( $c->request->accepts('application/json') ) {
9a76221e 45 ...
46 }
47
48 my $types = $c->request->accepted_content_types();
49
50=head1 DESCRIPTION
51
52This is a subclass of C<Catalyst::Request> that adds a few methods to
53the request object to faciliate writing REST-y code. Currently, these
54methods are all related to the content types accepted by the client.
55
5132f5e4 56Note that if you have a custom request class in your application, and it does
57not inherit from C<Catalyst::Request::REST>, your application will fail with an
58error indicating a conflict the first time it tries to use
59C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
60custom request class inherits from C<Catalyst::Request::REST>.
9a76221e 61
62=head1 METHODS
63
bd8fc54e 64=over
9a76221e 65
bd8fc54e 66=item data
9a76221e 67
bd8fc54e 68If the request went through the Deserializer action, this method will
69return the deserialized data structure.
fec6d454 70
9a76221e 71=item accepted_content_types
72
73Returns an array reference of content types accepted by the
74client.
75
76The list of types is created by looking at the following sources:
77
78=over 8
79
80=item * Content-type header
81
82If this exists, this will always be the first type in the list.
83
84=item * content-type parameter
85
86If the request is a GET request and there is a "content-type"
87parameter in the query string, this will come before any types in the
88Accept header.
89
90=item * Accept header
91
92This will be parsed and the types found will be ordered by the
93relative quality specified for each type.
94
95=back
96
97If a type appears in more than one of these places, it is ordered based on
98where it is first found.
99
9a76221e 100=item preferred_content_type
101
102This returns the first content type found. It is shorthand for:
103
104 $request->accepted_content_types->[0]
105
9a76221e 106=item accepts($type)
107
108Given a content type, this returns true if the type is accepted.
109
110Note that this does not do any wildcard expansion of types.
111
fec6d454 112=back
113
6e6e0bf9 114=head1 AUTHORS
9a76221e 115
6e6e0bf9 116See L<Catalyst::Action::REST> for authors.
fec6d454 117
9a76221e 118=head1 LICENSE
119
120You may distribute this code under the same terms as Perl itself.
121
122=cut