File Coverage

blib/lib/App/todoist.pm
Criterion Covered Total %
statement 21 60 35.0
branch 0 20 0.0
condition 0 12 0.0
subroutine 7 12 58.3
pod 0 5 0.0
total 28 109 25.6


line stmt bran cond sub pod time code
1             package App::todoist;
2             $App::todoist::VERSION = '0.01';
3 1     1   645 use 5.006;
  1         3  
  1         36  
4 1     1   4 use strict;
  1         1  
  1         33  
5 1     1   13 use warnings;
  1         2  
  1         27  
6 1     1   555 use Net::Todoist;
  1         42918  
  1         33  
7 1     1   8 use Carp qw/ croak /;
  1         1  
  1         47  
8 1     1   531 use AppConfig::Std;
  1         7245  
  1         46  
9 1     1   527 use File::Slurp qw/ read_file /;
  1         9748  
  1         525  
10              
11             sub new
12             {
13 0     0 0   my $class = shift;
14 0   0       my $obj = bless({}, $class)
15             || croak "Can't instantiate App::todoist\n";
16              
17 0           return $obj;
18             }
19              
20             sub process_options
21             {
22 0     0 0   my ($self, $opts) = @_;
23              
24 0   0       my $config = AppConfig::Std->new()
25             || croak "Can't instantiate AppConfig::Std\n";
26              
27 0           $config->define('token', { ARGCOUNT => 1 });
28 0           $config->define('project', { ARGCOUNT => 1 });
29 0           $config->define('importfile', { ARGCOUNT => 1, ALIAS => 'i' });
30 0           $config->define('priority', { ARGCOUNT => 1, ALIAS => 'p', DEFAULT => 4 });
31              
32 0 0 0       if (defined($ENV{HOME}) && -f "$ENV{HOME}/.todoist") {
33 0           my $filename = "$ENV{HOME}/.todoist";
34 0 0         if (((stat($filename))[2] & 36) != 0) {
35 0           croak "your config file ($filename) is readable by others!\n";
36             }
37 0 0         $config->file($filename) || exit 1;
38             }
39              
40 0 0         if ($opts->{argv}) {
41 0 0         $config->args($opts->{argv})
42             || die "run \"$0 -help\" to see valid options\n";
43             }
44              
45 0 0         croak "you must provide a token\n" unless $config->token;
46 0 0         croak "you must provide a project\n" unless $config->project;
47              
48 0           $self->{config} = $config;
49              
50 0   0       my $todoist = Net::Todoist->new(token => $config->token)
51             || croak "failed to connect to todoist\n";
52              
53 0           $self->{todoist} = $todoist;
54              
55 0 0         if ($config->project) {
56 0           my @projects = $todoist->getProjects;
57 0           my ($project) = grep { $_->{name} eq $config->project } @projects;
  0            
58 0 0         if (not defined $project) {
59 0           croak "couldn't find project '", $config->project, "' in todoist\n"
60             }
61 0           $self->{project_id} = $project->{id};
62             }
63             }
64              
65             sub config
66             {
67 0     0 0   my $self = shift;
68 0           return $self->{config};
69             }
70              
71             sub todoist
72             {
73 0     0 0   my $self = shift;
74 0           return $self->{todoist};
75             }
76              
77             sub run
78             {
79 0     0 0   my ($self, $opts) = @_;
80              
81 0           $self->process_options($opts);
82              
83 0 0         if ($self->config->importfile) {
84 0           my @tasks = read_file($self->config->importfile, chomp => 1);
85 0           foreach my $task (@tasks) {
86 0           $self->todoist->addItem(
87             project_id => $self->{project_id},
88             content => $task,
89             priority => $self->config->priority,
90             );
91             }
92 0           printf STDERR "%d tasks added from %s\n", int(@tasks),
93             $self->config->importfile;
94             }
95             }
96              
97             1;
98              
99             =head1 NAME
100              
101             App::todoist - implements the meat of a command-line interface to todoist.com
102              
103             =head1 SYNOPSIS
104              
105             Add something here
106              
107             =head1 DESCRIPTION
108              
109             This module implements the functionality behind the C script.
110             You should look at the documentation for that.
111              
112             =head1 REPOSITORY
113              
114             L
115              
116             =head1 AUTHOR
117              
118             Neil Bowers Eneilb@cpan.orgE
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is copyright (c) 2013 by Neil Bowers .
123              
124             This is free software; you can redistribute it and/or modify it under
125             the same terms as the Perl 5 programming language system itself.
126              
127             =cut
128