=================================================================== RCS file: /cvs/todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm,v retrieving revision 1.1 retrieving revision 1.8 diff -u -r1.1 -r1.8 --- todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/16 16:01:07 1.1 +++ todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm 2010/01/19 03:18:34 1.8 @@ -1,24 +1,266 @@ package Text::Todo::REST::API; -# $RedRiver$ +# $AFresh1: API.pm,v 1.7 2010/01/18 13:47:53 andrew Exp $ use warnings; use strict; use Carp; +use Data::Dumper; +use Text::Todo; + +use Class::Std::Utils; +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'); -# Other recommended modules (uncomment to use): -# use IO::Prompt; -# use Perl6::Export; -# use Perl6::Slurp; -# use Perl6::Say; +&RegisterActionHandler( + 'GET', + [ list => 'get_list' ], + [ entry => 'get_entry' ], + [ tags => 'get_tags' ], + [ files => 'get_files' ], +); +{ + my @attr_refs = \( + my %todo_of, -# Module implementation here + my %suffix_of, + my %file_regex_of, + my %format_of, + my %user_of, + my %list_of, + my %action_of, + my %args_of, -1; # Magic true value required at end of module + my %action_handlers, + ); + + sub new { + my ( $class, $options ) = @_; + + my $self = bless anon_scalar(), $class; + my $ident = ident($self); + + $format_of{$ident} = $options->{default_format}; + if ( $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'; + + $file_regex_of{$ident} = $options->{file_regex} || qr{ + .* + todo + .* + \Q$suffix_of{$ident}\E + $ + }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}, + } + ); + }; + if ($@) { + $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 ) = @_; + + foreach my $type (@types) { + $action_handlers{$handler}{ $type->[0] } = $type->[1]; + } + + return 1; + } + + sub content_type {return} + + sub _handle_representation { + my ( $self, $type, @args ) = @_; + + my $method = join q{_}, $type, $self->_action; + + foreach my $class ( $self->representations ) { + if ( $class->can_format( $self->_format ) + && $class->can( $method ) ) + { + return $class->$method($self->_format, @args); + } + } + + return $self->fail( + 'Unable to ' . $type . ' [' . $self->_action . ']' ); + } + + sub Dump { + my ( $self, @args ) = @_; + return $self->_handle_representation( 'dump', @args ); + } + + 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 ); + } + + return $self->fail( + 'Unable to handle ' . $method . ' [' . $self->_action . ']' ); + } + + sub GET { + my ( $self, @args ) = @_; + return $self->_handle_action( 'GET', @args ); + } + + sub get_entry { + my ( $self, $key ) = @_; + + if ( !$key ) { + return $self->fail("get_entry requires arguments"); + } + elsif ( ref $key eq 'ARRAY' ) { + my @entries; + foreach ( @{$key} ) { + push @entries, $self->get_entry($_); + } + return @entries; + } + + my @list = $self->get_list; + + my $entry; + if ( $key =~ /^[[:xdigit:]]{32}$/xms ) { + my $search = lc $key; + + 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 find entry!"); + } + + return $entry; + } + + sub get_list { + my ($self) = @_; + + my $line = 1; + return map ( { + line => $line++, + md5 => md5_hex( $_->text ), + text => $_->text, + }, + $self->_todo->list ); + } + + sub get_files { + my ($self) = @_; + my $dir = $self->_todo->file('todo_dir'); + + if ( !$dir ) { + return $self->fail('Unable to find todo_dir'); + } + + my $file_regex = $self->_file_regex; + + opendir my $dh, $dir or croak "Couldn't opendir: $!"; + my @files = grep {m/$file_regex/xms} readdir $dh; + closedir $dh; + + return @files; + } + + sub get_tags { + my ( $self, $tag ) = @_; + my $ident = ident($self); + + return $todo_of{$ident}->listtag($tag); + } + + sub POST { + my ( $self, @args ) = @_; + return $self->_handle_action( 'POST', @args ); + } + + sub PUT { + my ( $self, @args ) = @_; + return $self->_handle_action( 'PUT', @args ); + } + + sub DELETE { + my ( $self, @args ) = @_; + return $self->_handle_action( 'DELETE', @args ); + } + + sub fail { + my ( $self, @message ) = @_; + croak(@message); + } + + 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 _format { my ($self) = @_; return $format_of{ ident $self}; } + sub _args { my ($self) = @_; return $args_of{ ident $self}; } + + sub DESTROY { + my ($self) = @_; + my $ident = ident $self; + foreach my $attr_ref (@attr_refs) { + delete $attr_ref->{$ident}; + } + } +} +1; # Magic true value required at end of module __END__ =head1 NAME