Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

update Noe::Dispatcher

  • Loading branch information...
commit c305fd18820d09e12a720aff584e43a292588181 1 parent d7f0662
Yusuke Wada authored
2  Makefile.PL
@@ -8,7 +8,7 @@ requires 'Template';
8 8 requires 'IO::File';
9 9 requires 'Path::Class';
10 10 requires 'YAML';
11   -requires 'HTTPx::Dispatcher';
  11 +requires 'HTTPx::Dispatcher' => 0.07;
12 12 requires 'Text::MicroTemplate::Extended';
13 13
14 14 recommends 'JSON::XS';
42 lib/Noe/Dispatcher.pm
... ... @@ -1,11 +1,43 @@
1 1 package Noe::Dispatcher;
2 2 use strict;
3 3 use warnings;
4   -use HTTPx::Dispatcher;
5   -our @EXPORT = qw/connect match/;
  4 +use base qw/Exporter/;
  5 +use Scalar::Util qw/blessed/;
  6 +use Carp;
  7 +use HTTPx::Dispatcher::Rule;
  8 +our @EXPORT = qw/connect match get post/;
6 9
7   -# XXX
8   -*match = \&HTTPx::Dispatcher::match;
9   -*connect = \&HTTPx::Dispatcher::connect;
  10 +my $rules;
  11 +
  12 +sub _push {
  13 + push @$rules, HTTPx::Dispatcher::Rule->new(@_)
  14 +}
  15 +
  16 +sub connect {
  17 + _push( @_ );
  18 +}
  19 +
  20 +sub get {
  21 + my @args = @_;
  22 + $args[1]->{conditions} = { method => 'GET' };
  23 + _push( @args );
  24 +}
  25 +
  26 +sub post {
  27 + my @args = @_;
  28 + $args[1]->{conditions} = { method => 'POST' };
  29 + _push( @args );
  30 +}
  31 +
  32 +# stolen from HTTPx::Dispatcher
  33 +sub match {
  34 + my ( $class, $req ) = @_;
  35 + for my $rule ( @$rules ) {
  36 + if ( my $result = $rule->match($req) ) {
  37 + return $result;
  38 + }
  39 + }
  40 + return;
  41 +}
10 42
11 43 1;
36 t/30_dispatch.t
... ... @@ -0,0 +1,36 @@
  1 +use strict;
  2 +use warnings;
  3 +use lib 't/lib';
  4 +use Test::More;
  5 +use Plack::Request;
  6 +use TestApp::Dispatcher;
  7 +
  8 +use_ok('Noe::Dispatcher');
  9 +
  10 +{
  11 + my $req =
  12 + Plack::Request->new(
  13 + { PATH_INFO => '/', REQUEST_METHOD => 'GET', } );
  14 + my $rule = TestApp::Dispatcher->match($req);
  15 + is_deeply( $rule,
  16 + { controller => 'Root', args => {}, action => 'index' } );
  17 +}
  18 +
  19 +{
  20 + my $req =
  21 + Plack::Request->new(
  22 + { PATH_INFO => '/comment', REQUEST_METHOD => 'GET', } );
  23 + my $rule = TestApp::Dispatcher->match($req);
  24 + is_deeply( $rule,
  25 + { controller => 'Comment', args => {}, action => 'show' } );
  26 +}
  27 +
  28 +{
  29 + my $req =
  30 + Plack::Request->new( { PATH_INFO => '/comment', REQUEST_METHOD => 'POST', } );
  31 + my $rule = TestApp::Dispatcher->match( $req );
  32 + is_deeply( $rule,
  33 + { controller => 'Comment', args => {}, action => 'post' } );
  34 +}
  35 +
  36 +done_testing;
8 t/lib/TestApp/Dispatcher.pm
... ... @@ -1,4 +1,8 @@
1 1 package TestApp::Dispatcher;
2   -use HTTPx::Dispatcher;
3   -connect '' => {controller => 'Root', action => 'root'};
  2 +use Noe::Dispatcher;
  3 +
  4 +connect '' => { controller => 'Root', action => 'index' };
  5 +get '/comment' => { controller => 'Comment', action => 'show' };
  6 +post '/comment' => { controller => 'Comment', action => 'post' };
  7 +
4 8 1;

0 comments on commit c305fd1

Please sign in to comment.
Something went wrong with that request. Please try again.