File Coverage

blib/lib/POE/Component/Server/Bayeux/Message/Service.pm
Criterion Covered Total %
statement 9 46 19.5
branch 0 20 0.0
condition 0 15 0.0
subroutine 3 5 60.0
pod 2 2 100.0
total 14 88 15.9


line stmt bran cond sub pod time code
1             package POE::Component::Server::Bayeux::Message::Service;
2              
3             =head1 NAME
4              
5             POE::Component::Server::Bayeux::Message::Service - handles /service/ channels
6              
7             =head1 DESCRIPTION
8              
9             Subclasses L to implement the /service/* channels.
10             Does nothing by itself, as the Bayeux protocol doesn't define any specific services. Implements named services from the server config 'Services' - see the docs there.
11              
12             =cut
13              
14 3     3   2494 use strict;
  3         8  
  3         123  
15 3     3   20 use warnings;
  3         6  
  3         105  
16 3     3   16 use base qw(POE::Component::Server::Bayeux::Message);
  3         6  
  3         1723  
17              
18             __PACKAGE__->mk_accessors(qw(method handler));
19              
20             sub new {
21 0     0 1   my $class = shift;
22 0           my $self = $class->SUPER::new(@_);
23              
24             # Extract and save the service method and handler
25              
26 0           my ($method) = $self->channel =~ m{^/service/(.+)$};
27 0 0         if (! $method) {
28 0           $self->request->error("Must provide service method");
29 0           return;
30             }
31 0           my $handler = $method;
32              
33 0           my $known_methods = $self->server_config->{Services};
34              
35             # Allow for generic _handler handler
36 0 0 0       if (! $known_methods->{$method} && $known_methods->{_handler}) {
    0          
37 0           $handler = '_handler';
38             }
39             elsif (! $known_methods->{$method}) {
40 0           $self->request->error("Invalid service method $method");
41 0           return;
42             }
43              
44 0           $self->method($method);
45 0           $self->handler($handler);
46              
47 0           return $self;
48             }
49              
50             sub handle {
51 0     0 1   my ($self) = @_;
52              
53             # Class handle() will call validate_fields()
54 0           $self->SUPER::handle();
55              
56 0           my @responses;
57              
58 0 0         if (! $self->is_error) {
59 0           my $service_definition = $self->server_config->{Services}{ $self->handler };
60 0 0 0       if (ref $service_definition && ref $service_definition eq 'CODE') {
61 0           my @result;
62 0           eval {
63 0           @result = $service_definition->($self);
64             };
65 0 0         if (my $ex = $@) {
66 0           my $text;
67 0 0 0       if (ref($ex) && $ex->can('error')) {
68 0           $text = $ex->error;
69             }
70             else {
71 0           $text = $ex . '';
72             }
73 0           $self->is_error("Failed to execute method '".$self->handler."' coderef: $text");
74             }
75 0 0         push @responses, @result if @result;
76             }
77             }
78              
79 0 0         if ($self->is_error) {
80 0           push @responses, {
81             successful => JSON::XS::false,
82             error => $self->is_error,
83             };
84             }
85              
86 0           foreach my $response (@responses) {
87 0   0       $response->{channel} ||= $self->channel;
88 0 0 0       $response->{id} ||= $self->id if $self->id;
89 0           $self->request->add_response($response);
90             }
91             }
92              
93             =head1 COPYRIGHT
94              
95             Copyright (c) 2008 Eric Waters and XMission LLC (http://www.xmission.com/).
96             All rights reserved. This program is free software; you can redistribute it
97             and/or modify it under the same terms as Perl itself.
98              
99             The full text of the license can be found in the LICENSE file included with
100             this module.
101              
102             =head1 AUTHOR
103              
104             Eric Waters
105              
106             =cut
107              
108              
109             1;