=================================================================== RCS file: /cvs/todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm,v retrieving revision 1.12 retrieving revision 1.19 diff -u -r1.12 -r1.19 --- todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/26 03:51:49 1.12 +++ todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/02/16 06:12:26 1.19 @@ -1,6 +1,6 @@ package Text::Todo::REST::API; -# $AFresh1: API.pm,v 1.11 2010/01/24 04:38:21 andrew Exp $ +# $AFresh1: API.pm,v 1.18 2010/02/13 21:48:28 andrew Exp $ use warnings; use strict; @@ -9,8 +9,12 @@ use Text::Todo; use Text::Todo::REST::API::Response; +use Module::Pluggable + instantiate => 'new', + search_path => 'Text::Todo::REST::API::Actions', + sub_name => 'actions'; + use Class::Std::Utils; -use Digest::MD5 qw/ md5_hex /; use version; our $VERSION = qv('0.0.1'); @@ -62,22 +66,26 @@ } sub _parse_options { - my ( $self, $method, @args ) = @_; + my ( $self, $method, @options) = @_; my %options = ( method => lc $method, list => '', action => 'files', - args => [], + args => {}, + + suffix => $self->_suffix, + file_regex => $self->_file_regex, + format => $self->_format, ); - if (@args) { - if ( !ref $args[0] ) { - $options{path} = shift @args; + if (@options) { + if ( !ref $options[0] ) { + $options{path} = shift @options; } - if ( ref $args[0] eq 'HASH' ) { - my $opts = shift @args; + if ( ref $options[0] eq 'HASH' ) { + my $opts = shift @options; foreach my $o ( keys %{$opts} ) { $options{$o} = $opts->{$o}; } @@ -95,18 +103,21 @@ } } - if ( $options{action} eq 'entry' && @{ $options{args} } ) { - $options{entry} = shift @{ $options{args} }; - if ( @{ $options{args} } ) { - $options{action} .= q{_} . lc shift @{ $options{args} }; - } + if ( $options{action} eq 'entry' && @{ $options{extra} } ) { + $options{action} .= q{_} . lc shift @{ $options{extra} }; } + + push @{ $options{extra} }, @options; - push @{ $options{args} }, @args; + my $list = $self->_todo->file( $options{list} ); - $options{list} - = defined $options{list} ? $options{list} : 'todo_file'; + my $suffix = $self->_suffix || q{}; + if ( $list !~ / \Q$suffix\E $/ixms ) { + $list .= $suffix; + } + $options{list} = $list; + if ( $options{format} ) { $format_of{ ident $self } = $options{format}; delete $options{format}; @@ -131,7 +142,7 @@ my $todo = $self->_todo; $todo->load( $options{list} ); - foreach my $class ($self) { + foreach my $class ( $self->actions ) { if ( $class->can($method) ) { return Text::Todo::REST::API::Response->new( { type => $options{action}, @@ -149,9 +160,10 @@ my ( $self, $path ) = @_; my %options = ( - list => undef, action => undef, - args => [], + list => undef, + entry => undef, + extra => [], ); $path = defined $path ? $path : q{}; @@ -161,25 +173,24 @@ $options{format} = $1; } - ( $options{list}, $options{action}, @{ $options{args} } ) = split '/', - $path; + ( $options{list}, $options{action}, + @{ $options{extra} } ) = split '/', $path; - if ( $options{list} ) { - $options{action} ||= 'list'; - - my $suffix = $self->_suffix; - - if ( ( lc $options{list} ) eq 'files' ) { - $options{action} = lc $options{list}; - $options{list} = q{}; + if (!defined $options{action}) { + if ( $options{list} ) { + $options{action} = 'list'; } - elsif ( $self->_todo->file( $options{list} ) ) { - $options{list} = $self->_todo->file( $options{list} ); + else { + $options{action} = 'files'; } } - if ( @{ $options{args} } && ( lc $options{args}[0] ) eq 'entry' ) { - $options{action} = lc shift @{ $options{args} }; + $options{action} = lc( $options{action} ); + + if ($options{action} eq 'entry' + || $options{action} eq 'tags' + ) { + $options{ $options{action} } = shift @{ $options{extra} }; } return %options; @@ -188,84 +199,6 @@ sub GET { my ( $self, @args ) = @_; return $self->_handle_action( 'GET', @args ); - } - - sub get_entry_done { - my ( $self, $todo, $key ) = @_; - my $entry = $self->get_entry( $todo, $key ); - - return $todo->list->[ $entry->{line} - 1 ]->done; - } - - sub get_entry { - my ( $self, $todo, $key ) = @_; - - if ( !$key ) { - return $self->fail("get_entry requires arguments"); - } - elsif ( ref $key eq 'ARRAY' ) { - return self->get_entry( $_->[0] ); - } - elsif ( ref $key eq 'HASH' ) { - if ( exists $key->{entry} ) { - $key = $key->{entry}; - } - else { - return $self->fail('get_entry requires key [entry]'); - } - } - - my $list = $self->get_list($todo); - - if ( $key =~ /^[[:xdigit:]]{32}$/xms ) { - my $search = lc $key; - - foreach my $e ( @{$list} ) { - if ( $search eq $e->{md5} ) { - return $e; - } - } - } - elsif ( $key =~ /^\d+$/xms ) { - return $list->[ $key - 1 ]; - } - - return $self->fail("Unable to find entry!"); - } - - sub get_list { - my ( $self, $todo ) = @_; - - my $line = 1; - my @list = map ( { - line => $line++, - md5 => md5_hex( $_->text ), - text => $_->text, - }, - $todo->list ); - return \@list; - } - - sub get_files { - my ( $self, $todo ) = @_; - my $dir = $todo->file('todo_dir'); - - if ( !$dir ) { - return $self->fail('Unable to find todo_dir'); - } - - my $file_regex = $self->_file_regex; - - opendir my $dh, $dir or croak "Couldn't opendir: $!"; - my @files = grep {m/$file_regex/xms} readdir $dh; - closedir $dh; - - return \@files; - } - - sub get_tags { - my ( $self, $todo, $tag ) = @_; - return [ $todo->listtag($tag) ]; } sub POST {