File Coverage

blib/lib/CalDAV/Simple/Task.pm
Criterion Covered Total %
statement 12 25 48.0
branch 0 4 0.0
condition 0 3 0.0
subroutine 4 9 44.4
pod 0 1 0.0
total 16 42 38.1


line stmt bran cond sub pod time code
1             package CalDAV::Simple::Task;
2             $CalDAV::Simple::Task::VERSION = '0.01';
3 1     1   14 use 5.006;
  1         3  
  1         31  
4 1     1   4 use Moo 1.006;
  1         10  
  1         4  
5 1     1   197 use Carp qw/ croak /;
  1         2  
  1         40  
6 1     1   846 use DateTime;
  1         107713  
  1         775  
7              
8             has vcal_string => (is => 'ro');
9             has due => (is => 'lazy');
10              
11             my $extract_field = sub {
12             my ($self, $field) = @_;
13              
14             if ($self->vcal_string =~ m!^${field}:(.*?)$!ms) {
15             return $1;
16             }
17             else {
18             croak "failed to get '$field' field out of CalDAV response (", $self->vcal_string, ")\n";
19             }
20              
21             };
22              
23             foreach my $attribute (qw/ created summary status uid /) {
24             has $attribute => (
25             is => 'lazy',
26             builder => sub {
27 0     0     my $self = shift;
28 0           return $self->$extract_field(uc($attribute)) },
29             );
30             }
31              
32             my $extract_xml_element = sub {
33             my ($self, $tag) = @_;
34              
35             if ($self->vcal_string =~ m!<$tag>(.*?)!ms) {
36             return $1;
37             }
38             else {
39             croak "failed to get <$tag> element out of CalDAV response (", $self->vcal_string, ")\n";
40             }
41              
42             };
43              
44             has etag => (is => 'lazy',
45             builder => sub {
46 0     0     my $self = shift;
47 0           return $self->$extract_xml_element('d:getetag');
48             });
49              
50             has href => (is => 'lazy',
51             builder => sub {
52 0     0     my $self = shift;
53 0           return $self->$extract_xml_element('d:href');
54             });
55              
56              
57             sub BUILD
58             {
59 0     0 0   my $self = shift;
60              
61             # print STDERR $self->vcal_string, "\n";
62             }
63              
64             sub _build_due
65             {
66 0     0     my $self = shift;
67              
68             # The 'DUE' field can take two formats, either zulu time or with a timezone
69             # DUE;TZID=Europe/London:20150519T213000
70             # DUE:20150513T200129Z
71 0 0         if ($self->vcal_string =~ m!^DUE(;TZID=(.*?))?:(\d{4})(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)(Z?)$!ms) {
72 0           my ($dummy, $tz, $year, $month, $day, $hour, $min, $sec, $zulu) = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
73 0 0 0       $tz = 'UTC' if !defined($tz) && $zulu eq 'Z';
74 0           return DateTime->new(year => $year, month => $month, day => $day,
75             hour => $hour, minute => $min, second => $sec,
76             time_zone => $tz);
77             }
78             else {
79 0           croak "failed to get 'DUE' field out of VTODO string\n";
80             }
81             }
82              
83             1;
84              
85             =head1 NAME
86              
87             CalDAV::Simple::Task - a data class representing one task (VTODO) in a CalCAV calendar
88              
89             =head1 SYNOPSIS
90              
91             use CalDAV::Simple::Task;
92             my $task = CalDAV::Simple::Task->new(vcal_string => $string);
93             printf "task '%s' is due '%s'\n", $task->summary, $task->due;
94              
95             =head1 DESCRIPTION
96              
97             This module is used to hold details of a single task from a CalDAV calendar.
98             It is alpha quality code. I don't really know much about CalDAV, but I've
99             been hacking around until I could get what I wanted working.
100              
101             =head1 METHODS
102              
103             =head2 summary
104              
105             The short description / title of the task.
106              
107             =head2 status
108              
109             The CalDAV STATUS string for the task. I haven't looked into the different
110             values this can take.
111              
112             =head2 uid
113              
114             The CalDAV UID for the task.
115              
116             =head2 etag
117              
118             The L for the task.
119              
120             =head2 href
121              
122             The relative URL for the task.
123              
124             =head2 due
125              
126             A L instance holding the due date for the task.
127              
128             =head2 created
129              
130             When the task was created. This will currently be returned as an ISO 8601 date+time
131             string, I think. In the future I'll make this return a L instance as well.
132              
133             =head2 vcal_string
134              
135             This is the string returned from the CalDAV server for a single task.
136             It's basically the C element:
137              
138            
139             ...
140            
141             BEGIN:VCALENDAR
142             ...
143             END:VCALENDAR
144            
145             ...
146            
147              
148             Hopefully you won't have to deal with this.
149              
150             =head2 delete_task
151              
152             Takes a task (instance of L) and deletes it
153             from the calendar.
154              
155             =head1 SEE ALSO
156              
157             L - the main module of this distribution, the C
158             method of which returns instances of C.
159              
160             =head1 REPOSITORY
161              
162             L
163              
164             =head1 AUTHOR
165              
166             Neil Bowers Eneilb@cpan.orgE
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             This software is copyright (c) 2015 by Neil Bowers .
171              
172             This is free software; you can redistribute it and/or modify it under
173             the same terms as the Perl 5 programming language system itself.
174              
175             =cut
176              
177