POE::Component::Server::Bayeux::Message::Factory - create messages in the right subclass


POE-Component-Server-Bayeux documentation Contained in the POE-Component-Server-Bayeux distribution.

Index


Code Index:

NAME

Top

POE::Component::Server::Bayeux::Message::Factory - create messages in the right subclass

DESCRIPTION

Top

Implements create(), which will find the appropriate Message subclass to handle the data packet.

COPYRIGHT

Top

AUTHOR

Top

Eric Waters <ewaters@uarc.com>


POE-Component-Server-Bayeux documentation Contained in the POE-Component-Server-Bayeux distribution.
package POE::Component::Server::Bayeux::Message::Factory;

use strict;
use warnings;
use POE qw(
    Component::Server::Bayeux::Message::Invalid
    Component::Server::Bayeux::Message::Meta
    Component::Server::Bayeux::Message::Service
    Component::Server::Bayeux::Message::Publish
);

use Params::Validate qw(validate HASHREF ARRAYREF);

sub create {
    my $class = shift;

    my %args = validate(@_, {
        request => 1,
        data    => { type => HASHREF },
    });

    my $channel = $args{data}{channel};

    my $build_class;
    if (! $channel) {
        $build_class = 'Invalid';
    }
    elsif ($channel =~ m{^/meta/}) {
        $build_class = 'Meta';
    }
    elsif ($channel =~ m{^/service/}) {
        $build_class = 'Service';
    }
    else {
        $build_class = 'Publish';
    }

    $build_class = 'POE::Component::Server::Bayeux::Message::' . $build_class;
    return $build_class->new(%args);
}

1;