Server/Bayeux/Message.pm

filter: Statementstmt 50.0% 25 / 50 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 40.0% 6 / 15 Podpod 100% 8 / 8 totaltotal 52.0% 39 / 75 CC 3 SCAR 7.9
CC
3
Cov
48%
__ANON__
6.0
mk_data_accessors
2.3
new
2.0
CRAP 2.2
« Client/Bayeux/Transport.pm ↑ Summary Server/Bayeux/Message/Invalid.pm »
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<POE::Component::Server::Bayeux::Request>.
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use Params::Validate qw(validate validate_with);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 3 __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 3 __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 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
113 0 my $class = shift;
114  
115 0 my %args = validate(@_, {
116 request => 1,
117 data => 1,
118 });
119  
120 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 0 ▶sub server_config {
Subroutine server_config: not called CC=1 SCAR=6.9
Pod documented
136 0 my ($self) = @_;
137  
138 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 0 ▶sub pre_handle {
Subroutine pre_handle: not called CC=1 SCAR=6.9
Pod documented
153 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 0 ▶sub handle {
Subroutine handle: not called CC=1 SCAR=6.9
Pod documented
172 0 my ($self) = @_;
173  
174 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 0 ▶sub post_handle {
Subroutine post_handle: not called CC=1 SCAR=6.9
Pod documented
188 0 my ($self) = @_;
189  
190 # do nothing
191 }
192  
193 =head2 validate_fields (%spec)
194  
195 =over 4
196  
197 Given a L<Params::Validate> 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 0 ▶sub validate_fields {
Subroutine validate_fields: not called CC=1 SCAR=6.9
Pod documented
205 0 my ($self, %spec) = @_;
206  
207 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 eval {
221 validate_with(
222 0 params => [ %{ $self->{data} } ],
223 spec => \%spec,
224 0 ▶ on_fail => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 0 my $error = shift;
226 0 chomp $error;
227 0 $self->is_error($error);
228 0 die;
229 },
230 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 0 ▶sub payload {
Subroutine payload: not called CC=1 SCAR=6.9
Pod documented
256 0 my $self = shift;
257 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 6 ▶sub mk_data_accessors {
Subroutine mk_data_accessors: called CC=2 SCAR=8.2
Pod documented
272 6 my ($class, @accessors) = @_;
273  
274 6 foreach my $accessor (@accessors) {
275 48 my $method_name = $class . '::' . $accessor;
276 0 ▶ my $sub = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
277 0 my ($self, $value) = @_;
278  
279 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
280 0 $self->{data}{$accessor} = $value;
281 # Chain it
282 0 return $self;
283 }
284 0 return $self->{data}{$accessor};
285 48 };
286  
287 {
288 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
289 48 *{ $method_name } = $sub;
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 <ewaters@uarc.com>
306  
307 =cut
308  
309 3 1;
« Client/Bayeux/Transport.pm ↑ Summary Server/Bayeux/Message/Invalid.pm »