| 1 | package Catalyst::TraitFor::Request::ContentNegotiationHelpers; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | our $VERSION = '0.006'; | |||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use HTTP::Headers::ActionPack; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | has content_negotiator => ( | |||||||||
| 9 | is => 'bare', | ||||||||||
| 10 | required => 1, | ||||||||||
| 11 | lazy => 1, | ||||||||||
| 12 | builder => '_build_content_negotiator', | ||||||||||
| 13 | handles => +{ | ||||||||||
| 14 | raw_choose_media_type => 'choose_media_type', | ||||||||||
| 15 | raw_choose_language => 'choose_language', | ||||||||||
| 16 | raw_choose_charset => 'choose_charset', | ||||||||||
| 17 | raw_choose_encoding => 'choose_encoding', | ||||||||||
| 18 | }); | ||||||||||
| 19 | |||||||||||
| 20 | 5 | ▶ | sub _build_content_negotiator { | ||||||||
|
Subroutine
_build_content_negotiator: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 5 | return HTTP::Headers::ActionPack->new | |||||||||
| 22 | ->get_content_negotiator; | ||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 4 | ▶ | my $on_best = sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 26 | 4 | my ($self, $method, %callbacks) = @_; | |||||||||
| 27 | 4 | my $default = delete $callbacks{no_match}; | |||||||||
| 28 | 4 | ▶ | if(my $match = $self->$method(keys %callbacks)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 3 | return $callbacks{$match}->($self); | |||||||||
| 30 | } else { | ||||||||||
| 31 | 1 | ▶ | return $default ? $default->($self, %callbacks) : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | } | ||||||||||
| 33 | 2 | }; | |||||||||
| 34 | |||||||||||
| 35 | 9 | ▶ | sub choose_media_type { | ||||||||
|
Subroutine
choose_media_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | 9 | my $self = shift; | |||||||||
| 37 | 9 | return $self->raw_choose_media_type(\@_, $self->header('Accept')); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 3 | ▶ | sub accepts_media_type { | ||||||||
|
Subroutine
accepts_media_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 41 | 3 | my $self = shift; | |||||||||
| 42 | 3 | return map { $self->choose_media_type($_) } @_; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 4 | ▶ | sub on_best_media_type { | ||||||||
|
Subroutine
on_best_media_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | 4 | return shift->$on_best('choose_media_type', @_); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub choose_language { | ||||||||
|
Subroutine
choose_language: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 50 | 0 | my $self = shift; | |||||||||
| 51 | 0 | return $self->raw_choose_language(\@_, $self->header('Accept-Language')); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | sub accepts_language { | ||||||||
|
Subroutine
accepts_language: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 55 | 0 | my $self = shift; | |||||||||
| 56 | 0 | ▶ | return $self->choose_language(@_) ? 1:0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub on_best_language { | ||||||||
|
Subroutine
on_best_language: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 60 | 0 | return shift->$on_best('choose_language', @_); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub choose_charset { | ||||||||
|
Subroutine
choose_charset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 64 | 0 | my $self = shift; | |||||||||
| 65 | 0 | return $self->raw_choose_charset(\@_, $self->header('Accept-Charset')); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub accepts_charset { | ||||||||
|
Subroutine
accepts_charset: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 69 | 0 | my $self = shift; | |||||||||
| 70 | 0 | ▶ | return $self->choose_charset(@_) ? 1:0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub on_best_charset { | ||||||||
|
Subroutine
on_best_charset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 0 | return shift->$on_best('choose_charset', @_); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub choose_encoding { | ||||||||
|
Subroutine
choose_encoding: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 78 | 0 | my $self = shift; | |||||||||
| 79 | 0 | return $self->raw_choose_encoding(\@_, $self->header('Accept-Encoding')); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | sub accepts_encoding { | ||||||||
|
Subroutine
accepts_encoding: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 83 | 0 | my $self = shift; | |||||||||
| 84 | 0 | ▶ | return $self->choose_encoding(@_) ? 1:0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub on_best_encoding { | ||||||||
|
Subroutine
on_best_encoding: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 88 | 0 | return shift->$on_best('choose_encoding', @_); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 2 | 1; | |||||||||
| 92 | |||||||||||
| 93 | =head1 NAME | ||||||||||
| 94 | |||||||||||
| 95 | Catalyst::TraitFor::Request::ContentNegotiationHelpers - assistance with content negotiation | ||||||||||
| 96 | |||||||||||
| 97 | =head1 SYNOPSIS | ||||||||||
| 98 | |||||||||||
| 99 | For L<Catalyst> v5.90090+ | ||||||||||
| 100 | |||||||||||
| 101 | package MyApp; | ||||||||||
| 102 | |||||||||||
| 103 | use Catalyst; | ||||||||||
| 104 | |||||||||||
| 105 | MyApp->request_class_traits(['Catalyst::TraitFor::Request::ContentNegotiationHelpers']); | ||||||||||
| 106 | MyApp->setup; | ||||||||||
| 107 | |||||||||||
| 108 | For L<Catalyst> older than v5.90090 | ||||||||||
| 109 | |||||||||||
| 110 | package MyApp; | ||||||||||
| 111 | |||||||||||
| 112 | use Catalyst; | ||||||||||
| 113 | use CatalystX::RoleApplicator; | ||||||||||
| 114 | |||||||||||
| 115 | MyApp->apply_request_class_roles('Catalyst::TraitFor::Request::ContentNegotiationHelpers'); | ||||||||||
| 116 | MyApp->setup; | ||||||||||
| 117 | |||||||||||
| 118 | In a controller: | ||||||||||
| 119 | |||||||||||
| 120 | package MyApp::Controller::Example; | ||||||||||
| 121 | |||||||||||
| 122 | use Moose; | ||||||||||
| 123 | use MooseX::MethodAttributes; | ||||||||||
| 124 | |||||||||||
| 125 | sub myaction :Local { | ||||||||||
| 126 | my ($self, $c) = @_; | ||||||||||
| 127 | my $best_media_type = $c->req->choose_media_type('application/json', 'text/html'); | ||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | sub choose :Local { | ||||||||||
| 131 | my ($self, $c) = @_; | ||||||||||
| 132 | my $body = $c->req->on_best_media_type( | ||||||||||
| 133 | 'no_match' => sub { 'none' }, | ||||||||||
| 134 | 'text/html' => sub { 'html' }, | ||||||||||
| 135 | 'application/json' => sub { 'json' }); | ||||||||||
| 136 | |||||||||||
| 137 | $c->res->body($body); | ||||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | sub filter : Local { | ||||||||||
| 141 | my ($self, $c) = @_; | ||||||||||
| 142 | my @acceptable = $c->req->accepts_media_type('image/jpeg', 'text/html', 'text/plain'); | ||||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | |||||||||||
| 146 | =head1 DESCRIPTION | ||||||||||
| 147 | |||||||||||
| 148 | When using L<Catalyst> and developing web APIs it can be desirable to examine | ||||||||||
| 149 | the state of HTTP Headers of the request to make decisions about what to do, | ||||||||||
| 150 | for example what format to return (HTML, XML, JSON, other). This role can | ||||||||||
| 151 | be applied to your L<Catalyst::Request> to add some useful helper methods for | ||||||||||
| 152 | the more common types of server side content negotiation. | ||||||||||
| 153 | |||||||||||
| 154 | Most of the real work is done by L<HTTP::Headers::ActionPack::ContentNegotiation>, | ||||||||||
| 155 | this role just seeks to make it easier to use those features. | ||||||||||
| 156 | |||||||||||
| 157 | =head1 ATTRIBUTES | ||||||||||
| 158 | |||||||||||
| 159 | This role defines the following attributes. | ||||||||||
| 160 | |||||||||||
| 161 | =head2 content_negotiator | ||||||||||
| 162 | |||||||||||
| 163 | This is a 'bare' attribute with no direct accessors. It expose a few methods to | ||||||||||
| 164 | assist in content negotation. | ||||||||||
| 165 | |||||||||||
| 166 | =head1 METHODS | ||||||||||
| 167 | |||||||||||
| 168 | This role defines the following methods: | ||||||||||
| 169 | |||||||||||
| 170 | =head2 choose_media_type (@array_of_types) | ||||||||||
| 171 | |||||||||||
| 172 | Given an array of possible media types ('application/json', 'text/html', etc.) | ||||||||||
| 173 | return the one that is the best match for the current request (by looking at the | ||||||||||
| 174 | current request ACCEPT header, parsing it and comparing). | ||||||||||
| 175 | |||||||||||
| 176 | my $best_type = $c->req->accepts_media_type('image/jpeg', 'text/html', 'text/plain'); | ||||||||||
| 177 | |||||||||||
| 178 | Returns undefined if no types match. | ||||||||||
| 179 | |||||||||||
| 180 | =head2 accepts_media_type ($type | @types) | ||||||||||
| 181 | |||||||||||
| 182 | For each media type passed as in a list of arguments, return that media type IF | ||||||||||
| 183 | the type is acceptable to the requesting client. Can be used in boolean context | ||||||||||
| 184 | to determine if a single or list of types is acceptable or as a filter to permit | ||||||||||
| 185 | on those types that are acceptable: | ||||||||||
| 186 | |||||||||||
| 187 | if($c->req->accepts_media_type('application/json')) { | ||||||||||
| 188 | # handle JSON | ||||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | my @acceptable = $c->req->accepts_media_type('image/jpeg', 'text/html', 'text/plain'); | ||||||||||
| 192 | |||||||||||
| 193 | If nothing is acceptable an undef will be returned. | ||||||||||
| 194 | |||||||||||
| 195 | =head2 on_best_media_type (%callbacks) | ||||||||||
| 196 | |||||||||||
| 197 | Given a hash where the keys are media types and the values are coderefs, execute | ||||||||||
| 198 | and return the value of the coderef whose key is the best match for that media type | ||||||||||
| 199 | (based on the result of L</choose_media_type>. For example: | ||||||||||
| 200 | |||||||||||
| 201 | my $body = $c->req->on_best_media_type( | ||||||||||
| 202 | 'no_match' => sub { 'none' }, | ||||||||||
| 203 | 'text/html' => sub { 'html' }, | ||||||||||
| 204 | 'application/json' => sub { 'json' }); | ||||||||||
| 205 | |||||||||||
| 206 | $c->res->body($body); | ||||||||||
| 207 | |||||||||||
| 208 | The coderef will receive the current request object as its single argument. | ||||||||||
| 209 | |||||||||||
| 210 | If there are no matches, execute the coderef associated with a 'no_match' key | ||||||||||
| 211 | or return undef if no such key exists. When executing the 'no_match' callback | ||||||||||
| 212 | (if any) we also pass a hash of the other callbacks, which you might use for | ||||||||||
| 213 | setting a default response, or to inspect as part of the information required. | ||||||||||
| 214 | |||||||||||
| 215 | 'no_match' => sub { | ||||||||||
| 216 | my ($req, %callbacks) = @_; | ||||||||||
| 217 | my @allowed = keys %callbacks; | ||||||||||
| 218 | ... | ||||||||||
| 219 | } | ||||||||||
| 220 | |||||||||||
| 221 | In this case the 'no_match' callback is removed from '%callbacks' passed to prevent | ||||||||||
| 222 | the possibility of recursion. | ||||||||||
| 223 | |||||||||||
| 224 | =head2 choose_language (@array_of_langauges) | ||||||||||
| 225 | |||||||||||
| 226 | Given an array of possible media types ('en-US', 'es', etc.) | ||||||||||
| 227 | return the one that is the best match for the current request. | ||||||||||
| 228 | |||||||||||
| 229 | =head2 accepts_language ($type) | ||||||||||
| 230 | |||||||||||
| 231 | Like L</accepts_media_type> but for request language. | ||||||||||
| 232 | |||||||||||
| 233 | =head2 on_best_language (%callbacks) | ||||||||||
| 234 | |||||||||||
| 235 | Works like L</on_best_media_type> but matches language. | ||||||||||
| 236 | |||||||||||
| 237 | =head2 choose_charset (@array_of_character_sets) | ||||||||||
| 238 | |||||||||||
| 239 | Given an array of possible media types ("UTF-8", "US-ASCII", etc.) | ||||||||||
| 240 | return the one that is the best match for the current request. | ||||||||||
| 241 | |||||||||||
| 242 | =head2 accepts_charset ($type) | ||||||||||
| 243 | |||||||||||
| 244 | Like L</accepts_media_type> but for request character set. | ||||||||||
| 245 | |||||||||||
| 246 | =head2 on_best_charset (%callbacks) | ||||||||||
| 247 | |||||||||||
| 248 | Works like L</on_best_media_type> but matches charset. | ||||||||||
| 249 | |||||||||||
| 250 | =head2 choose_encoding (@array_of_encodings) | ||||||||||
| 251 | |||||||||||
| 252 | Given an array of possible encodings ("gzip", "identity", etc.) | ||||||||||
| 253 | return the one that is the best match for the current request. | ||||||||||
| 254 | |||||||||||
| 255 | =head2 accepts_encoding ($type) | ||||||||||
| 256 | |||||||||||
| 257 | Like L</accepts_media_type> but for request encoding. | ||||||||||
| 258 | |||||||||||
| 259 | =head2 on_best_encoding (%callbacks) | ||||||||||
| 260 | |||||||||||
| 261 | Works like L</on_best_media_type> but matches encoding. | ||||||||||
| 262 | |||||||||||
| 263 | =head2 raw_choose_media_type | ||||||||||
| 264 | |||||||||||
| 265 | =head2 raw_choose_language | ||||||||||
| 266 | |||||||||||
| 267 | =head2 raw_choose_charset | ||||||||||
| 268 | |||||||||||
| 269 | =head2 raw_choose_encoding | ||||||||||
| 270 | |||||||||||
| 271 | These are methods that map directly to the underlying L<HTTP::Headers::ActionPack::ContentNegotiation> | ||||||||||
| 272 | object. The are basicallty the same functionality except they require two arguments | ||||||||||
| 273 | (an additional one to support the HTTP header string). You generally won't need them unless | ||||||||||
| 274 | you need to compare am HTTP header that is not one that is part of the current request. | ||||||||||
| 275 | |||||||||||
| 276 | =head1 Action Roles | ||||||||||
| 277 | |||||||||||
| 278 | The following ActionRoles are part of this distribution and are offered as | ||||||||||
| 279 | an alternative method for constructing your controllers | ||||||||||
| 280 | |||||||||||
| 281 | =over 4 | ||||||||||
| 282 | |||||||||||
| 283 | =item L<Catalyst::ActionRole::ProvidesMedia> | ||||||||||
| 284 | |||||||||||
| 285 | =back | ||||||||||
| 286 | |||||||||||
| 287 | =head1 AUTHOR | ||||||||||
| 288 | |||||||||||
| 289 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||
| 290 | |||||||||||
| 291 | =head1 SEE ALSO | ||||||||||
| 292 | |||||||||||
| 293 | L<Catalyst>, L<Catalyst::Request>, L<HTTP::Headers::ActionPack::ContentNegotiation> | ||||||||||
| 294 | |||||||||||
| 295 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 296 | |||||||||||
| 297 | Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||
| 298 | |||||||||||
| 299 | This library is free software; you can redistribute it and/or modify it under | ||||||||||
| 300 | the same terms as Perl itself. | ||||||||||
| 301 | |||||||||||
| 302 | =cut | ||||||||||