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

version 1.6, 2010/01/18 03:51:40 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.5 2010/01/18 03:10:42 andrew Exp $  # $AFresh1: API.pm,v 1.19 2010/02/16 06:12:26 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 Module::Pluggable  use Module::Pluggable
     require     => 1,      instantiate => 'new',
     search_path => __PACKAGE__ . '::Representations',      search_path => 'Text::Todo::REST::API::Actions',
     sub_name    => 'representations';      sub_name    => 'actions';
 use Digest::MD5 qw/ md5_hex /;  
 use File::Spec;  
   
   use Class::Std::Utils;
   
 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 %format_of,
         my %list_of,  
         my %action_of,  
         my %args_of,  
   
         my %action_handlers,          my %action_handlers,
     );      );
Line 48 
Line 33 
     sub new {      sub new {
         my ( $class, $options ) = @_;          my ( $class, $options ) = @_;
   
         my $format = $options->{default_format};  
         if ( $options->{format} ) {  
             $format = $options->{format};  
         }  
         elsif ($options->{path_info}  
             && $options->{path_info} =~ s/\.(\w+)$//xms )  
         {  
             $format = $1;  
         }  
   
         my $self = bless anon_scalar(), $class;          my $self = bless anon_scalar(), $class;
         my $ident = ident($self);          my $ident = ident($self);
   
         if ( ref $self eq __PACKAGE__ && $format ) {          $format_of{$ident} = $options->{default_format};
             my $found_handler = 0;          if ( $options->{format} ) {
         REP: foreach my $rep ( $self->representations ) {              $format_of{$ident} = $options->{format};
                 if ( $rep->_handles($format) ) {  
                     $self          = $rep->new($options);  
                     $found_handler = 1;  
                     last REP;  
                 }  
             }  
             if ( !$found_handler ) {  
                 croak("Unable to find handler for [$format]\n");  
             }  
         }          }
   
         $basedir_of{$ident} = $options->{basedir};          $suffix_of{$ident} = $options->{suffix} || '.txt';
         $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{
             .*              .*
Line 87 
Line 51 
             $              $
         }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, @options) = @_;
         }  
         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;              suffix     => $self->_suffix,
         if ( $user_of{$ident} ) {              file_regex => $self->_file_regex,
             push @todo_dir, $user_of{$ident};              format     => $self->_format,
             if ( $subdir_of{$ident} ) {          );
                 push @todo_dir, $subdir_of{$ident};  
           if (@options) {
               if ( !ref $options[0] ) {
                   $options{path} = shift @options;
             }              }
   
             $todo_dir = File::Spec->catdir(@todo_dir);              if ( ref $options[0] eq 'HASH' ) {
                   my $opts = shift @options;
                   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};
         }  
   
         $todo_of{$ident} = Text::Todo->new(              foreach my $o ( keys %opts ) {
             {   todo_dir  => $todo_dir,                  if ( defined $opts{$o} ) {
                 todo_file => $todo_file,                      $options{$o} = $opts{$o};
                   }
             }              }
         ) or $self->fail('Unable to create Text::Todo object');          }
   
         $todo_of{$ident}->load('todo_file')          if ( $options{action} eq 'entry' && @{ $options{extra} } ) {
             or $self->fail('Unable to create Text::Todo object');              $options{action} .= q{_} . lc shift @{ $options{extra} };
           }
   
           push @{ $options{extra} }, @options;
   
         return $self;          my $list = $self->_todo->file( $options{list} );
     }  
   
     sub RegisterActionHandler {          my $suffix = $self->_suffix || q{};
         my ( $handler, @types ) = @_;          if ( $list !~ / \Q$suffix\E $/ixms ) {
               $list .= $suffix;
         foreach my $type (@types) {  
             $action_handlers{$handler}{ $type->[0] } = $type->[1];  
         }          }
   
         return 1;          $options{list} = $list;
     }  
   
     sub content_type {return}          if ( $options{format} ) {
               $format_of{ ident $self } = $options{format};
               delete $options{format};
           }
   
     sub Dump {          my @method;
         my ($self) = @_;          foreach my $o qw( method action ) {
         return $self->fail( 'Unable to Dump [' . $self->_action . ']' );              if ( $options{$o} ) {
     }                  push @method, $options{$o};
               }
     sub Load {  
         my ($self) = @_;  
         return $self->fail( 'Unable to Load [' . $self->_action . ']' );  
     }  
   
     sub _handle_action {  
         my ( $self, $method, $params ) = @_;  
   
         if ( exists $action_handlers{$method}{ $self->_action } ) {  
             my $a = $action_handlers{$method}{ $self->_action };  
             return $self->$a( $self->_args, $params );  
         }          }
           $method = join q{_}, @method;
   
         return $self->fail(          return $method, %options;
             'Unable to handle ' . $method . ' [' . $self->_action . ']' );  
     }      }
   
     sub GET {      sub _handle_action {
         my ( $self, @args ) = @_;          my ( $self, @args ) = @_;
         return $self->_handle_action( 'GET', @args );  
     }  
   
     sub get_entry {          my ( $method, %options ) = $self->_parse_options(@args);
         my ( $self, $key ) = @_;  
   
         if ( !$key ) {          my $todo = $self->_todo;
             return $self->fail("get_entry requires arguments");          $todo->load( $options{list} );
         }  
         elsif ( ref $key eq 'ARRAY' ) {  
             my @entries;  
             foreach ( @{$key} ) {  
                 push @entries, $self->get_entry($_);  
             }  
             return @entries;  
         }  
   
         my @list = $self->get_list;          foreach my $class ( $self->actions ) {
               if ( $class->can($method) ) {
         my $entry;                  return Text::Todo::REST::API::Response->new(
         if ( $key =~ /^[[:xdigit:]]{32}$/xms ) {                      {   type   => $options{action},
             my $search = lc $key;                          format => $self->_format,
                           data   => $class->$method( $todo, \%options ),
         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 handle [' . $method . ']' );
             return $self->fail("Unable to find entry!");  
         }  
   
         return $entry;  
     }      }
   
     sub get_list {      sub _split_path {
         my ($self) = @_;          my ( $self, $path ) = @_;
   
         my $line = 1;          my %options = (
         return map ( {              action => undef,
                 line => $line++,              list   => undef,
                 md5  => md5_hex( $_->text ),              entry  => undef,
                 text => $_->text,              extra  => [],
             },          );
             $self->_todo->list );  
     }  
   
     sub get_files {          $path = defined $path ? $path : q{};
         my ($self) = @_;          $path =~ s{^/}{}xms;
         my $dir = $self->_todo->file('todo_dir');  
   
         if ( !$dir ) {          if ( $path =~ s/\.(\w+)$//xms ) {
             return $self->fail('Unable to find todo_dir');              $options{format} = $1;
         }          }
   
         my $file_regex = $self->_file_regex;          ( $options{list}, $options{action},
             @{ $options{extra} } ) = split '/', $path;
   
         opendir my $dh, $dir or croak "Couldn't opendir: $!";          if (!defined $options{action}) {
         my @files = grep {m/$file_regex/xms} readdir $dh;              if ( $options{list} ) {
         closedir $dh;                  $options{action} = 'list';
               }
               else {
                   $options{action} = 'files';
               }
           }
   
         return @files;          $options{action} = lc( $options{action} );
     }  
   
     sub get_tags {          if ($options{action} eq 'entry'
         my ( $self, $tag ) = @_;           || $options{action} eq 'tags'
         my $ident = ident($self);          ) {
               $options{ $options{action} } = shift @{ $options{extra} };
           }
   
         return $todo_of{$ident}->listtag($tag);          return %options;
     }      }
   
     sub POST {      sub GET    { return shift->_handle_action( 'GET',    @_ ) }
         my ( $self, @args ) = @_;      sub POST   { return shift->_handle_action( 'POST',   @_ ) }
         return $self->_handle_action( 'POST', @args );      sub PUT    { return shift->_handle_action( 'PUT',    @_ ) }
     }      sub DELETE { return shift->_handle_action( 'DELETE', @_ ) }
   
     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 ) = @_;
         croak(@message);          croak(@message);
     }      }
   
     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.6  
changed lines
  Added in v.1.20

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