=================================================================== RCS file: /cvs/todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm,v retrieving revision 1.7 retrieving revision 1.16 diff -u -r1.7 -r1.16 --- todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/18 13:47:53 1.7 +++ todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/26 06:03:11 1.16 @@ -1,31 +1,23 @@ package Text::Todo::REST::API; -# $AFresh1: API.pm,v 1.6 2010/01/18 03:51:40 andrew Exp $ +# $AFresh1: API.pm,v 1.15 2010/01/26 05:47:30 andrew Exp $ use warnings; use strict; use Carp; -use Data::Dumper; use Text::Todo; +use Text::Todo::REST::API::Response; -use Class::Std::Utils; use Module::Pluggable - require => 1, - search_path => __PACKAGE__ . '::Representations', - sub_name => 'representations'; -use Digest::MD5 qw/ md5_hex /; + instantiate => 'new', + search_path => 'Text::Todo::REST::API::Actions', + sub_name => 'actions'; +use Class::Std::Utils; + 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 %todo_of, @@ -33,10 +25,7 @@ my %suffix_of, my %file_regex_of, - my %user_of, - my %list_of, - my %action_of, - my %args_of, + my %format_of, my %action_handlers, ); @@ -44,34 +33,15 @@ sub new { 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 $ident = ident($self); - if ( ref $self eq __PACKAGE__ && $format ) { - my $found_handler = 0; - REP: foreach my $rep ( $self->representations ) { - if ( $rep->_handles($format) ) { - $self = $rep->new($options); - $found_handler = 1; - last REP; - } - } - if ( !$found_handler ) { - croak("Unable to find handler for [$format]\n"); - } + $format_of{$ident} = $options->{default_format}; + if ( $options->{format} ) { + $format_of{$ident} = $options->{format}; } - $suffix_of{$ident} = $options->{suffix} || '.txt'; + $suffix_of{$ident} = $options->{suffix} || '.txt'; $file_regex_of{$ident} = $options->{file_regex} || qr{ .* @@ -81,146 +51,158 @@ $ }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 { $todo_of{$ident} = Text::Todo->new( - { todo_dir => $options->{todo_dir}, - todo_file => $options->{todo_file}, - } - ) }; + eval { + $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' . $@); + $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; } - sub RegisterActionHandler { - my ( $handler, @types ) = @_; + sub _parse_options { + my ( $self, $method, @args ) = @_; - foreach my $type (@types) { - $action_handlers{$handler}{ $type->[0] } = $type->[1]; - } + my %options = ( + 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 (@args) { + if ( !ref $args[0] ) { + $options{path} = shift @args; + } - sub Dump { - my ($self) = @_; - return $self->fail( 'Unable to Dump [' . $self->_action . ']' ); - } + if ( ref $args[0] eq 'HASH' ) { + my $opts = shift @args; + foreach my $o ( keys %{$opts} ) { + $options{$o} = $opts->{$o}; + } + } + } - sub Load { - my ($self) = @_; - return $self->fail( 'Unable to Load [' . $self->_action . ']' ); - } + if ( exists $options{path} ) { + my %opts = $self->_split_path( $options{path} ); + delete $options{path}; - sub _handle_action { - my ( $self, $method, $params ) = @_; + foreach my $o ( keys %opts ) { + if ( defined $opts{$o} ) { + $options{$o} = $opts{$o}; + } + } + } - if ( exists $action_handlers{$method}{ $self->_action } ) { - my $a = $action_handlers{$method}{ $self->_action }; - return $self->$a( $self->_args, $params ); + if ( $options{action} eq 'entry' && @{ $options{args} } ) { + $options{entry} = shift @{ $options{args} }; + if ( @{ $options{args} } ) { + $options{action} .= q{_} . lc shift @{ $options{args} }; + } } - return $self->fail( - 'Unable to handle ' . $method . ' [' . $self->_action . ']' ); - } + push @{ $options{args} }, @args; - sub GET { - my ( $self, @args ) = @_; - return $self->_handle_action( 'GET', @args ); - } + my $list = $self->_todo->file( $options{list} ); - sub get_entry { - my ( $self, $key ) = @_; + my $suffix = $self->_suffix || q{}; + if ( $list !~ / \Q$suffix\E $/ixms ) { + $list .= $suffix; + } - if ( !$key ) { - return $self->fail("get_entry requires arguments"); + $options{list} = $list; + + if ( $options{format} ) { + $format_of{ ident $self } = $options{format}; + delete $options{format}; } - elsif ( ref $key eq 'ARRAY' ) { - my @entries; - foreach ( @{$key} ) { - push @entries, $self->get_entry($_); + + my @method; + foreach my $o qw( method action ) { + if ( $options{$o} ) { + push @method, $options{$o}; } - return @entries; } + $method = join q{_}, @method; - my @list = $self->get_list; + return $method, %options; + } - my $entry; - if ( $key =~ /^[[:xdigit:]]{32}$/xms ) { - my $search = lc $key; + sub _handle_action { + my ( $self, @args ) = @_; - ENTRY: foreach my $e (@list) { - if ( $search eq $e->{md5} ) { - $entry = $e; - last ENTRY; - } + my ( $method, %options ) = $self->_parse_options(@args); + + my $todo = $self->_todo; + $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 find entry!"); - } - - return $entry; + return $self->fail( 'Unable to handle [' . $method . ']' ); } - sub get_list { - my ($self) = @_; + sub _split_path { + my ( $self, $path ) = @_; - my $line = 1; - return map ( { - line => $line++, - md5 => md5_hex( $_->text ), - text => $_->text, - }, - $self->_todo->list ); - } + my %options = ( + list => undef, + action => undef, + args => [], + ); - sub get_files { - my ($self) = @_; - my $dir = $self->_todo->file('todo_dir'); + $path = defined $path ? $path : q{}; + $path =~ s{^/}{}xms; - if ( !$dir ) { - return $self->fail('Unable to find todo_dir'); + if ( $path =~ s/\.(\w+)$//xms ) { + $options{format} = $1; } - my $file_regex = $self->_file_regex; + ( $options{list}, $options{action}, @{ $options{args} } ) = split '/', + $path; - opendir my $dh, $dir or croak "Couldn't opendir: $!"; - my @files = grep {m/$file_regex/xms} readdir $dh; - closedir $dh; + if ( $options{list} ) { + $options{action} ||= 'list'; - return @files; - } + my $list = $options{list}; - sub get_tags { - my ( $self, $tag ) = @_; - my $ident = ident($self); + if ( ( lc $options{list} ) eq 'files' ) { + $options{action} = lc $list; + $list = q{}; + } - return $todo_of{$ident}->listtag($tag); + $options{list} = $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 POST { my ( $self, @args ) = @_; return $self->_handle_action( 'POST', @args ); @@ -244,10 +226,7 @@ sub _todo { my ($self) = @_; return $todo_of{ ident $self }; } sub _suffix { my ($self) = @_; return $suffix_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 _args { my ($self) = @_; return $args_of{ ident $self}; } + sub _format { my ($self) = @_; return $format_of{ ident $self}; } sub DESTROY { my ($self) = @_;