[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.5 and 1.12

version 1.5, 2010/01/18 03:10:42 version 1.12, 2010/01/26 03:51:49
Line 1 
Line 1 
 package Text::Todo::REST::API;  package Text::Todo::REST::API;
   
 # $AFresh1: API.pm,v 1.4 2010/01/18 00:30:42 andrew Exp $  # $AFresh1: API.pm,v 1.11 2010/01/24 04:38:21 andrew Exp $
   
 use warnings;  use warnings;
 use strict;  use strict;
 use Carp;  use Carp;
   
 use Data::Dumper;  
 use Text::Todo;  use Text::Todo;
   use Text::Todo::REST::API::Response;
   
 use Class::Std::Utils;  use Class::Std::Utils;
 use Digest::MD5 qw/ md5_hex /;  use Digest::MD5 qw/ md5_hex /;
 use File::Spec;  
   
 use version; our $VERSION = qv('0.0.1');  use version; our $VERSION = qv('0.0.1');
   
 &RegisterActionHandler(  
     'GET',  
     [ list  => 'get_list' ],  
     [ entry => 'get_entry' ],  
     [ tags  => 'get_tags' ],  
     [ files => 'get_files' ],  
 );  
   
 {  {
     my @attr_refs = \(      my @attr_refs = \(
         my %todo_of,          my %todo_of,
         my %basedir_of,  
         my %subdir_of,  
         my %suffix_of,          my %suffix_of,
         my %file_regex_of,          my %file_regex_of,
         my %user_of,  
         my %list_of,          my %format_of,
         my %action_of,  
         my %args_of,  
         my %action_handlers,          my %action_handlers,
         my %format_handlers,  
     );      );
   
     sub new {      sub new {
Line 44 
Line 32 
         my $self = bless anon_scalar(), $class;          my $self = bless anon_scalar(), $class;
         my $ident = ident($self);          my $ident = ident($self);
   
         my $format = $options->{default_format};          $format_of{$ident} = $options->{default_format};
         if ( $options->{format} ) {          if ( $options->{format} ) {
             $format = $options->{format};              $format_of{$ident} = $options->{format};
         }          }
         elsif ($options->{path_info}  
             && $options->{path_info} =~ s/\.(\w+)$//xms )  
         {  
             $format = $1;  
         }  
   
         if ( ref $self eq __PACKAGE__ && $format ) {          $suffix_of{$ident} = $options->{suffix} || '.txt';
             if ( $format_handlers{$format} ) {  
                 bless $self, $format_handlers{$format};  
             }  
             else {  
                 croak("Unable to find handler for [$format]\n");  
             }  
         }  
   
         $basedir_of{$ident} = $options->{basedir};  
         $subdir_of{$ident}  = $options->{subdir};  
         $suffix_of{$ident}  = $options->{suffix} || '.txt';  
   
         $file_regex_of{$ident} = $options->{file_regex} || qr{          $file_regex_of{$ident} = $options->{file_regex} || qr{
             .*              .*
             todo              todo
Line 75 
Line 47 
             $              $
         }ixms;          }ixms;
   
         if ( !$basedir_of{$ident} ) {          eval {
             return $self->fail('Required option [basedir]');              $todo_of{$ident} = Text::Todo->new(
                   {   todo_dir  => $options->{todo_dir},
                       todo_file => $options->{todo_file},
                   }
               );
           };
           if ($@) {
               $self->fail( 'Unable to create Text::Todo object' . $@ );
         }          }
   
         $options->{path_info} ||= q{};          return $self;
         $options->{path_info} =~ s{^/}{}xms;      }
         (   $user_of{$ident},   $list_of{$ident},  
             $action_of{$ident}, @{ $args_of{$ident} },  
         ) = split '/', $options->{path_info};  
   
         if ( $list_of{$ident} ) {      sub _parse_options {
             $action_of{$ident} ||= 'list';          my ( $self, $method, @args ) = @_;
         }  
         elsif ( $user_of{$ident} ) {  
             $action_of{$ident} = 'files';  
         }  
   
         my @todo_dir = $basedir_of{$ident};          my %options = (
               method => lc $method,
               list   => '',
               action => 'files',
               args   => [],
           );
   
         my $todo_dir;          if (@args) {
         if ( $user_of{$ident} ) {              if ( !ref $args[0] ) {
             push @todo_dir, $user_of{$ident};                  $options{path} = shift @args;
             if ( $subdir_of{$ident} ) {  
                 push @todo_dir, $subdir_of{$ident};  
             }              }
   
             $todo_dir = File::Spec->catdir(@todo_dir);              if ( ref $args[0] eq 'HASH' ) {
                   my $opts = shift @args;
                   foreach my $o ( keys %{$opts} ) {
                       $options{$o} = $opts->{$o};
                   }
               }
         }          }
   
         my $todo_file;          if ( exists $options{path} ) {
         if ( $list_of{$ident} ) {              my %opts = $self->_split_path( $options{path} );
             $todo_file = $list_of{$ident} . $suffix_of{$ident};              delete $options{path};
   
               foreach my $o ( keys %opts ) {
                   if ( defined $opts{$o} ) {
                       $options{$o} = $opts{$o};
                   }
               }
         }          }
   
         $todo_of{$ident} = Text::Todo->new(          if ( $options{action} eq 'entry' && @{ $options{args} } ) {
             {   todo_dir  => $todo_dir,              $options{entry} = shift @{ $options{args} };
                 todo_file => $todo_file,              if ( @{ $options{args} } ) {
                   $options{action} .= q{_} . lc shift @{ $options{args} };
             }              }
         ) or $self->fail('Unable to create Text::Todo object');          }
   
         $todo_of{$ident}->load('todo_file')          push @{ $options{args} }, @args;
             or $self->fail('Unable to create Text::Todo object');  
   
         return $self;          $options{list}
     }              = defined $options{list} ? $options{list} : 'todo_file';
   
     sub RegisterFormatHandler {          if ( $options{format} ) {
         my ( $handler, @types ) = @_;              $format_of{ ident $self } = $options{format};
               delete $options{format};
           }
   
         foreach my $type (@types) {          my @method;
             $format_handlers{$type} = $handler;          foreach my $o qw( method action ) {
               if ( $options{$o} ) {
                   push @method, $options{$o};
               }
         }          }
           $method = join q{_}, @method;
   
         return 1;          return $method, %options;
     }      }
   
     sub RegisterActionHandler {      sub _handle_action {
         my ( $handler, @types ) = @_;          my ( $self, @args ) = @_;
   
         foreach my $type (@types) {          my ( $method, %options ) = $self->_parse_options(@args);
             $action_handlers{$handler}{ $type->[0] } = $type->[1];  
           my $todo = $self->_todo;
           $todo->load( $options{list} );
   
           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 ),
                       }
                   );
               }
         }          }
   
         return 1;          return $self->fail( 'Unable to handle [' . $method . ']' );
     }      }
   
     sub content_type {return}      sub _split_path {
           my ( $self, $path ) = @_;
   
     sub Dump {          my %options = (
         my ($self) = @_;              list   => undef,
         return $self->fail( 'Unable to Dump [' . $self->_action . ']' );              action => undef,
     }              args   => [],
           );
   
     sub Load {          $path = defined $path ? $path : q{};
         my ($self) = @_;          $path =~ s{^/}{}xms;
         return $self->fail( 'Unable to Load [' . $self->_action . ']' );  
     }  
   
     sub _handle_action {          if ( $path =~ s/\.(\w+)$//xms ) {
         my ( $self, $method, $params ) = @_;              $options{format} = $1;
           }
   
         if ( exists $action_handlers{$method}{ $self->_action } ) {          ( $options{list}, $options{action}, @{ $options{args} } ) = split '/',
             my $a = $action_handlers{$method}{ $self->_action };              $path;
             return $self->$a( $self->_args, $params );  
           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} );
               }
         }          }
   
         return $self->fail(          if ( @{ $options{args} } && ( lc $options{args}[0] ) eq 'entry' ) {
             'Unable to handle ' . $method . ' [' . $self->_action . ']' );              $options{action} = lc shift @{ $options{args} };
           }
   
           return %options;
     }      }
   
     sub GET {      sub GET {
Line 170 
Line 190 
         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 {      sub get_entry {
         my ( $self, $key ) = @_;          my ( $self, $todo, $key ) = @_;
   
         if ( !$key ) {          if ( !$key ) {
             return $self->fail("get_entry requires arguments");              return $self->fail("get_entry requires arguments");
         }          }
         elsif ( ref $key eq 'ARRAY' ) {          elsif ( ref $key eq 'ARRAY' ) {
             my @entries;              return self->get_entry( $_->[0] );
             foreach ( @{$key} ) {          }
                 push @entries, $self->get_entry($_);          elsif ( ref $key eq 'HASH' ) {
               if ( exists $key->{entry} ) {
                   $key = $key->{entry};
             }              }
             return @entries;              else {
                   return $self->fail('get_entry requires key [entry]');
               }
         }          }
   
         my @list = $self->get_list;          my $list = $self->get_list($todo);
   
         my $entry;  
         if ( $key =~ /^[[:xdigit:]]{32}$/xms ) {          if ( $key =~ /^[[:xdigit:]]{32}$/xms ) {
             my $search = lc $key;              my $search = lc $key;
   
         ENTRY: foreach my $e (@list) {              foreach my $e ( @{$list} ) {
                 if ( $search eq $e->{md5} ) {                  if ( $search eq $e->{md5} ) {
                     $entry = $e;                      return $e;
                     last ENTRY;  
                 }                  }
             }              }
         }          }
         elsif ( $key =~ /^\d+$/xms ) {          elsif ( $key =~ /^\d+$/xms ) {
             $entry = $list[ $key - 1 ];              return $list->[ $key - 1 ];
         }          }
   
         if ( !$entry ) {          return $self->fail("Unable to find entry!");
             return $self->fail("Unable to find entry!");  
         }  
   
         return $entry;  
     }      }
   
     sub get_list {      sub get_list {
         my ($self) = @_;          my ( $self, $todo ) = @_;
   
         my $line = 1;          my $line = 1;
         return map ( {          my @list = map ( {
                 line => $line++,                  line => $line++,
                 md5  => md5_hex( $_->text ),                  md5  => md5_hex( $_->text ),
                 text => $_->text,                  text => $_->text,
             },              },
             $self->_todo->list );              $todo->list );
           return \@list;
     }      }
   
     sub get_files {      sub get_files {
         my ($self) = @_;          my ( $self, $todo ) = @_;
         my $dir = $self->_todo->file('todo_dir');          my $dir = $todo->file('todo_dir');
   
         if ( !$dir ) {          if ( !$dir ) {
             return $self->fail('Unable to find todo_dir');              return $self->fail('Unable to find todo_dir');
Line 234 
Line 260 
         my @files = grep {m/$file_regex/xms} readdir $dh;          my @files = grep {m/$file_regex/xms} readdir $dh;
         closedir $dh;          closedir $dh;
   
         return @files;          return \@files;
     }      }
   
     sub get_tags {      sub get_tags {
         my ( $self, $tag ) = @_;          my ( $self, $todo, $tag ) = @_;
         my $ident = ident($self);          return [ $todo->listtag($tag) ];
   
         return $todo_of{$ident}->listtag($tag);  
     }      }
   
     sub POST {      sub POST {
Line 265 
Line 289 
     }      }
   
     sub _todo       { my ($self) = @_; return $todo_of{ ident $self }; }      sub _todo       { my ($self) = @_; return $todo_of{ ident $self }; }
     sub _basedir    { my ($self) = @_; return $basedir_of{ ident $self}; }  
     sub _subdir     { my ($self) = @_; return $subdir_of{ ident $self}; }  
     sub _suffix     { my ($self) = @_; return $suffix_of{ ident $self}; }      sub _suffix     { my ($self) = @_; return $suffix_of{ ident $self}; }
     sub _file_regex { my ($self) = @_; return $file_regex_of{ ident $self}; }      sub _file_regex { my ($self) = @_; return $file_regex_of{ ident $self}; }
     sub _user       { my ($self) = @_; return $user_of{ ident $self}; }      sub _format     { my ($self) = @_; return $format_of{ ident $self}; }
     sub _list       { my ($self) = @_; return $list_of{ ident $self}; }  
     sub _action     { my ($self) = @_; return $action_of{ ident $self}; }  
     sub _args       { my ($self) = @_; return $args_of{ ident $self}; }  
   
     sub DESTROY {      sub DESTROY {
         my ($self) = @_;          my ($self) = @_;

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

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