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

version 1.8, 2010/01/19 03:18:34 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.7 2010/01/18 13:47:53 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 Digest::MD5 qw/ md5_hex /;  
   
 use Module::Pluggable  use Module::Pluggable
     instantiate => 'new',      instantiate => 'new',
     search_path => __PACKAGE__ . '::Representations',      search_path => 'Text::Todo::REST::API::Actions',
     sub_name    => 'representations';      sub_name    => 'actions';
   
   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,
Line 35 
Line 26 
         my %file_regex_of,          my %file_regex_of,
   
         my %format_of,          my %format_of,
         my %user_of,  
         my %list_of,  
         my %action_of,  
         my %args_of,  
   
         my %action_handlers,          my %action_handlers,
     );      );
Line 53 
Line 40 
         if ( $options->{format} ) {          if ( $options->{format} ) {
             $format_of{$ident} = $options->{format};              $format_of{$ident} = $options->{format};
         }          }
         elsif ($options->{path_info}  
             && $options->{path_info} =~ s/\.(\w+)$//xms )  
         {  
             $format_of{$ident} = $1;  
         }  
   
         $suffix_of{$ident} = $options->{suffix} || '.txt';          $suffix_of{$ident} = $options->{suffix} || '.txt';
   
Line 69 
Line 51 
             $              $
         }ixms;          }ixms;
   
         $options->{path_info} ||= q{};  
         $options->{path_info} =~ s{^/}{}xms;  
         ( $list_of{$ident}, $action_of{$ident}, @{ $args_of{$ident} }, )  
             = split '/', $options->{path_info};  
   
         if ( $list_of{$ident} ) {  
             $action_of{$ident} ||= 'list';  
         }  
         else {  
             $action_of{$ident} = 'files';  
         }  
   
         eval {          eval {
             $todo_of{$ident} = Text::Todo->new(              $todo_of{$ident} = Text::Todo->new(
                 {   todo_dir  => $options->{todo_dir},                  {   todo_dir  => $options->{todo_dir},
Line 92 
Line 62 
             $self->fail( 'Unable to create Text::Todo object' . $@ );              $self->fail( 'Unable to create Text::Todo object' . $@ );
         }          }
   
         $todo_of{$ident}->load('todo_file')  
             or $self->fail('Unable to load todo_file in Text::Todo object');  
   
         return $self;          return $self;
     }      }
   
     sub RegisterActionHandler {      sub _parse_options {
         my ( $handler, @types ) = @_;          my ( $self, $method, @options) = @_;
   
         foreach my $type (@types) {          my %options = (
             $action_handlers{$handler}{ $type->[0] } = $type->[1];              method => lc $method,
         }              list   => '',
               action => 'files',
               args   => {},
   
         return 1;              suffix     => $self->_suffix,
     }              file_regex => $self->_file_regex,
               format     => $self->_format,
           );
   
     sub content_type {return}          if (@options) {
               if ( !ref $options[0] ) {
                   $options{path} = shift @options;
               }
   
     sub _handle_representation {              if ( ref $options[0] eq 'HASH' ) {
         my ( $self, $type, @args ) = @_;                  my $opts = shift @options;
                   foreach my $o ( keys %{$opts} ) {
                       $options{$o} = $opts->{$o};
                   }
               }
           }
   
         my $method = join q{_}, $type, $self->_action;          if ( exists $options{path} ) {
               my %opts = $self->_split_path( $options{path} );
               delete $options{path};
   
         foreach my $class ( $self->representations ) {              foreach my $o ( keys %opts ) {
             if (   $class->can_format( $self->_format )                  if ( defined $opts{$o} ) {
                 && $class->can( $method ) )                      $options{$o} = $opts{$o};
             {                  }
                 return $class->$method($self->_format, @args);  
             }              }
         }          }
   
         return $self->fail(          if ( $options{action} eq 'entry' && @{ $options{extra} } ) {
             'Unable to ' . $type . ' [' . $self->_action . ']' );              $options{action} .= q{_} . lc shift @{ $options{extra} };
     }          }
   
           push @{ $options{extra} }, @options;
   
     sub Dump {          my $list = $self->_todo->file( $options{list} );
         my ( $self, @args ) = @_;  
         return $self->_handle_representation( 'dump', @args );  
     }  
   
     sub _handle_action {          my $suffix = $self->_suffix || q{};
         my ( $self, $method, $params ) = @_;          if ( $list !~ / \Q$suffix\E $/ixms ) {
               $list .= $suffix;
         if ( exists $action_handlers{$method}{ $self->_action } ) {  
             my $a = $action_handlers{$method}{ $self->_action };  
             return $self->$a( $self->_args, $params );  
         }          }
   
         return $self->fail(          $options{list} = $list;
             'Unable to handle ' . $method . ' [' . $self->_action . ']' );  
     }  
   
     sub GET {          if ( $options{format} ) {
         my ( $self, @args ) = @_;              $format_of{ ident $self } = $options{format};
         return $self->_handle_action( 'GET', @args );              delete $options{format};
     }  
   
     sub get_entry {  
         my ( $self, $key ) = @_;  
   
         if ( !$key ) {  
             return $self->fail("get_entry requires arguments");  
         }          }
         elsif ( ref $key eq 'ARRAY' ) {  
             my @entries;          my @method;
             foreach ( @{$key} ) {          foreach my $o qw( method action ) {
                 push @entries, $self->get_entry($_);              if ( $options{$o} ) {
                   push @method, $options{$o};
             }              }
             return @entries;  
         }          }
           $method = join q{_}, @method;
   
         my @list = $self->get_list;          return $method, %options;
       }
   
         my $entry;      sub _handle_action {
         if ( $key =~ /^[[:xdigit:]]{32}$/xms ) {          my ( $self, @args ) = @_;
             my $search = lc $key;  
   
         ENTRY: foreach my $e (@list) {          my ( $method, %options ) = $self->_parse_options(@args);
                 if ( $search eq $e->{md5} ) {  
                     $entry = $e;          my $todo = $self->_todo;
                     last ENTRY;          $todo->load( $options{list} );
                 }  
           foreach my $class ( $self->actions ) {
               if ( $class->can($method) ) {
                   return Text::Todo::REST::API::Response->new(
                       {   type   => $options{action},
                           format => $self->_format,
                           data   => $class->$method( $todo, \%options ),
                       }
                   );
             }              }
         }          }
         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);
Line 246 
Line 209 
     sub _todo       { my ($self) = @_; return $todo_of{ ident $self }; }      sub _todo       { my ($self) = @_; return $todo_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 _list       { my ($self) = @_; return $list_of{ ident $self}; }  
     sub _action     { my ($self) = @_; return $action_of{ ident $self}; }  
     sub _format     { my ($self) = @_; return $format_of{ ident $self}; }      sub _format     { my ($self) = @_; return $format_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.8  
changed lines
  Added in v.1.20

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