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