X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FTraitFor%2FRequest%2FREST.pm;h=1d35710aaa56e619971f96ac8446f03e0e1aa273;hb=refs%2Fheads%2Ffix-accepted-content-types;hp=4a2ca875a5f6d3dc1054e743bdc3403a813f68ae;hpb=042656b67276f5c8f17dcb64955c1b350e9418b7;p=catagits%2FCatalyst-Action-REST.git diff --git a/lib/Catalyst/TraitFor/Request/REST.pm b/lib/Catalyst/TraitFor/Request/REST.pm index 4a2ca87..1d35710 100644 --- a/lib/Catalyst/TraitFor/Request/REST.pm +++ b/lib/Catalyst/TraitFor/Request/REST.pm @@ -24,14 +24,46 @@ has preferred_content_type => ( init_arg => undef, ); -sub _build_accepted_content_types { - my $self = shift; +has accepted_response_content_types => ( + is => 'ro', + isa => 'ArrayRef', + lazy => 1, + builder => '_build_accepted_response_content_types', + init_arg => undef, +); - my %types; +has preferred_response_content_type => ( + is => 'ro', + isa => 'Str', + lazy => 1, + builder => '_build_preferred_response_content_type', + init_arg => undef, +); + +sub _accepted_types_sort { + my ($self, %types) = @_; + [ sort { $types{$b} <=> $types{$a} } keys %types ]; +} +sub _build_accepted_content_types { + my $self = shift; + my %types = $self->_accepted_response_content_types_inner; # First, we use the content type in the HTTP Request. It wins all. $types{ $self->content_type } = 3 if $self->content_type; + $self->_accepted_types_sort(%types); +} + +sub _build_accepted_response_content_types { + my $self = shift; + my %types = $self->_accepted_response_content_types_inner; + $self->_accepted_types_sort(%types); +} + +sub _accepted_response_content_types_inner { + my $self = shift; + + my %types; if ($self->method eq "GET" && $self->param('content-type')) { $types{ $self->param('content-type') } = 2; @@ -66,11 +98,13 @@ sub _build_accepted_content_types { } } - [ sort { $types{$b} <=> $types{$a} } keys %types ]; + %types; } sub _build_preferred_content_type { $_[0]->accepted_content_types->[0] } +sub _build_preferred_response_content_type { $_[0]->accepted_response_content_types->[0] } + sub accepts { my $self = shift; my $type = shift;