=================================================================== RCS file: /cvs/todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm,v retrieving revision 1.10 retrieving revision 1.14 diff -u -r1.10 -r1.14 --- todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/24 04:17:39 1.10 +++ todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/26 05:05:00 1.14 @@ -1,6 +1,6 @@ package Text::Todo::REST::API; -# $AFresh1: API.pm,v 1.9 2010/01/23 07:15:40 andrew Exp $ +# $AFresh1: API.pm,v 1.13 2010/01/26 04:30:13 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'); @@ -69,6 +73,10 @@ list => '', action => 'files', args => [], + + suffix => $self->_suffix, + file_regex => $self->_file_regex, + format => $self->_format, ); if (@args) { @@ -98,7 +106,7 @@ if ( $options{action} eq 'entry' && @{ $options{args} } ) { $options{entry} = shift @{ $options{args} }; if ( @{ $options{args} } ) { - $options{action} = lc shift @{ $options{args} }; + $options{action} .= q{_} . lc shift @{ $options{args} }; } } @@ -106,6 +114,10 @@ $options{list} = defined $options{list} ? $options{list} : 'todo_file'; + + if ($options{list} !~ /\Q $options{suffix} \E$/xms) { + $options{list} .= $options{suffix}; + } if ( $options{format} ) { $format_of{ ident $self } = $options{format}; @@ -131,15 +143,14 @@ my $todo = $self->_todo; $todo->load( $options{list} ); - foreach my $class ($self) { + foreach my $class ( $self->actions ) { if ( $class->can($method) ) { - my @data = $class->$method( $todo, \%options ); - - return Text::Todo::REST::API::Response->new({ - type => $options{action}, - format => $self->_format, - data => \@data, - }); + return Text::Todo::REST::API::Response->new( + { type => $options{action}, + format => $self->_format, + data => $class->$method( $todo, \%options ), + } + ); } } @@ -168,8 +179,6 @@ if ( $options{list} ) { $options{action} ||= 'list'; - my $suffix = $self->_suffix; - if ( ( lc $options{list} ) eq 'files' ) { $options{action} = lc $options{list}; $options{list} = q{}; @@ -189,86 +198,6 @@ sub GET { my ( $self, @args ) = @_; return $self->_handle_action( 'GET', @args ); - } - - sub get_entry { - my ( $self, $todo, $key ) = @_; - - if ( !$key ) { - return $self->fail("get_entry requires arguments"); - } - elsif ( ref $key eq 'ARRAY' ) { - my @entries; - foreach ( @{$key} ) { - push @entries, $self->get_entry($_); - } - return @entries; - } - 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); - - my $entry; - if ( $key =~ /^[[:xdigit:]]{32}$/xms ) { - my $search = lc $key; - - ENTRY: foreach my $e (@list) { - if ( $search eq $e->{md5} ) { - $entry = $e; - last ENTRY; - } - } - } - elsif ( $key =~ /^\d+$/xms ) { - $entry = $list[ $key - 1 ]; - } - - if ( !$entry ) { - return $self->fail("Unable to find entry!"); - } - - return $entry; - } - - sub get_list { - my ( $self, $todo ) = @_; - - my $line = 1; - return map ( { - line => $line++, - md5 => md5_hex( $_->text ), - text => $_->text, - }, - $todo->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 {