Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update Noe::Dispatcher

  • Loading branch information...
commit c305fd18820d09e12a720aff584e43a292588181 1 parent d7f0662
Yusuke Wada authored
2  Makefile.PL
View
@@ -8,7 +8,7 @@ requires 'Template';
requires 'IO::File';
requires 'Path::Class';
requires 'YAML';
-requires 'HTTPx::Dispatcher';
+requires 'HTTPx::Dispatcher' => 0.07;
requires 'Text::MicroTemplate::Extended';
recommends 'JSON::XS';
42 lib/Noe/Dispatcher.pm
View
@@ -1,11 +1,43 @@
package Noe::Dispatcher;
use strict;
use warnings;
-use HTTPx::Dispatcher;
-our @EXPORT = qw/connect match/;
+use base qw/Exporter/;
+use Scalar::Util qw/blessed/;
+use Carp;
+use HTTPx::Dispatcher::Rule;
+our @EXPORT = qw/connect match get post/;
-# XXX
-*match = \&HTTPx::Dispatcher::match;
-*connect = \&HTTPx::Dispatcher::connect;
+my $rules;
+
+sub _push {
+ push @$rules, HTTPx::Dispatcher::Rule->new(@_)
+}
+
+sub connect {
+ _push( @_ );
+}
+
+sub get {
+ my @args = @_;
+ $args[1]->{conditions} = { method => 'GET' };
+ _push( @args );
+}
+
+sub post {
+ my @args = @_;
+ $args[1]->{conditions} = { method => 'POST' };
+ _push( @args );
+}
+
+# stolen from HTTPx::Dispatcher
+sub match {
+ my ( $class, $req ) = @_;
+ for my $rule ( @$rules ) {
+ if ( my $result = $rule->match($req) ) {
+ return $result;
+ }
+ }
+ return;
+}
1;
36 t/30_dispatch.t
View
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+use lib 't/lib';
+use Test::More;
+use Plack::Request;
+use TestApp::Dispatcher;
+
+use_ok('Noe::Dispatcher');
+
+{
+ my $req =
+ Plack::Request->new(
+ { PATH_INFO => '/', REQUEST_METHOD => 'GET', } );
+ my $rule = TestApp::Dispatcher->match($req);
+ is_deeply( $rule,
+ { controller => 'Root', args => {}, action => 'index' } );
+}
+
+{
+ my $req =
+ Plack::Request->new(
+ { PATH_INFO => '/comment', REQUEST_METHOD => 'GET', } );
+ my $rule = TestApp::Dispatcher->match($req);
+ is_deeply( $rule,
+ { controller => 'Comment', args => {}, action => 'show' } );
+}
+
+{
+ my $req =
+ Plack::Request->new( { PATH_INFO => '/comment', REQUEST_METHOD => 'POST', } );
+ my $rule = TestApp::Dispatcher->match( $req );
+ is_deeply( $rule,
+ { controller => 'Comment', args => {}, action => 'post' } );
+}
+
+done_testing;
8 t/lib/TestApp/Dispatcher.pm
View
@@ -1,4 +1,8 @@
package TestApp::Dispatcher;
-use HTTPx::Dispatcher;
-connect '' => {controller => 'Root', action => 'root'};
+use Noe::Dispatcher;
+
+connect '' => { controller => 'Root', action => 'index' };
+get '/comment' => { controller => 'Comment', action => 'show' };
+post '/comment' => { controller => 'Comment', action => 'post' };
+
1;
Please sign in to comment.
Something went wrong with that request. Please try again.