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

version 1.8, 2010/01/19 03:18:34 version 1.10, 2010/01/24 04:17:39
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.9 2010/01/23 07:15:40 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 Module::Pluggable  
     instantiate => 'new',  
     search_path => __PACKAGE__ . '::Representations',  
     sub_name    => 'representations';  
   
 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 22 
         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 36 
         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 47 
             $              $
         }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 58 
             $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, @args ) = @_;
   
         foreach my $type (@types) {          my %options = (
             $action_handlers{$handler}{ $type->[0] } = $type->[1];              method => lc $method,
               list   => '',
               action => 'files',
               args   => [],
           );
   
           if (@args) {
               if ( !ref $args[0] ) {
                   $options{path} = shift @args;
               }
   
               if ( ref $args[0] eq 'HASH' ) {
                   my $opts = shift @args;
                   foreach my $o ( keys %{$opts} ) {
                       $options{$o} = $opts->{$o};
                   }
               }
         }          }
   
         return 1;          if ( exists $options{path} ) {
     }              my %opts = $self->_split_path( $options{path} );
               delete $options{path};
   
     sub content_type {return}              foreach my $o ( keys %opts ) {
                   if ( defined $opts{$o} ) {
                       $options{$o} = $opts{$o};
                   }
               }
           }
   
     sub _handle_representation {          if ( $options{action} eq 'entry' && @{ $options{args} } ) {
         my ( $self, $type, @args ) = @_;              $options{entry} = shift @{ $options{args} };
               if ( @{ $options{args} } ) {
                   $options{action} = lc shift @{ $options{args} };
               }
           }
   
         my $method = join q{_}, $type, $self->_action;          push @{ $options{args} }, @args;
   
         foreach my $class ( $self->representations ) {          $options{list}
             if (   $class->can_format( $self->_format )              = defined $options{list} ? $options{list} : 'todo_file';
                 && $class->can( $method ) )  
             {          if ( $options{format} ) {
                 return $class->$method($self->_format, @args);              $format_of{ ident $self } = $options{format};
               delete $options{format};
           }
   
           my @method;
           foreach my $o qw( method action ) {
               if ( $options{$o} ) {
                   push @method, $options{$o};
             }              }
         }          }
           $method = join q{_}, @method;
   
         return $self->fail(          return $method, %options;
             'Unable to ' . $type . ' [' . $self->_action . ']' );  
     }      }
   
     sub Dump {      sub _handle_action {
         my ( $self, @args ) = @_;          my ( $self, @args ) = @_;
         return $self->_handle_representation( 'dump', @args );  
           my ( $method, %options ) = $self->_parse_options(@args);
   
           my $todo = $self->_todo;
           $todo->load( $options{list} );
   
           foreach my $class ($self) {
               if ( $class->can($method) ) {
                   my @data = $class->$method( $todo, \%options );
   
                   return Text::Todo::REST::API::Response->new({
                       type   => $options{action},
                       format => $self->_format,
                       data   => \@data,
                   });
               }
           }
   
           return $self->fail( 'Unable to handle [' . $method . ']' );
     }      }
   
     sub _handle_action {      sub _split_path {
         my ( $self, $method, $params ) = @_;          my ( $self, $path ) = @_;
   
         if ( exists $action_handlers{$method}{ $self->_action } ) {          my %options = (
             my $a = $action_handlers{$method}{ $self->_action };              list   => undef,
             return $self->$a( $self->_args, $params );              action => undef,
               args   => [],
           );
   
           $path = defined $path ? $path : q{};
           $path =~ s{^/}{}xms;
   
           if ( $path =~ s/\.(\w+)$//xms ) {
               $options{format} = $1;
         }          }
   
         return $self->fail(          ( $options{list}, $options{action}, @{ $options{args} } ) = split '/',
             'Unable to handle ' . $method . ' [' . $self->_action . ']' );              $path;
   
           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 {      sub GET {
Line 150 
Line 192 
     }      }
   
     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");
Line 162 
Line 204 
             }              }
             return @entries;              return @entries;
         }          }
           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;          my @list = $self->get_list($todo);
   
         my $entry;          my $entry;
         if ( $key =~ /^[[:xdigit:]]{32}$/xms ) {          if ( $key =~ /^[[:xdigit:]]{32}$/xms ) {
Line 188 
Line 238 
     }      }
   
     sub get_list {      sub get_list {
         my ($self) = @_;          my ( $self, $todo ) = @_;
   
         my $line = 1;          my $line = 1;
         return map ( {          return map ( {
Line 196 
Line 246 
                 md5  => md5_hex( $_->text ),                  md5  => md5_hex( $_->text ),
                 text => $_->text,                  text => $_->text,
             },              },
             $self->_todo->list );              $todo->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 217 
Line 267 
     }      }
   
     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 246 
Line 294 
     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.10

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