diff --git a/lib/HTTPx/Dispatcher.pm b/lib/HTTPx/Dispatcher.pm index 8e7c77e..ab2a3fa 100644 --- a/lib/HTTPx/Dispatcher.pm +++ b/lib/HTTPx/Dispatcher.pm @@ -8,32 +8,29 @@ use Scalar::Util qw/blessed/; use Carp; use base qw/Exporter/; -our @EXPORT = qw/connect match uri_for get post/; +our @EXPORT = qw/connect match uri_for/; my $rules; sub connect { my $pkg = caller(0); - my @args = @_; - push @{ $rules->{$pkg} }, HTTPx::Dispatcher::Rule->new(@args); -} - -#XXX -sub method { - my ( $name, $pkg, @args ) = @_; - push @{ $rules->{$pkg} }, HTTPx::Dispatcher::Rule->new(@args, $name ); + my @rules; + #XXX + my ($path, $method); + for ( @_ ){ + if( ref $_ eq 'HASH' ){ + push @rules, [ $path, $_, $method ]; + }elsif ( $_ =~ /^(?:get|post)$/ ) { + $method = $_; + }else{ + $path = $_; + } + } + for ( @rules ){ + push @{ $rules->{$pkg} }, HTTPx::Dispatcher::Rule->new(@$_); + } } -sub get { - my $pkg = caller(0); - method('get', $pkg, @_ ); -}; - -sub post { - my $pkg = caller(0); - method('post', $pkg, @_ ); -}; - sub match { my ( $class, $req ) = @_; croak "request required" unless blessed $req; diff --git a/lib/HTTPx/Dispatcher/Rule.pm b/lib/HTTPx/Dispatcher/Rule.pm index 9bc12dc..c2d573f 100644 --- a/lib/HTTPx/Dispatcher/Rule.pm +++ b/lib/HTTPx/Dispatcher/Rule.pm @@ -9,10 +9,9 @@ __PACKAGE__->mk_accessors(qw/re pattern controller action capture requirements c sub new { my ($class, $pattern, $args, $method) = @_; - use Data::Dumper; $args ||= {}; $args->{conditions} ||= {}; - $args->{method} = $method || ''; + $args->{method} = $method || 'get'; my $self = bless { %$args }, $class; $self->compile($pattern); @@ -46,8 +45,8 @@ sub match { my ($self, $req) = @_; croak "request required" unless blessed $req; - if( $self->{method} ){ - croak "request method is not match" if $req->method ne uc $self->{method}; + if( $self->{method} && $req->method ){ + return if $req->method ne uc $self->{method}; } my $uri = ref($req->uri) ? $req->uri->path : $req->uri; diff --git a/t/06_plack_get_and_post.t b/t/06_plack_get_and_post.t index 4b94108..f940e52 100644 --- a/t/06_plack_get_and_post.t +++ b/t/06_plack_get_and_post.t @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests => 3; +use Test::More tests => 2; use YAML; use HTTPx::Dispatcher; use HTTP::Request; @@ -9,38 +9,30 @@ use Test::Requires 'Plack::Request'; { package MyDispatcher; use HTTPx::Dispatcher; - get '', { controller => 'Root', action => 'index' }; - post '/comment/{id}', { controller => 'Comment', action => 'post' }; + connect '/comment', + get => { controller => 'Comment', action => 'show' }, + post => { controller => 'Comment', action => 'post' }; } do { my $req = - Plack::Request->new( { PATH_INFO => '/', REQUEST_METHOD => 'GET', } ); + Plack::Request->new( { PATH_INFO => '/comment', REQUEST_METHOD => 'GET', } ); my $x = MyDispatcher->match($req); is_deeply $x, { - 'controller' => 'Root', + 'controller' => 'Comment', 'args' => {}, - 'action' => 'index' + 'action' => 'show' }; }; do { - my $req = Plack::Request->new({PATH_INFO => '/comment/1', REQUEST_METHOD => 'POST' }); + my $req = Plack::Request->new({PATH_INFO => '/comment', REQUEST_METHOD => 'POST' }); my $x = MyDispatcher->match($req); is_deeply $x, { 'controller' => 'Comment', - 'args' => { id => 1 }, + 'args' => {}, 'action' => 'post' }; }; - -# Error case - -do { - my $req = - Plack::Request->new( { PATH_INFO => '/', REQUEST_METHOD => 'POST', } ); - my $x = MyDispatcher->match($req); - ok( !$x ); -};