Fix docs
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / TraitFor / Request / REST.pm
1 package Catalyst::TraitFor::Request::REST;
2 use Moose::Role;
3 use HTTP::Headers::Util qw(split_header_words);
4 use namespace::autoclean;
5
6 has [qw/ data accept_only /] => ( is => 'rw' );
7
8 sub accepted_content_types {
9     my $self = shift;
10
11     return $self->{content_types} if $self->{content_types};
12
13     my %types;
14
15     # First, we use the content type in the HTTP Request.  It wins all.
16     $types{ $self->content_type } = 3
17         if $self->content_type;
18
19     if ($self->method eq "GET" && $self->param('content-type')) {
20         $types{ $self->param('content-type') } = 2;
21     }
22
23     # Third, we parse the Accept header, and see if the client
24     # takes a format we understand.
25     #
26     # This is taken from chansen's Apache2::UploadProgress.
27     if ( $self->header('Accept') ) {
28         $self->accept_only(1) unless keys %types;
29
30         my $accept_header = $self->header('Accept');
31         my $counter       = 0;
32
33         foreach my $pair ( split_header_words($accept_header) ) {
34             my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
35             next if $types{$type};
36
37             # cope with invalid (missing required q parameter) header like:
38             # application/json; charset="utf-8"
39             # http://tools.ietf.org/html/rfc2616#section-14.1
40             unless ( defined $pair->[2] && lc $pair->[2] eq 'q' ) {
41                 $qvalue = undef;
42             }
43
44             unless ( defined $qvalue ) {
45                 $qvalue = 1 - ( ++$counter / 1000 );
46             }
47
48             $types{$type} = sprintf( '%.3f', $qvalue );
49         }
50     }
51
52     return $self->{content_types} =
53         [ sort { $types{$b} <=> $types{$a} } keys %types ];
54 }
55
56 sub preferred_content_type { $_[0]->accepted_content_types->[0] }
57
58 sub accepts {
59     my $self = shift;
60     my $type = shift;
61
62     return grep { $_ eq $type } @{ $self->accepted_content_types };
63 }
64
65 1;
66 __END__
67
68 =head1 NAME
69
70 Catalyst::TraitFor::Request::REST - A role to apply to Catalyst::Request giving it REST methods and attributes.
71
72 =head1 SYNOPSIS
73
74      if ( $c->request->accepts('application/json') ) {
75          ...
76      }
77
78      my $types = $c->request->accepted_content_types();
79
80 =head1 DESCRIPTION
81
82 This is a L<Moose::Role> applied to L<Catalyst::Request> that adds a few
83 methods to the request object to faciliate writing REST-y code.
84 Currently, these methods are all related to the content types accepted by
85 the client.
86
87 =head1 METHODS
88
89 =over
90
91 =item data
92
93 If the request went through the Deserializer action, this method will
94 return the deserialized data structure.
95
96 =item accepted_content_types
97
98 Returns an array reference of content types accepted by the
99 client.
100
101 The list of types is created by looking at the following sources:
102
103 =over 8
104
105 =item * Content-type header
106
107 If this exists, this will always be the first type in the list.
108
109 =item * content-type parameter
110
111 If the request is a GET request and there is a "content-type"
112 parameter in the query string, this will come before any types in the
113 Accept header.
114
115 =item * Accept header
116
117 This will be parsed and the types found will be ordered by the
118 relative quality specified for each type.
119
120 =back
121
122 If a type appears in more than one of these places, it is ordered based on
123 where it is first found.
124
125 =item preferred_content_type
126
127 This returns the first content type found. It is shorthand for:
128
129   $request->accepted_content_types->[0]
130
131 =item accepts($type)
132
133 Given a content type, this returns true if the type is accepted.
134
135 Note that this does not do any wildcard expansion of types.
136
137 =back
138
139 =head1 AUTHORS
140
141 See L<Catalyst::Action::REST> for authors.
142
143 =head1 LICENSE
144
145 You may distribute this code under the same terms as Perl itself.
146
147 =cut
148