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