Add boilderplate POD
[catagits/Catalyst-Engine-STOMP.git] / lib / Catalyst / Controller / MessageDriven.pm
index d74452e..56883c0 100644 (file)
@@ -1,6 +1,8 @@
 package Catalyst::Controller::MessageDriven;
 use Moose;
 use Data::Serializer;
+use Moose::Util::TypeConstraints;
+use MooseX::Types::Moose qw/Str/;
 use namespace::autoclean;
 
 BEGIN { extends 'Catalyst::Controller' }
@@ -45,17 +47,49 @@ Dispatches to method named by the key C<< $c->stash->{request}->{type} >>
 
 Serializes the response from C<< $c->stash->{response} >>
 
+=head1 CONFIGURATION
+
+In the configuration file add the following to set the value for a parameter
+
+  <MessageDriven>
+    type_key foo
+  </MessageDriven>
+
+=head2 type_key
+
+The hash key the module will try to pull out the received message to call
+within the controller. This defaults to 'type'.
+
+=head2 serializer
+
+The serializer used to serialiser/deserialise. See Data::Serializer to see
+what is available. Defaults to YAML. JSON is anotther that is available.
+
+
 =cut
 
-__PACKAGE__->config( serializer => 'YAML' );
+class_type 'Data::Serializer';
+my $serializer_t = subtype 'Data::Serializer', where { 1 };
+coerce $serializer_t, from 'Str',
+    via { Data::Serializer->new( serializer => $_ ) };
+
+has serializer => (
+    isa => $serializer_t, is => 'ro', required => 1,
+    default => 'YAML', coerce => 1,
+);
+
+has type_key => (
+    is => 'ro', required =>1,
+    default => 'type',
+);
+
 
 sub begin : Private {
     my ($self, $c) = @_;
 
     # Deserialize the request message
-        my $message;
-    my $serializer = $self->config->{serializer};
-    my $s = Data::Serializer->new( serializer => $serializer );
+    my $message;
+    my $s = $self->serializer;
     eval {
         my $body = $c->request->body;
         open my $IN, "$body" or die "can't open temp file $body";
@@ -80,14 +114,14 @@ sub end : Private {
     my $output;
 
     # Load a serializer
-    my $serializer = $self->config->{serializer};
-    my $s = Data::Serializer->new( serializer => $serializer );
+    my $s = $self->serializer;
 
     # Custom error handler - steal errors from catalyst and dump them into
     # the stash, to get them serialized out as the reply.
      if (scalar @{$c->error}) {
-         my $error = join "\n", @{$c->error};
-         $c->stash->{response} = { status => 'ERROR', error => $error };
+        $c->log->error($_) for @{$c->error}; # Log errors in Catalyst
+        my $error = join "\n", @{$c->error};
+        $c->stash->{response} = { status => 'ERROR', error => $error };
         $output = $s->serialize( $c->stash->{response} );
         $c->clear_errors;
         $c->response->status(400);
@@ -112,7 +146,7 @@ sub default : Private {
 
     # Forward the request to the appropriate action, based on the
     # message type.
-    my $action = $c->stash->{request}->{type};
+    my $action = $c->stash->{request}->{ $self->type_key };
     if (defined $action) {
         $c->forward($action, [$c->stash->{request}]);
     }
@@ -123,3 +157,17 @@ sub default : Private {
 
 __PACKAGE__->meta->make_immutable;
 
+=head1 AUTHOR AND CONTRIBUTORS
+
+See information in L<Catalyst::Engine::Stomp>
+
+=head1 LICENCE AND COPYRIGHT
+
+Copyright (C) 2009 Venda Ltd
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8.8 or,
+at your option, any later version of Perl 5 you may have available.
+
+=cut
+