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