CGI-Application-Plugin-DeclareREST
view release on metacpan or search on metacpan
lib/CGI/Application/Plugin/DeclareREST.pm view on Meta::CPAN
use strict;
use warnings;
package CGI::Application::Plugin::DeclareREST;
# ABSTRACT: Declare RESTful API for CGI::Application
$CGI::Application::Plugin::DeclareREST::VERSION = '0.03';
use Exporter;
use REST::Utils qw( request_method );
use Routes::Tiny 0.11;
our @ISA = qw( Exporter );
our @EXPORT = qw(
get post del put patch any
match captures
add_route
);
our %EXPORT_TAGS = (
http_methods => [qw( get post del put patch )],
);
our %routes;
sub add_route {
my $self = shift;
my $class = ref $self;
my $router = $routes{ $class } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
return $router->add_route( @_ );
}
sub get {
my $sub = pop;
my ($path, %args) = @_;
my $caller = caller();
my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
return $router->add_route($path, method => 'get', name => $sub, %args);
}
sub post {
my $sub = pop;
my ($path, %args) = @_;
my $caller = caller();
my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
return $router->add_route($path, method => 'post', name => $sub, %args );
}
sub del {
my $sub = pop;
my ($path, %args) = @_;
my $caller = caller();
my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
return $router->add_route($path, method => 'delete', name => $sub, %args );
}
sub put {
my $sub = pop;
my ($path, %args) = @_;
my $caller = caller();
my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
return $router->add_route($path, method => 'put', name => $sub, %args );
}
sub patch {
my $sub = pop;
my ($path, %args) = @_;
my $caller = caller();
my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
return $router->add_route($path, method => 'patch', name => $sub, %args );
}
sub any {
my $sub = pop;
my ($methods, $path, %args) = @_;
my $caller = caller();
my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
return $router->add_route($path, method => $methods, name => $sub, %args);
}
sub import {
my $caller = caller;
$caller->add_callback('prerun', \&_routes_prerun);
goto &Exporter::import;
}
sub _routes_prerun {
my $self = shift;
my $class = ref $self || $self;
if(defined $self->query->param( $self->mode_param )) {
# OK, we got the query param, to select the right runmode:
# We should passthrough to allow normal behaviour
return;
}
my $method = request_method($self->query);
my $r = $routes{$class};
if($r) {
my $match = $r->match($self->query->path_info, method => $method );
if($match) {
$self->run_modes( $match->name => $match->name );
$self->prerun_mode( $match->name );
$self->{__MATCH} = $match;
return;
}
}
}
sub match {
(shift)->{__MATCH};
}
sub captures {
(shift)->{__MATCH}->captures;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
CGI::Application::Plugin::DeclareREST - Declare RESTful API for CGI::Application
=head1 VERSION
version 0.03
( run in 2.043 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )