File Coverage

blib/lib/POE/Component/Server/Bayeux/Message.pm
Criterion Covered Total %
statement 22 47 46.8
branch 0 2 0.0
condition n/a
subroutine 6 15 40.0
pod 8 8 100.0
total 36 72 50.0


line stmt bran cond sub pod time code
1             package POE::Component::Server::Bayeux::Message;
2              
3             =head1 NAME
4              
5             POE::Component::Server::Bayeux::Client::Message - An object representing a single message of a request
6              
7             =head1 DESCRIPTION
8              
9             Used internally by L.
10              
11             This is the parent class of the different message types (Meta, Service, Publish, by default).
12             Each message can override or call via SUPER the object methods here.
13              
14             =cut
15              
16 3     3   17 use strict;
  3         5  
  3         3802  
17 3     3   181 use warnings;
  3         6  
  3         761  
18              
19 3     3   184 use Params::Validate qw(validate validate_with);
  3         5  
  3         1168  
20 3     3   15 use base qw(Class::Accessor);
  3         12  
  3         1736  
21              
22             =head1 METHODS
23              
24             =head2 Accessors
25              
26             =over 4
27              
28             Accessors to this objects hashref.
29              
30             =over 4
31              
32             =item is_error
33              
34             =item request
35              
36             =back
37              
38             =back
39              
40             =cut
41              
42             __PACKAGE__->mk_accessors(qw(
43             is_error
44             request
45             ));
46              
47             =head2 Data Accessors
48              
49             =over 4
50              
51             These access the named field in the message payload
52              
53             =over 4
54              
55             =item channel
56              
57             =item version
58              
59             =item minimumVersion
60              
61             =item supportedConnectionTypes
62              
63             =item clientId
64              
65             =item advice
66              
67             =item connectionType
68              
69             =item id
70              
71             =item timestamp
72              
73             =item data
74              
75             =item connectionId
76              
77             =item successful
78              
79             =item subscription
80              
81             =item error
82              
83             =item ext
84              
85             =back
86              
87             =back
88              
89             =cut
90              
91             # From '3. Message Field Definitions' of the protocol draft
92             __PACKAGE__->mk_data_accessors(qw(
93             channel
94             version
95             minimumVersion
96             supportedConnectionTypes
97             clientId
98             advice
99             connectionType
100             id
101             timestamp
102             data
103             connectionId
104             successful
105             subscription
106             error
107             ext
108             ));
109              
110             ## Class Methods ###
111              
112             sub new {
113 0     0 1 0 my $class = shift;
114              
115 0         0 my %args = validate(@_, {
116             request => 1,
117             data => 1,
118             });
119              
120 0         0 return bless \%args, $class;
121             }
122              
123             ## Object Methods ###
124              
125             =head2 server_config ()
126              
127             =over 4
128              
129             Returns the server's args
130              
131             =back
132              
133             =cut
134              
135             sub server_config {
136 0     0 1 0 my ($self) = @_;
137              
138 0         0 return $self->request->heap->{args};
139             }
140              
141             =head2 pre_handle ()
142              
143             =over 4
144              
145             Called by the request before handle(). Enables the message to affect the
146             queueing of the other messages in the request, or do anything else it wants.
147              
148             =back
149              
150             =cut
151              
152             sub pre_handle {
153 0     0 1 0 my ($self) = @_;
154              
155             # do nothing
156             }
157              
158             =head2 handle ()
159              
160             =over 4
161              
162             At a minimum, validates the fields of the message payload. A message will usually
163             add a response in this block:
164              
165             $message->request->add_response({ successful => 1 });
166              
167             =back
168              
169             =cut
170              
171             sub handle {
172 0     0 1 0 my ($self) = @_;
173              
174 0         0 $self->validate_fields();
175             }
176              
177             =head2 post_handle ()
178              
179             =over 4
180              
181             Like pre_handle(), but called after the handle() phase.
182              
183             =back
184              
185             =cut
186              
187             sub post_handle {
188 0     0 1 0 my ($self) = @_;
189              
190             # do nothing
191             }
192              
193             =head2 validate_fields (%spec)
194              
195             =over 4
196              
197             Given a L spec, will test the payload for validity. Failure
198             causes an error message stored in is_error().
199              
200             =back
201              
202             =cut
203              
204             sub validate_fields {
205 0     0 1 0 my ($self, %spec) = @_;
206              
207 0         0 %spec = (
208             %spec,
209              
210             # Globally required
211             channel => {
212             regex => qr{^\S+$},
213             },
214              
215             # Globally optional
216             id => 0,
217             ext => 0,
218             );
219              
220 0         0 eval {
221 0         0 validate_with(
222             params => [ %{ $self->{data} } ],
223             spec => \%spec,
224             on_fail => sub {
225 0     0   0 my $error = shift;
226 0         0 chomp $error;
227 0         0 $self->is_error($error);
228 0         0 die;
229             },
230 0         0 allow_extra => 1,
231             )
232             };
233             }
234              
235             =head1 CLASS METHODS
236              
237             =head2 new (..)
238              
239             =over 4
240              
241             Basic new() call, needs only 'request' and 'data'.
242              
243             =back
244              
245             =head2 payload
246              
247             =over 4
248              
249             Returns the message payload
250              
251             =back
252              
253             =cut
254              
255             sub payload {
256 0     0 1 0 my $self = shift;
257 0         0 return $self->{data};
258             }
259              
260             =head2 mk_data_accessors (@method_names)
261              
262             =over 4
263              
264             Generates object accessor methods for the named methods. Supplements the generic
265             methods that are created for all message types.
266              
267             =back
268              
269             =cut
270              
271             sub mk_data_accessors {
272 6     6 1 22 my ($class, @accessors) = @_;
273              
274 6         15 foreach my $accessor (@accessors) {
275 48         75 my $method_name = $class . '::' . $accessor;
276             my $sub = sub {
277 0     0   0 my ($self, $value) = @_;
278              
279 0 0       0 if (defined $value) {
280 0         0 $self->{data}{$accessor} = $value;
281             # Chain it
282 0         0 return $self;
283             }
284 0         0 return $self->{data}{$accessor};
285 48         153 };
286              
287             {
288 3     3   23 no strict 'refs';
  3         10  
  3         204  
  48         52  
289 48         47 *{ $method_name } = $sub;
  48         301  
290             }
291             }
292             }
293              
294             =head1 COPYRIGHT
295              
296             Copyright (c) 2008 Eric Waters and XMission LLC (http://www.xmission.com/).
297             All rights reserved. This program is free software; you can redistribute it
298             and/or modify it under the same terms as Perl itself.
299              
300             The full text of the license can be found in the LICENSE file included with
301             this module.
302              
303             =head1 AUTHOR
304              
305             Eric Waters
306              
307             =cut
308              
309             1;