Permalink
Browse files

Added Tatsumaki::Request and ::Response to allow automatic utf8 decod…

…ing of params (in a hackish way)
  • Loading branch information...
1 parent e739c9d commit eafb4ab2f9808ec0f2d8dd6e8df7e1e5e7d6c766 @miyagawa committed Nov 4, 2009
Showing with 43 additions and 8 deletions.
  1. +1 −0 Makefile.PL
  2. +1 −3 eg/demo.pl
  3. +2 −3 lib/Tatsumaki/Application.pm
  4. +2 −2 lib/Tatsumaki/Handler.pm
  5. +31 −0 lib/Tatsumaki/Request.pm
  6. +6 −0 lib/Tatsumaki/Response.pm
View
@@ -5,6 +5,7 @@ readme_from 'lib/Tatsumaki.pm';
requires 'AnyEvent';
requires 'AnyEvent::HTTP';
requires 'Moose';
+requires 'MooseX::NonMoose';
requires 'Plack', 0.9006;
requires 'Plack::Server::AnyEvent', 0.02;
requires 'Plack::Request';
View
@@ -109,10 +109,8 @@ package ChatPostHandler;
sub post {
my($self, $channel) = @_;
- # TODO: decode should be done in the framework or middleware
my $v = $self->request->params;
- my $text = Encode::decode_utf8($v->{text});
- my $html = $self->format_message($text);
+ my $html = $self->format_message($v->{text});
my $mq = Tatsumaki::MessageQueue->instance($channel);
$mq->publish({
type => "message", html => $html, ident => $v->{ident},
@@ -1,9 +1,8 @@
package Tatsumaki::Application;
use AnyEvent;
use Moose;
-use Plack::Request;
-use Plack::Response;
use Tatsumaki::Handler;
+use Tatsumaki::Request;
use Text::MicroTemplate::File;
use Try::Tiny;
@@ -51,7 +50,7 @@ sub psgi_app {
my $self = shift;
return sub {
my $env = shift;
- my $req = Plack::Request->new($env);
+ my $req = Tatsumaki::Request->new($env);
my $handler = $self->dispatch($req->path)
or return [ 404, [ 'Content-Type' => 'text/html' ], [ "404 Not Found" ] ];
View
@@ -10,8 +10,8 @@ use Tatsumaki::Error;
has application => (is => 'rw', isa => 'Tatsumaki::Application');
has condvar => (is => 'rw', isa => 'AnyEvent::CondVar');
-has request => (is => 'rw', isa => 'Plack::Request');
-has response => (is => 'rw', isa => 'Plack::Response', lazy_build => 1);
+has request => (is => 'rw', isa => 'Tatsumaki::Request');
+has response => (is => 'rw', isa => 'Tatsumaki::Response', lazy_build => 1);
has args => (is => 'rw', isa => 'ArrayRef');
has writer => (is => 'rw');
has mxhr => (is => 'rw', isa => 'Bool');
View
@@ -0,0 +1,31 @@
+package Tatsumaki::Request;
+use Encode;
+use Moose;
+use MooseX::NonMoose;
+extends 'Plack::Request';
+
+use Tatsumaki::Response;
+
+override _build_parameters => sub {
+ my $self = shift;
+
+ my $params = super();
+
+ my $decoded_params = {};
+ while (my($k, $v) = each %$params) {
+ $decoded_params->{decode_utf8($k)} = decode_utf8($v);
+ }
+
+ return $decoded_params;
+};
+
+sub new_response {
+ my $self = shift;
+ Tatsumaki::Response->new(@_);
+}
+
+no Moose;
+__PACKAGE__->meta->make_immutable;
+
+1;
+
@@ -0,0 +1,6 @@
+package Tatsumaki::Response;
+use Moose;
+use MooseX::NonMoose;
+extends 'Plack::Response';
+
+1;

0 comments on commit eafb4ab

Please sign in to comment.