finished the scheme matching and uri_for updates
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionRole / Scheme.pm
1 package Catalyst::ActionRole::Scheme;
2
3 use Moose::Role;
4
5 requires 'match', 'match_captures', 'list_extra_info';
6
7 around ['match','match_captures'] => sub {
8     my ($orig, $self, $ctx, @args) = @_;
9     my $request_scheme = lc($ctx->req->env->{'psgi.url_scheme'});
10     my $match_scheme = lc($self->scheme||'');
11
12     return $request_scheme eq $match_scheme ? $self->$orig($ctx, @args) : 0;
13 };
14
15 around 'list_extra_info' => sub {
16   my ($orig, $self, @args) = @_;
17   return {
18     %{ $self->$orig(@args) }, 
19     Scheme => $self->attributes->{Scheme}[0]||'',
20   };
21 };
22
23 1;
24
25 =head1 NAME
26
27 Catalyst::ActionRole::ConsumesContent - Match on HTTP Request Content-Type
28
29 =head1 SYNOPSIS
30
31     package MyApp::Web::Controller::MyController;
32
33     use base 'Catalyst::Controller';
34
35     sub start : POST Chained('/') CaptureArg(0) { ... }
36
37       sub is_json       : Chained('start') Consumes('application/json') { ... }
38       sub is_urlencoded : Chained('start') Consumes('application/x-www-form-urlencoded') { ... }
39       sub is_multipart  : Chained('start') Consumes('multipart/form-data') { ... }
40       
41       ## Alternatively, for common types...
42
43       sub is_json       : Chained('start') Consume(JSON) { ... }
44       sub is_urlencoded : Chained('start') Consumes(UrlEncoded) { ... }
45       sub is_multipart  : Chained('start') Consumes(Multipart) { ... }
46
47       ## Or allow more than one type
48       
49       sub is_more_than_one
50         : Chained('start')
51         : Consumes('application/x-www-form-urlencoded')
52         : Consumes('multipart/form-data')
53       {
54         ## ... 
55       }
56
57       1;
58
59 =head1 DESCRIPTION
60
61 This is an action role that lets your L<Catalyst::Action> match on the content
62 type of the incoming request.  
63
64 Generally when there's a PUT or POST request, there's a request content body
65 with a matching MIME content type.  Commonly this will be one of the types
66 used with classic HTML forms ('application/x-www-form-urlencoded' for example)
67 but there's nothing stopping you specifying any valid content type.
68
69 For matching purposes, we match strings but the casing is insensitive.
70
71 =head1 REQUIRES
72
73 This role requires the following methods in the consuming class.
74
75 =head2 match
76
77 =head2 match_captures
78
79 Returns 1 if the action matches the existing request and zero if not.
80
81 =head1 METHODS
82
83 This role defines the following methods
84
85 =head2 match
86
87 =head2 match_captures
88
89 Around method modifier that return 1 if the request content type matches one of the
90 allowed content types (see L</http_methods>) and zero otherwise.
91
92 =head2 allowed_content_types
93
94 An array of strings that are the allowed content types for matching this action.
95
96 =head2 can_consume
97
98 Boolean.  Does the current request match content type with what this actionrole
99 can consume?
100
101 =head2 list_extra_info
102
103 Add the accepted content type to the debug screen.
104
105 =head1 AUTHORS
106
107 Catalyst Contributors, see Catalyst.pm
108
109 =head1 COPYRIGHT
110
111 This library is free software. You can redistribute it and/or modify it under
112 the same terms as Perl itself.
113
114 =cut