[BACK]Return to API.pm CVS log [TXT][DIR] Up to [local] / todotxt / Text-Todo-REST-API / lib / Text / Todo / REST

Annotation of todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm, Revision 1.4

1.1       andrew      1: package Text::Todo::REST::API;
                      2:
1.4     ! andrew      3: # $AFresh1: API.pm,v 1.3 2010/01/17 23:48:06 andrew Exp $
1.1       andrew      4:
                      5: use warnings;
                      6: use strict;
                      7: use Carp;
                      8:
1.2       andrew      9: use Data::Dumper;
                     10: use Text::Todo;
1.1       andrew     11:
1.2       andrew     12: use Class::Std;
                     13: use Digest::MD5 qw/ md5_hex /;
                     14: use File::Spec;
1.1       andrew     15:
1.2       andrew     16: use version; our $VERSION = qv('0.0.1');
1.1       andrew     17:
1.2       andrew     18: &RegisterActionHandler(
                     19:     'GET',
                     20:     [ list  => 'get_list' ],
                     21:     [ entry => 'get_entry' ],
                     22:     [ tags  => 'get_tags' ],
                     23:     [ files => 'get_files' ],
                     24: );
                     25:
                     26: {
                     27:     my (%todo_of,         %basedir_of,    %subdir_of,
                     28:         %suffix_of,       %file_regex_of, %user_of,
                     29:         %list_of,         %action_of,     %args_of,
                     30:         %action_handlers, %format_handlers,
                     31:     ) : ATTRS;
                     32:
                     33:     sub BUILD {
                     34:         my ( $self, $ident, $options ) = @_;
                     35:
                     36:         my $format = $options->{default_format};
                     37:         if ( $options->{format} ) {
                     38:             $format = $options->{format};
                     39:         }
                     40:         elsif ($options->{path_info}
                     41:             && $options->{path_info} =~ s/\.(\w+)$//xms )
                     42:         {
                     43:             $format = $1;
                     44:         }
                     45:
                     46:         if ( ref $self eq __PACKAGE__ && $format ) {
                     47:             if ( $format_handlers{$format} ) {
                     48:                 bless $self, $format_handlers{$format};
                     49:             }
                     50:             else {
                     51:                 croak("Unable to find handler for [$format]\n");
                     52:             }
                     53:         }
                     54:
                     55:         $basedir_of{$ident} = $options->{basedir};
                     56:         $subdir_of{$ident}  = $options->{subdir};
                     57:         $suffix_of{$ident}  = $options->{suffix} || '.txt';
                     58:
                     59:         $file_regex_of{$ident} = $options->{file_regex} || qr{
                     60:             .*
                     61:             todo
                     62:             .*
                     63:             \Q$suffix_of{$ident}\E
                     64:             $
                     65:         }ixms;
                     66:
                     67:         if ( !$basedir_of{$ident} ) {
                     68:             return $self->fail('Required option [basedir]');
                     69:         }
                     70:
                     71:         $options->{path_info} ||= q{};
                     72:         $options->{path_info} =~ s{^/}{}xms;
                     73:         (   $user_of{$ident},   $list_of{$ident},
                     74:             $action_of{$ident}, @{ $args_of{$ident} },
                     75:         ) = split '/', $options->{path_info};
                     76:
                     77:         if ( $list_of{$ident} ) {
                     78:             $action_of{$ident} ||= 'list';
                     79:         }
                     80:         elsif ( $user_of{$ident} ) {
                     81:             $action_of{$ident} = 'files';
                     82:         }
                     83:
                     84:         my @todo_dir = $basedir_of{$ident};
                     85:
                     86:         my $todo_dir;
                     87:         if ( $user_of{$ident} ) {
                     88:             push @todo_dir, $user_of{$ident};
                     89:             if ( $subdir_of{$ident} ) {
                     90:                 push @todo_dir, $subdir_of{$ident};
                     91:             }
                     92:
                     93:             $todo_dir = File::Spec->catdir(@todo_dir);
                     94:         }
                     95:
                     96:         my $todo_file;
                     97:         if ( $list_of{$ident} ) {
                     98:             $todo_file = $list_of{$ident} . $suffix_of{$ident};
                     99:         }
                    100:
                    101:         $todo_of{$ident} = Text::Todo->new(
                    102:             {   todo_dir  => $todo_dir,
                    103:                 todo_file => $todo_file,
                    104:             }
                    105:         ) or $self->fail('Unable to create Text::Todo object');
                    106:
                    107:         $todo_of{$ident}->load('todo_file')
                    108:             or $self->fail('Unable to create Text::Todo object');
                    109:
                    110:         return;
                    111:     }
                    112:
                    113:     sub RegisterFormatHandler {
                    114:         my ( $handler, @types ) = @_;
                    115:
                    116:         foreach my $type (@types) {
                    117:             $format_handlers{$type} = $handler;
                    118:         }
                    119:
                    120:         return 1;
                    121:     }
                    122:
                    123:     sub RegisterActionHandler {
                    124:         my ( $handler, @types ) = @_;
                    125:
                    126:         foreach my $type (@types) {
                    127:             $action_handlers{$handler}{ $type->[0] } = $type->[1];
                    128:         }
                    129:
                    130:         return 1;
                    131:     }
1.4     ! andrew    132:
        !           133:     sub content_type { return };
1.2       andrew    134:
                    135:     sub Dump {
                    136:         my ($self) = @_;
                    137:         return $self->fail( 'Unable to Dump [' . $self->_action . ']' );
                    138:     }
                    139:
                    140:     sub Load {
                    141:         my ($self) = @_;
                    142:         return $self->fail( 'Unable to Load [' . $self->_action . ']' );
                    143:     }
                    144:
1.3       andrew    145:     sub _handle_action {
                    146:         my ( $self, $method, $params ) = @_;
1.2       andrew    147:
1.3       andrew    148:         if ( exists $action_handlers{$method}{ $self->_action } ) {
                    149:             my $a = $action_handlers{$method}{ $self->_action };
                    150:             return $self->$a( $self->_args, $params );
1.2       andrew    151:         }
                    152:
1.3       andrew    153:         return $self->fail(
                    154:             'Unable to handle ' . $method . ' [' . $self->_action . ']' );
                    155:     }
                    156:
                    157:     sub GET {
                    158:         my ( $self, @args ) = @_;
                    159:         return $self->_handle_action( 'GET', @args );
1.2       andrew    160:     }
                    161:
                    162:     sub get_entry {
                    163:         my ( $self, $key ) = @_;
                    164:
                    165:         if ( !$key ) {
                    166:             return $self->fail("get_entry requires arguments");
                    167:         }
                    168:         elsif ( ref $key eq 'ARRAY' ) {
                    169:             my @entries;
                    170:             foreach ( @{$key} ) {
                    171:                 push @entries, $self->get_entry($_);
                    172:             }
                    173:             return @entries;
                    174:         }
                    175:
                    176:         my @list = $self->get_list;
                    177:
                    178:         my $entry;
                    179:         if ( $key =~ /^[[:xdigit:]]{32}$/xms ) {
                    180:             my $search = lc $key;
                    181:
                    182:         ENTRY: foreach my $e (@list) {
                    183:                 if ( $search eq $e->{md5} ) {
                    184:                     $entry = $e;
                    185:                     last ENTRY;
                    186:                 }
                    187:             }
                    188:         }
                    189:         elsif ( $key =~ /^\d+$/xms ) {
                    190:             $entry = $list[ $key - 1 ];
                    191:         }
                    192:
                    193:         if ( !$entry ) {
                    194:             return $self->fail("Unable to find entry!");
                    195:         }
                    196:
                    197:         return $entry;
                    198:     }
                    199:
                    200:     sub get_list {
                    201:         my ($self) = @_;
                    202:
                    203:         my $line = 1;
                    204:         return map ( {
                    205:                 line => $line++,
                    206:                 md5  => md5_hex( $_->text ),
                    207:                 text => $_->text,
                    208:             },
                    209:             $self->_todo->list );
                    210:     }
                    211:
                    212:     sub get_files {
                    213:         my ($self) = @_;
                    214:         my $dir = $self->_todo->file('todo_dir');
                    215:
                    216:         if ( !$dir ) {
                    217:             return $self->fail('Unable to find todo_dir');
                    218:         }
                    219:
                    220:         my $file_regex = $self->_file_regex;
                    221:
                    222:         opendir my $dh, $dir or croak "Couldn't opendir: $!";
                    223:         my @files = grep {m/$file_regex/xms} readdir $dh;
                    224:         closedir $dh;
                    225:
                    226:         return @files;
                    227:     }
                    228:
                    229:     sub get_tags {
                    230:         my ( $self, $tag ) = @_;
                    231:         my $ident = ident($self);
                    232:
                    233:         return $todo_of{$ident}->listtag($tag);
                    234:     }
                    235:
1.3       andrew    236:     sub POST   {
                    237:         my ( $self, @args ) = @_;
                    238:         return $self->_handle_action( 'POST', @args );
                    239:     }
                    240:
                    241:     sub PUT    {
                    242:         my ( $self, @args ) = @_;
                    243:         return $self->_handle_action( 'PUT', @args );
                    244:     }
                    245:
                    246:     sub DELETE {
                    247:         my ( $self, @args ) = @_;
                    248:         return $self->_handle_action( 'DELETE', @args );
                    249:     }
1.2       andrew    250:
                    251:     sub fail {
                    252:         my ( $self, @message ) = @_;
                    253:         croak(@message);
                    254:     }
                    255:
                    256:     sub _todo       { my ($self) = @_; return $todo_of{ ident $self }; }
                    257:     sub _basedir    { my ($self) = @_; return $basedir_of{ ident $self}; }
                    258:     sub _subdir     { my ($self) = @_; return $subdir_of{ ident $self}; }
                    259:     sub _suffix     { my ($self) = @_; return $suffix_of{ ident $self}; }
                    260:     sub _file_regex { my ($self) = @_; return $file_regex_of{ ident $self}; }
                    261:     sub _user       { my ($self) = @_; return $user_of{ ident $self}; }
                    262:     sub _list       { my ($self) = @_; return $list_of{ ident $self}; }
                    263:     sub _action     { my ($self) = @_; return $action_of{ ident $self}; }
                    264:     sub _args       { my ($self) = @_; return $args_of{ ident $self}; }
1.1       andrew    265:
1.2       andrew    266: }
                    267: 1;    # Magic true value required at end of module
1.1       andrew    268: __END__
                    269:
                    270: =head1 NAME
                    271:
                    272: Text::Todo::REST::API - [One line description of module's purpose here]
                    273:
                    274:
                    275: =head1 VERSION
                    276:
                    277: This document describes Text::Todo::REST::API version 0.0.1
                    278:
                    279:
                    280: =head1 SYNOPSIS
                    281:
                    282:     use Text::Todo::REST::API;
                    283:
                    284: =for author to fill in:
                    285:     Brief code example(s) here showing commonest usage(s).
                    286:     This section will be as far as many users bother reading
                    287:     so make it as educational and exeplary as possible.
                    288:
                    289:
                    290: =head1 DESCRIPTION
                    291:
                    292: =for author to fill in:
                    293:     Write a full description of the module and its features here.
                    294:     Use subsections (=head2, =head3) as appropriate.
                    295:
                    296:
                    297: =head1 INTERFACE
                    298:
                    299: =for author to fill in:
                    300:     Write a separate section listing the public components of the modules
                    301:     interface. These normally consist of either subroutines that may be
                    302:     exported, or methods that may be called on objects belonging to the
                    303:     classes provided by the module.
                    304:
                    305:
                    306: =head1 DIAGNOSTICS
                    307:
                    308: =for author to fill in:
                    309:     List every single error and warning message that the module can
                    310:     generate (even the ones that will "never happen"), with a full
                    311:     explanation of each problem, one or more likely causes, and any
                    312:     suggested remedies.
                    313:
                    314: =over
                    315:
                    316: =item C<< Error message here, perhaps with %s placeholders >>
                    317:
                    318: [Description of error here]
                    319:
                    320: =item C<< Another error message here >>
                    321:
                    322: [Description of error here]
                    323:
                    324: [Et cetera, et cetera]
                    325:
                    326: =back
                    327:
                    328:
                    329: =head1 CONFIGURATION AND ENVIRONMENT
                    330:
                    331: =for author to fill in:
                    332:     A full explanation of any configuration system(s) used by the
                    333:     module, including the names and locations of any configuration
                    334:     files, and the meaning of any environment variables or properties
                    335:     that can be set. These descriptions must also include details of any
                    336:     configuration language used.
                    337:
                    338: Text::Todo::REST::API requires no configuration files or environment variables.
                    339:
                    340:
                    341: =head1 DEPENDENCIES
                    342:
                    343: =for author to fill in:
                    344:     A list of all the other modules that this module relies upon,
                    345:     including any restrictions on versions, and an indication whether
                    346:     the module is part of the standard Perl distribution, part of the
                    347:     module's distribution, or must be installed separately. ]
                    348:
                    349: None.
                    350:
                    351:
                    352: =head1 INCOMPATIBILITIES
                    353:
                    354: =for author to fill in:
                    355:     A list of any modules that this module cannot be used in conjunction
                    356:     with. This may be due to name conflicts in the interface, or
                    357:     competition for system or program resources, or due to internal
                    358:     limitations of Perl (for example, many modules that use source code
                    359:     filters are mutually incompatible).
                    360:
                    361: None reported.
                    362:
                    363:
                    364: =head1 BUGS AND LIMITATIONS
                    365:
                    366: =for author to fill in:
                    367:     A list of known problems with the module, together with some
                    368:     indication Whether they are likely to be fixed in an upcoming
                    369:     release. Also a list of restrictions on the features the module
                    370:     does provide: data types that cannot be handled, performance issues
                    371:     and the circumstances in which they may arise, practical
                    372:     limitations on the size of data sets, special cases that are not
                    373:     (yet) handled, etc.
                    374:
                    375: No bugs have been reported.
                    376:
                    377: Please report any bugs or feature requests to
                    378: C<bug-text-todo-rest-api@rt.cpan.org>, or through the web interface at
                    379: L<http://rt.cpan.org>.
                    380:
                    381:
                    382: =head1 AUTHOR
                    383:
                    384: Andrew Fresh  C<< <andrew@cpan.org> >>
                    385:
                    386:
                    387: =head1 LICENSE AND COPYRIGHT
                    388:
                    389: Copyright (c) 2010, Andrew Fresh C<< <andrew@cpan.org> >>. All rights reserved.
                    390:
                    391: This module is free software; you can redistribute it and/or
                    392: modify it under the same terms as Perl itself. See L<perlartistic>.
                    393:
                    394:
                    395: =head1 DISCLAIMER OF WARRANTY
                    396:
                    397: BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
                    398: FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
                    399: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
                    400: PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
                    401: EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                    402: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
                    403: ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
                    404: YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
                    405: NECESSARY SERVICING, REPAIR, OR CORRECTION.
                    406:
                    407: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
                    408: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
                    409: REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
                    410: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
                    411: OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
                    412: THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
                    413: RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
                    414: FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
                    415: SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
                    416: SUCH DAMAGES.

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