=================================================================== RCS file: /cvs/todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm,v retrieving revision 1.9 retrieving revision 1.11 diff -u -r1.9 -r1.11 --- todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/23 07:15:40 1.9 +++ todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/24 04:38:21 1.11 @@ -1,12 +1,13 @@ package Text::Todo::REST::API; -# $AFresh1: API.pm,v 1.8 2010/01/19 03:18:34 andrew Exp $ +# $AFresh1: API.pm,v 1.10 2010/01/24 04:17:39 andrew Exp $ use warnings; use strict; use Carp; use Text::Todo; +use Text::Todo::REST::API::Response; use Class::Std::Utils; use Digest::MD5 qw/ md5_hex /; @@ -94,16 +95,17 @@ } } - if ($options{action} eq 'entry' && @{ $options{args} }) { + if ( $options{action} eq 'entry' && @{ $options{args} } ) { $options{entry} = shift @{ $options{args} }; - if (@{ $options{args} }) { + if ( @{ $options{args} } ) { $options{action} = lc shift @{ $options{args} }; } } push @{ $options{args} }, @args; - $options{list} = defined $options{list} ? $options{list} : 'todo_file'; + $options{list} + = defined $options{list} ? $options{list} : 'todo_file'; if ( $options{format} ) { $format_of{ ident $self } = $options{format}; @@ -124,14 +126,19 @@ sub _handle_action { my ( $self, @args ) = @_; - my ($method, %options) = $self->_parse_options(@args); + my ( $method, %options ) = $self->_parse_options(@args); my $todo = $self->_todo; $todo->load( $options{list} ); - foreach my $class ( $self ) { - if ($class->can($method) ) { - return $class->$method( $todo, \%options ); + foreach my $class ($self) { + if ( $class->can($method) ) { + return Text::Todo::REST::API::Response->new( + { type => $options{action}, + format => $self->_format, + data => $class->$method( $todo, \%options ), + } + ); } } @@ -162,12 +169,12 @@ my $suffix = $self->_suffix; - if (( lc $options{list} ) eq 'files') { + if ( ( lc $options{list} ) eq 'files' ) { $options{action} = lc $options{list}; - $options{list} = q{}; + $options{list} = q{}; } - elsif ($self->_todo->file( $options{list} )) { - $options{list} = $self->_todo->file( $options{list} ); + elsif ( $self->_todo->file( $options{list} ) ) { + $options{list} = $self->_todo->file( $options{list} ); } } @@ -197,21 +204,21 @@ return @entries; } elsif ( ref $key eq 'HASH' ) { - if (exists $key->{entry}) { - $key = $key->{entry}; + if ( exists $key->{entry} ) { + $key = $key->{entry}; } else { return $self->fail('get_entry requires key [entry]'); } } - my @list = $self->get_list($todo); + my $list = $self->get_list($todo); my $entry; if ( $key =~ /^[[:xdigit:]]{32}$/xms ) { my $search = lc $key; - ENTRY: foreach my $e (@list) { + ENTRY: foreach my $e ( @{$list} ) { if ( $search eq $e->{md5} ) { $entry = $e; last ENTRY; @@ -219,7 +226,7 @@ } } elsif ( $key =~ /^\d+$/xms ) { - $entry = $list[ $key - 1 ]; + $entry = $list->[ $key - 1 ]; } if ( !$entry ) { @@ -230,19 +237,20 @@ } sub get_list { - my ($self, $todo) = @_; + my ( $self, $todo ) = @_; my $line = 1; - return map ( { + my @list = map ( { line => $line++, md5 => md5_hex( $_->text ), text => $_->text, }, $todo->list ); + return \@list; } sub get_files { - my ($self, $todo) = @_; + my ( $self, $todo ) = @_; my $dir = $todo->file('todo_dir'); if ( !$dir ) { @@ -255,12 +263,12 @@ my @files = grep {m/$file_regex/xms} readdir $dh; closedir $dh; - return @files; + return \@files; } sub get_tags { my ( $self, $todo, $tag ) = @_; - return $todo->listtag($tag); + return [ $todo->listtag($tag) ]; } sub POST {