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