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

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

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