[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.10 and 1.20

version 1.10, 2010/01/24 04:17:39 version 1.20, 2010/04/30 00:04:29
Line 1 
Line 1 
 package Text::Todo::REST::API;  package Text::Todo::REST::API;
   
 # $AFresh1: API.pm,v 1.9 2010/01/23 07:15:40 andrew Exp $  # $AFresh1: API.pm,v 1.19 2010/02/16 06:12:26 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 62 
Line 66 
     }      }
   
     sub _parse_options {      sub _parse_options {
         my ( $self, $method, @args ) = @_;          my ( $self, $method, @options) = @_;
   
         my %options = (          my %options = (
             method => lc $method,              method => lc $method,
             list   => '',              list   => '',
             action => 'files',              action => 'files',
             args   => [],              args   => {},
   
               suffix     => $self->_suffix,
               file_regex => $self->_file_regex,
               format     => $self->_format,
         );          );
   
         if (@args) {          if (@options) {
             if ( !ref $args[0] ) {              if ( !ref $options[0] ) {
                 $options{path} = shift @args;                  $options{path} = shift @options;
             }              }
   
             if ( ref $args[0] eq 'HASH' ) {              if ( ref $options[0] eq 'HASH' ) {
                 my $opts = shift @args;                  my $opts = shift @options;
                 foreach my $o ( keys %{$opts} ) {                  foreach my $o ( keys %{$opts} ) {
                     $options{$o} = $opts->{$o};                      $options{$o} = $opts->{$o};
                 }                  }
Line 95 
Line 103 
             }              }
         }          }
   
         if ( $options{action} eq 'entry' && @{ $options{args} } ) {          if ( $options{action} eq 'entry' && @{ $options{extra} } ) {
             $options{entry} = shift @{ $options{args} };              $options{action} .= q{_} . lc shift @{ $options{extra} };
             if ( @{ $options{args} } ) {  
                 $options{action} = lc shift @{ $options{args} };  
             }  
         }          }
   
           push @{ $options{extra} }, @options;
   
         push @{ $options{args} }, @args;          my $list = $self->_todo->file( $options{list} );
   
         $options{list}          my $suffix = $self->_suffix || q{};
             = defined $options{list} ? $options{list} : 'todo_file';          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 142 
         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) ) {
                 my @data = $class->$method( $todo, \%options );                  return Text::Todo::REST::API::Response->new(
                       {   type   => $options{action},
                 return Text::Todo::REST::API::Response->new({                          format => $self->_format,
                     type   => $options{action},                          data   => $class->$method( $todo, \%options ),
                     format => $self->_format,                      }
                     data   => \@data,                  );
                 });  
             }              }
         }          }
   
Line 150 
Line 160 
         my ( $self, $path ) = @_;          my ( $self, $path ) = @_;
   
         my %options = (          my %options = (
             list   => undef,  
             action => undef,              action => undef,
             args   => [],              list   => undef,
               entry  => undef,
               extra  => [],
         );          );
   
         $path = defined $path ? $path : q{};          $path = defined $path ? $path : q{};
Line 162 
Line 173 
             $options{format} = $1;              $options{format} = $1;
         }          }
   
         ( $options{list}, $options{action}, @{ $options{args} } ) = split '/',          ( $options{list}, $options{action},
             $path;            @{ $options{extra} } ) = split '/', $path;
   
         if ( $options{list} ) {          if (!defined $options{action}) {
             $options{action} ||= 'list';              if ( $options{list} ) {
                   $options{action} = 'list';
             my $suffix = $self->_suffix;  
   
             if ( ( lc $options{list} ) eq 'files' ) {  
                 $options{action} = lc $options{list};  
                 $options{list}   = q{};  
             }              }
             elsif ( $self->_todo->file( $options{list} ) ) {  
                 $options{list} = $self->_todo->file( $options{list} );  
             }  
         }  
   
         if ( @{ $options{args} } && ( lc $options{args}[0] ) eq 'entry' ) {  
             $options{action} = lc shift @{ $options{args} };  
         }  
   
         return %options;  
     }  
   
     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 {              else {
                 return $self->fail('get_entry requires key [entry]');                  $options{action} = 'files';
             }              }
         }          }
   
         my @list = $self->get_list($todo);          $options{action} = lc( $options{action} );
   
         my $entry;          if ($options{action} eq 'entry'
         if ( $key =~ /^[[:xdigit:]]{32}$/xms ) {           || $options{action} eq 'tags'
             my $search = lc $key;          ) {
               $options{ $options{action} } = shift @{ $options{extra} };
         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 %options;
             return $self->fail("Unable to find entry!");  
         }  
   
         return $entry;  
     }      }
   
     sub get_list {      sub GET    { return shift->_handle_action( 'GET',    @_ ) }
         my ( $self, $todo ) = @_;      sub POST   { return shift->_handle_action( 'POST',   @_ ) }
       sub PUT    { return shift->_handle_action( 'PUT',    @_ ) }
         my $line = 1;      sub DELETE { return shift->_handle_action( 'DELETE', @_ ) }
         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 {  
         my ( $self, @args ) = @_;  
         return $self->_handle_action( 'POST', @args );  
     }  
   
     sub PUT {  
         my ( $self, @args ) = @_;  
         return $self->_handle_action( 'PUT', @args );  
     }  
   
     sub DELETE {  
         my ( $self, @args ) = @_;  
         return $self->_handle_action( 'DELETE', @args );  
     }  
   
     sub fail {      sub fail {
         my ( $self, @message ) = @_;          my ( $self, @message ) = @_;

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.20

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