package Catalyst::Action::Rest;

our $VERSION='0.01';

use base 'Catalyst::Action';


=head1 NAME

Catalyst::Action::Rest - Assist with building REST actions.

=head1 SYNOPSIS

    sub add_to_collection :	ActionClass('Rest') 
							Methods(qw/POST/)
							RestBody('XML::Simple')
	{
		my ($self, $c, @args) = @_;
		
		my $name = $c->stash->{rest_body}->{name};
	}
	
    sub list_collection :	ActionClass('Rest') 
							Methods(qw/GET/)
	{
		my ($self, $c, @args) = @_;
	}	

=head1 DESCRIPTION

This action assists you in creating REST inspired web services.  It
allows you to match by the HTTP method type so that you can dispatch
to actions based on GET, POST, PUT, DELETE, etc.  It also will parse
the http body for PUT and POST methods and automatically instantiate
a class suitable for accessing it's content.

By default this class is XML::Simple although XML::Atom is also quite
suitable.

=head1 EXAMPLE

TDB

=head1 METHODS

This module uses the following Methods.

=head 2 match

Override the match to return false if the http method doesn't match
one of the allowed.

=cut

sub match
{
	my $self	= shift @_;
    my ($c)		= @_;
	
	## If we don't have a Methods attribute there is nothing to do.
    return 1 unless exists $self->attributes->{Methods};
	
	## Get the array of methods.
    my @methods = @{$self->attributes->{Methods}};
	
	foreach my $method (@methods)
	{
		$c->log->debug("Testing method $method");
		return 1 if $method eq $c->request->method;
	}
	
	## If we get this far there is no match so just don't allow the
	## action to proceed.
	
    return 0;
	
	## Or should I use next to check the next match?
	
	##return $self->NEXT::match( @_ );
}



=head 2 execute

Override the execute method to parse the submitted body (if any) into
a suitable object, such as XML::Simple or XML::ATOM.



sub execute
{
    my $self = shift;
    my ( $controller, $c, $test ) = @_;
	
	if( $c->request->method eq 'POST' or
		$c->request->method eq 'PUT ) )
	{
		$c->stash->{rest_body} = 'world';
	}
	
    $self->NEXT::execute( @_ );
}

=cut

=head1 AUTHOR

John Napiorkowski <john.napiorkowski@beijingsolution.com>

=head1 LICENSE

This library is free software. You can redistribute it and/or modify it
under the same terms as Perl itself.

=cut