[BACK]Return to API.pm CVS log [TXT][DIR] Up to [local] / todotxt / Text-Todo-REST-API / lib / Text / Todo / REST

Diff for /todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm between version 1.12 and 1.15

version 1.12, 2010/01/26 03:51:49 version 1.15, 2010/01/26 05:47:30
Line 1 
Line 1 
 package Text::Todo::REST::API;  package Text::Todo::REST::API;
   
 # $AFresh1: API.pm,v 1.11 2010/01/24 04:38:21 andrew Exp $  # $AFresh1: API.pm,v 1.14 2010/01/26 05:05:00 andrew Exp $
   
 use warnings;  use warnings;
 use strict;  use strict;
Line 9 
Line 9 
 use Text::Todo;  use Text::Todo;
 use Text::Todo::REST::API::Response;  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 Class::Std::Utils;
 use Digest::MD5 qw/ md5_hex /;  
   
 use version; our $VERSION = qv('0.0.1');  use version; our $VERSION = qv('0.0.1');
   
Line 69 
Line 73 
             list   => '',              list   => '',
             action => 'files',              action => 'files',
             args   => [],              args   => [],
   
               suffix     => $self->_suffix,
               file_regex => $self->_file_regex,
               format     => $self->_format,
         );          );
   
         if (@args) {          if (@args) {
Line 104 
Line 112 
   
         push @{ $options{args} }, @args;          push @{ $options{args} }, @args;
   
         $options{list}          my $list = $self->_todo->file( $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} ) {          if ( $options{format} ) {
             $format_of{ ident $self } = $options{format};              $format_of{ ident $self } = $options{format};
             delete $options{format};              delete $options{format};
Line 131 
Line 145 
         my $todo = $self->_todo;          my $todo = $self->_todo;
         $todo->load( $options{list} );          $todo->load( $options{list} );
   
         foreach my $class ($self) {          foreach my $class ( $self->actions ) {
             if ( $class->can($method) ) {              if ( $class->can($method) ) {
                 return Text::Todo::REST::API::Response->new(                  return Text::Todo::REST::API::Response->new(
                     {   type   => $options{action},                      {   type   => $options{action},
Line 167 
Line 181 
         if ( $options{list} ) {          if ( $options{list} ) {
             $options{action} ||= 'list';              $options{action} ||= 'list';
   
             my $suffix = $self->_suffix;              my $list = $options{list};
   
             if ( ( lc $options{list} ) eq 'files' ) {              if ( ( lc $options{list} ) eq 'files' ) {
                 $options{action} = lc $options{list};                  $options{action} = lc $list;
                 $options{list}   = q{};                  $list = q{};
             }              }
             elsif ( $self->_todo->file( $options{list} ) ) {  
                 $options{list} = $self->_todo->file( $options{list} );              $options{list} = $list;
             }  
         }          }
   
         if ( @{ $options{args} } && ( lc $options{args}[0] ) eq 'entry' ) {          if ( @{ $options{args} } && ( lc $options{args}[0] ) eq 'entry' ) {
Line 188 
Line 201 
     sub GET {      sub GET {
         my ( $self, @args ) = @_;          my ( $self, @args ) = @_;
         return $self->_handle_action( 'GET', @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 {      sub POST {

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.15

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>