Catalyst-TraitFor-Request-ContentNegotiationHelpers
view release on metacpan or search on metacpan
# This file documents the revision history for Perl extension Catalyst-TraitFor-Request-ContentNegotiationHelpers.
0.006 - 2015-11-30
- New ActionRole to delegate media type negotiation across actions
0.005 - 2015-11-12
- 'no_match' callbacks now get a hash of the other possible callbacks
to make it easier to set a default or to inspect.
0.004 - 2015-09-24
- Fixed missing $VERSION information
0.003 - 2015-08-24
- all 'accepts_*' methods now accept a list and return allowed content types
not just true / false. This should not break any existing use but be aware
that the return value for true is no longer just '1'. Return value for
'nothing acceptable' is now undef.
README.mkdn view on Meta::CPAN
Given an array of possible media types ('application/json', 'text/html', etc.)
return the one that is the best match for the current request (by looking at the
current request ACCEPT header, parsing it and comparing).
## accepts\_media\_type ($type)
Given a string type that is a media type (text/html, application/json) returns true
if that type is acceptable to the requesting client.
## on\_best\_media\_type (%callbacks)
Given a hash where the keys are media types and the values are coderefs, execute
and return the value of the coderef whose key is the best match for that media type
(based on the result of ["choose\_media\_type"](#choose_media_type). For example:
my $body = $c->req->on_best_media_type(
'no_match' => sub { 'none' },
'text/html' => sub { 'html' },
'application/json' => sub { 'json' });
README.mkdn view on Meta::CPAN
## choose\_language (@array\_of\_langauges)
Given an array of possible media types ('en-US', 'es', etc.)
return the one that is the best match for the current request.
## accepts\_language ($type)
Given a string type that is a language string returns true
if that is acceptable to the requesting client.
## on\_best\_language (%callbacks)
Works like ["on\_best\_media\_type"](#on_best_media_type) but matches language.
## choose\_charset (@array\_of\_character\_sets)
Given an array of possible media types ("UTF-8", "US-ASCII", etc.)
return the one that is the best match for the current request.
## accepts\_charset ($type)
Given a string type that is a charset string returns true
if that is acceptable to the requesting client.
## on\_best\_charset (%callbacks)
Works like ["on\_best\_media\_type"](#on_best_media_type) but matches charset.
## choose\_encoding (@array\_of\_encodings)
Given an array of possible encodings ("gzip", "identity", etc.)
return the one that is the best match for the current request.
## accepts\_encoding ($type)
Given a string type that is an encoding string returns true
if that is acceptable to the requesting client.
## on\_best\_encoding (%callbacks)
Works like ["on\_best\_media\_type"](#on_best_media_type) but matches encoding.
## raw\_choose\_media\_type
## raw\_choose\_language
## raw\_choose\_charset
## raw\_choose\_encoding
lib/Catalyst/ActionRole/ProvidesMedia.pm view on Meta::CPAN
return \%media_actions;
}
sub forwards {
my ($self, $ctx) = @_;
Scalar::Util::weaken($ctx);
my %media_actions = %{$self->media_actions};
unless(exists $media_actions{no_match}) {
$media_actions{no_match} = sub {
my ($req, %callbacks) = @_;
$ctx->res->status(406);
$ctx->res->content_type('text/plain');
my $allowed = join(',',(keys %callbacks));
$ctx->res->body("You requested a media type we don't support. Acceptable types are: $allowed");
};
}
my @forwards = map {
my $action = $media_actions{$_};
$_ => $_ eq 'no_match' ?
sub { $ctx->forward($action, \%media_actions) } :
sub { $ctx->forward($action) };
} (keys %media_actions);
lib/Catalyst/TraitFor/Request/ContentNegotiationHelpers.pm view on Meta::CPAN
raw_choose_charset => 'choose_charset',
raw_choose_encoding => 'choose_encoding',
});
sub _build_content_negotiator {
return HTTP::Headers::ActionPack->new
->get_content_negotiator;
}
my $on_best = sub {
my ($self, $method, %callbacks) = @_;
my $default = delete $callbacks{no_match};
if(my $match = $self->$method(keys %callbacks)) {
return $callbacks{$match}->($self);
} else {
return $default ? $default->($self, %callbacks) : undef;
}
};
sub choose_media_type {
my $self = shift;
return $self->raw_choose_media_type(\@_, $self->header('Accept'));
}
sub accepts_media_type {
my $self = shift;
lib/Catalyst/TraitFor/Request/ContentNegotiationHelpers.pm view on Meta::CPAN
on those types that are acceptable:
if($c->req->accepts_media_type('application/json')) {
# handle JSON
}
my @acceptable = $c->req->accepts_media_type('image/jpeg', 'text/html', 'text/plain');
If nothing is acceptable an undef will be returned.
=head2 on_best_media_type (%callbacks)
Given a hash where the keys are media types and the values are coderefs, execute
and return the value of the coderef whose key is the best match for that media type
(based on the result of L</choose_media_type>. For example:
my $body = $c->req->on_best_media_type(
'no_match' => sub { 'none' },
'text/html' => sub { 'html' },
'application/json' => sub { 'json' });
$c->res->body($body);
The coderef will receive the current request object as its single argument.
If there are no matches, execute the coderef associated with a 'no_match' key
or return undef if no such key exists. When executing the 'no_match' callback
(if any) we also pass a hash of the other callbacks, which you might use for
setting a default response, or to inspect as part of the information required.
'no_match' => sub {
my ($req, %callbacks) = @_;
my @allowed = keys %callbacks;
...
}
In this case the 'no_match' callback is removed from '%callbacks' passed to prevent
the possibility of recursion.
=head2 choose_language (@array_of_langauges)
Given an array of possible media types ('en-US', 'es', etc.)
return the one that is the best match for the current request.
=head2 accepts_language ($type)
Like L</accepts_media_type> but for request language.
=head2 on_best_language (%callbacks)
Works like L</on_best_media_type> but matches language.
=head2 choose_charset (@array_of_character_sets)
Given an array of possible media types ("UTF-8", "US-ASCII", etc.)
return the one that is the best match for the current request.
=head2 accepts_charset ($type)
Like L</accepts_media_type> but for request character set.
=head2 on_best_charset (%callbacks)
Works like L</on_best_media_type> but matches charset.
=head2 choose_encoding (@array_of_encodings)
Given an array of possible encodings ("gzip", "identity", etc.)
return the one that is the best match for the current request.
=head2 accepts_encoding ($type)
Like L</accepts_media_type> but for request encoding.
=head2 on_best_encoding (%callbacks)
Works like L</on_best_media_type> but matches encoding.
=head2 raw_choose_media_type
=head2 raw_choose_language
=head2 raw_choose_charset
=head2 raw_choose_encoding
( run in 0.721 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )