version 1.6, 2010/01/18 03:51:40 |
version 1.16, 2010/01/26 06:03:11 |
|
|
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.15 2010/01/26 05:47:30 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, |
); |
); |
|
|
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{ |
.* |
.* |
|
|
$ |
$ |
}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, @args ) = @_; |
} |
|
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 (@args) { |
|
if ( !ref $args[0] ) { |
|
$options{path} = shift @args; |
} |
} |
|
|
$todo_dir = File::Spec->catdir(@todo_dir); |
if ( ref $args[0] eq 'HASH' ) { |
|
my $opts = shift @args; |
|
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}; |
|
|
|
foreach my $o ( keys %opts ) { |
|
if ( defined $opts{$o} ) { |
|
$options{$o} = $opts{$o}; |
|
} |
|
} |
} |
} |
|
|
$todo_of{$ident} = Text::Todo->new( |
if ( $options{action} eq 'entry' && @{ $options{args} } ) { |
{ todo_dir => $todo_dir, |
$options{entry} = shift @{ $options{args} }; |
todo_file => $todo_file, |
if ( @{ $options{args} } ) { |
|
$options{action} .= q{_} . lc shift @{ $options{args} }; |
} |
} |
) or $self->fail('Unable to create Text::Todo object'); |
} |
|
|
$todo_of{$ident}->load('todo_file') |
push @{ $options{args} }, @args; |
or $self->fail('Unable to create Text::Todo object'); |
|
|
|
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' ) { |
foreach my $class ( $self->actions ) { |
my @entries; |
if ( $class->can($method) ) { |
foreach ( @{$key} ) { |
return Text::Todo::REST::API::Response->new( |
push @entries, $self->get_entry($_); |
{ type => $options{action}, |
|
format => $self->_format, |
|
data => $class->$method( $todo, \%options ), |
|
} |
|
); |
} |
} |
return @entries; |
|
} |
} |
|
|
my @list = $self->get_list; |
return $self->fail( 'Unable to handle [' . $method . ']' ); |
|
} |
|
|
my $entry; |
sub _split_path { |
if ( $key =~ /^[[:xdigit:]]{32}$/xms ) { |
my ( $self, $path ) = @_; |
my $search = lc $key; |
|
|
|
ENTRY: foreach my $e (@list) { |
my %options = ( |
if ( $search eq $e->{md5} ) { |
list => undef, |
$entry = $e; |
action => undef, |
last ENTRY; |
args => [], |
} |
); |
} |
|
} |
|
elsif ( $key =~ /^\d+$/xms ) { |
|
$entry = $list[ $key - 1 ]; |
|
} |
|
|
|
if ( !$entry ) { |
$path = defined $path ? $path : q{}; |
return $self->fail("Unable to find entry!"); |
$path =~ s{^/}{}xms; |
|
|
|
if ( $path =~ s/\.(\w+)$//xms ) { |
|
$options{format} = $1; |
} |
} |
|
|
return $entry; |
( $options{list}, $options{action}, @{ $options{args} } ) = split '/', |
} |
$path; |
|
|
sub get_list { |
if ( $options{list} ) { |
my ($self) = @_; |
$options{action} ||= 'list'; |
|
|
my $line = 1; |
my $list = $options{list}; |
return map ( { |
|
line => $line++, |
|
md5 => md5_hex( $_->text ), |
|
text => $_->text, |
|
}, |
|
$self->_todo->list ); |
|
} |
|
|
|
sub get_files { |
if ( ( lc $options{list} ) eq 'files' ) { |
my ($self) = @_; |
$options{action} = lc $list; |
my $dir = $self->_todo->file('todo_dir'); |
$list = q{}; |
|
} |
|
|
if ( !$dir ) { |
$options{list} = $list; |
return $self->fail('Unable to find todo_dir'); |
|
} |
} |
|
|
my $file_regex = $self->_file_regex; |
if ( @{ $options{args} } && ( lc $options{args}[0] ) eq 'entry' ) { |
|
$options{action} = lc shift @{ $options{args} }; |
|
} |
|
|
opendir my $dh, $dir or croak "Couldn't opendir: $!"; |
return %options; |
my @files = grep {m/$file_regex/xms} readdir $dh; |
|
closedir $dh; |
|
|
|
return @files; |
|
} |
} |
|
|
sub get_tags { |
sub GET { |
my ( $self, $tag ) = @_; |
my ( $self, @args ) = @_; |
my $ident = ident($self); |
return $self->_handle_action( 'GET', @args ); |
|
|
return $todo_of{$ident}->listtag($tag); |
|
} |
} |
|
|
sub POST { |
sub POST { |
|
|
} |
} |
|
|
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) = @_; |