lib/Catalyst/ActionRole/ConsumesContent.pm

filter: Statementstmt 90.3% 28 / 31 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 89.5% 43 / 48 CC 6 SCAR 8.6
CC
6
Cov
90%
__ANON__
3.0
_build_allowed_content_types
3.0
can_consume
2.0
CRAP 2.4
« lib/Catalyst/Request/Upload.pm ↑ Summary lib/Catalyst/ActionRole/HTTPMethods.pm »
1 package Catalyst::ActionRole::ConsumesContent;
2  
3 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 requires 'match', 'match_captures', 'list_extra_info';
6  
7 4 has allowed_content_types => (
8 is=>'ro',
9 required=>1,
10 lazy=>1,
11 isa=>'ArrayRef',
12 builder=>'_build_allowed_content_types');
13  
14 4 has normalized => (
15 is=>'ro',
16 required=>1,
17 lazy=>1,
18 isa=>'HashRef',
19 builder=>'_build_normalized');
20  
21  
22 13 ▶sub _build_normalized {
Subroutine _build_normalized: called CC=1 SCAR=0.0
23 return +{
24 13 JSON => 'application/json',
25 JS => 'application/javascript',
26 PERL => 'application/perl',
27 HTML => 'text/html',
28 XML => 'text/XML',
29 Plain => 'text/plain',
30 UrlEncoded => 'application/x-www-form-urlencoded',
31 Multipart => 'multipart/form-data',
32 HTMLForm => ['application/x-www-form-urlencoded','multipart/form-data'],
33 };
34 }
35  
36 13 ▶sub _build_allowed_content_types {
Subroutine _build_allowed_content_types: called CC=3 SCAR=11.0
37 13 my $self = shift;
38 13 my @proto = map {split ',', $_ } @{$self->attributes->{Consumes}};
39 my @converted = map {
40 13 ▶ if(my $normalized = $self->normalized->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $normalized = $self->normalized->{$_}) { } 8 7 15
41 8 ▶ ref $normalized ? @$normalized : ($normalized);
Branch: 1 branch
BranchTrueFalseTotal
ref $normalized ? : 1 7 8
42 } else {
43 7 $_;
44 }
45 } @proto;
46  
47 13 return \@converted;
48 }
49  
50 57 ▶around ['match','match_captures'] => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
51 57 my ($orig, $self, $ctx, @args) = @_;
52 57 ▶ if(my $content_type = $ctx->req->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content_type = $ctx->req->content_type) 57 0 57
53 57 ▶ return 0 unless $self->can_consume($content_type);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_consume($content_type) 30 27 57
54 }
55 27 return $self->$orig($ctx, @args);
56 4 };
57  
58 57 ▶sub can_consume {
Subroutine can_consume: called CC=2 SCAR=6.9
Pod documented
59 57 my ($self, $request_content_type) = @_;
60 63 my @matches = grep { lc($_) eq lc($request_content_type) }
61 57 @{$self->allowed_content_types};
62 57 ▶ return @matches ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
@matches ? : 27 30 57
63 }
64  
65 0 ▶around 'list_extra_info' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my ($orig, $self, @args) = @_;
67 return {
68 0 %{ $self->$orig(@args) },
69 CONSUMES => $self->allowed_content_types,
70 };
71 4 };
72  
73 4 1;
74  
75 =head1 NAME
76  
77 Catalyst::ActionRole::ConsumesContent - Match on HTTP Request Content-Type
78  
79 =head1 SYNOPSIS
80  
81 package MyApp::Web::Controller::MyController;
82  
83 use base 'Catalyst::Controller';
84  
85 sub start : POST Chained('/') CaptureArg(0) { ... }
86  
87 sub is_json : Chained('start') Consumes('application/json') { ... }
88 sub is_urlencoded : Chained('start') Consumes('application/x-www-form-urlencoded') { ... }
89 sub is_multipart : Chained('start') Consumes('multipart/form-data') { ... }
90  
91 ## Alternatively, for common types...
92  
93 sub is_json : Chained('start') Consume(JSON) { ... }
94 sub is_urlencoded : Chained('start') Consumes(UrlEncoded) { ... }
95 sub is_multipart : Chained('start') Consumes(Multipart) { ... }
96  
97 ## Or allow more than one type
98  
99 sub is_more_than_one
100 : Chained('start')
101 : Consumes('application/x-www-form-urlencoded')
102 : Consumes('multipart/form-data')
103 {
104 ## ...
105 }
106  
107 1;
108  
109 =head1 DESCRIPTION
110  
111 This is an action role that lets your L<Catalyst::Action> match on the content
112 type of the incoming request.
113  
114 Generally when there's a PUT or POST request, there's a request content body
115 with a matching MIME content type. Commonly this will be one of the types
116 used with classic HTML forms ('application/x-www-form-urlencoded' for example)
117 but there's nothing stopping you specifying any valid content type.
118  
119 For matching purposes, we match strings but the casing is insensitive.
120  
121 =head1 REQUIRES
122  
123 This role requires the following methods in the consuming class.
124  
125 =head2 match
126  
127 =head2 match_captures
128  
129 Returns 1 if the action matches the existing request and zero if not.
130  
131 =head1 METHODS
132  
133 This role defines the following methods
134  
135 =head2 match
136  
137 =head2 match_captures
138  
139 Around method modifier that return 1 if the request content type matches one of the
140 allowed content types (see L</http_methods>) and zero otherwise.
141  
142 =head2 allowed_content_types
143  
144 An array of strings that are the allowed content types for matching this action.
145  
146 =head2 can_consume
147  
148 Boolean. Does the current request match content type with what this actionrole
149 can consume?
150  
151 =head2 list_extra_info
152  
153 Add the accepted content type to the debug screen.
154  
155 =head1 AUTHORS
156  
157 Catalyst Contributors, see Catalyst.pm
158  
159 =head1 COPYRIGHT
160  
161 This library is free software. You can redistribute it and/or modify it under
162 the same terms as Perl itself.
163  
164 =cut
« lib/Catalyst/Request/Upload.pm ↑ Summary lib/Catalyst/ActionRole/HTTPMethods.pm »