File Coverage

blib/lib/OurCal/Provider/ICalendar.pm
Criterion Covered Total %
statement 30 90 33.3
branch 0 20 0.0
condition n/a
subroutine 10 22 45.4
pod 9 9 100.0
total 49 141 34.7


line stmt bran cond sub pod time code
1             package OurCal::Provider::ICalendar;
2              
3 1     1   1548 use strict;
  1         28  
  1         60  
4 1     1   1244 use LWP::Simple;
  1         161171  
  1         12  
5 1     1   1689 use Data::ICal::DateTime;
  1         131037  
  1         9  
6 1     1   148 use DateTime;
  1         3  
  1         5  
7 1     1   22 use DateTime::Span;
  1         2  
  1         5  
8 1     1   18 use File::Spec;
  1         2  
  1         9  
9 1     1   18 use OurCal::Event;
  1         2  
  1         9  
10 1     1   20 use OurCal::Provider;
  1         1  
  1         7  
11 1     1   21 use Carp qw(cluck);
  1         1  
  1         49  
12 1     1   5 use Digest::MD5 qw(md5_hex);
  1         1  
  1         870  
13              
14              
15             =head1 NAME
16              
17             OurCal::Provider::ICalendar - an RFC2445 provider for OurCal
18              
19             =head1 SYNOPSIS
20              
21             [an_ical_provider
22             type = icalendar
23             file = path/to/file.ics
24              
25             =head1 CONFIG OPTIONS
26              
27             =over 4
28              
29             =item file
30              
31             The ics file. Can either be a local file or a url of one.
32              
33             =item cache
34              
35             The name of a cache provider. This will cache fetching the file.
36              
37             =back
38              
39             =head1 METHODS
40              
41             =cut
42              
43             =head2 new
44              
45             Requires an C object as config param and a name param.
46              
47             =cut
48              
49              
50             sub new {
51 0     0 1   my $class = shift;
52 0           my %what = @_;
53 0           my $conf = $what{config}->config($what{name});
54 0           my $file = $conf->{file};
55 0 0         if ($file !~ m!^http://!) {
56 0           $file = File::Spec->rel2abs($file);
57 0           $file = "file://$file";
58             }
59 0           $what{file} = $file;
60 0 0         if ($conf->{cache}) {
61 0           $what{_cache} = OurCal::Provider->load_provider($conf->{cache}, $what{config});
62             }
63              
64 0           return bless \%what, $class;
65             }
66              
67             sub users {
68 0     0 1   return ();
69             }
70              
71             sub todos {
72 0     0 1   return ();
73             }
74              
75             sub events {
76 0     0 1   my $self = shift;
77 0           my %opts = @_;
78 0           my $data = $self->_fetch_data;
79 0 0         return () unless $data;
80 0           my $cal = Data::ICal->new( data => $data );
81 0 0         return () unless $cal;
82 0           my @vals;
83 0 0         if (defined $opts{date}) {
84 0           my @names = qw(year month day);
85 0           my @bits = split /-/, $opts{date};
86 0           my %conf;
87 0           foreach my $bit (@bits) {
88 0           my $name = shift @names;
89 0 0         last unless $name;
90 0           $conf{$name} = $bit;
91             }
92 0           my $s = DateTime->new(%conf)->truncate( to => 'day');
93 0           my $e = $s->clone->add( days => 1)->subtract( seconds => 1 );
94 0           my $span = DateTime::Span->from_datetimes( start => $s, end => $e );
95 0           push @vals, $span, 'day';
96             }
97            
98 0           my @events = $cal->events(@vals);
99 0 0         @events = map { $_->explode(@vals) } @events if @vals;
  0            
100 0           @events = sort { $a->start->epoch <=> $b->start->epoch } @events;
  0            
101 0 0         @events = splice @events, 0, $opts{limit} if defined $opts{limit};
102 0           return map { $self->_to_event($_) } @events;
  0            
103             }
104              
105              
106             sub _fetch_data {
107 0     0     my $self = shift;
108 0 0         return get($self->{file}) unless defined $self->{_cache};
109 0           my $file = $self->{name}."@".md5_hex($self->{file});
110 0     0     return ($self->{_cache}->cache( $file, sub { get($self->{file}) }))[0];
  0            
111             }
112              
113             sub _to_event {
114 0     0     my $self = shift;
115 0           my $event = shift;
116 0           my %what;
117 0           $what{id} = $event->property('id');
118 0           $what{date} = $event->start->strftime("%Y-%m-%d");
119 0           $what{description} = $event->summary;
120 0 0         $what{recurring} = $event->property('rrule') or $event->property('rdate');
121 0           $what{editable} = 0;
122 0           return OurCal::Event->new(%what);
123             }
124              
125             sub has_events {
126 0     0 1   my $self = shift;
127 0           return scalar($self->events(@_));
128             }
129              
130             sub save_event {
131 0     0 1   return 0;
132             }
133              
134             sub del_event {
135 0     0 1   return 0;
136             }
137              
138             sub save_todo {
139 0     0 1   return 0;
140             }
141              
142             sub del_todo {
143 0     0 1   return 0;
144             }
145              
146              
147             =head2 todos
148              
149             Returns all the todos on the system.
150              
151             =head2 has_events
152              
153             Returns whether there are events given the params.
154              
155             =head2 events
156              
157             Returns all the events for the given params.
158              
159             =head2 users
160              
161             Returns the name of all the users on the system.
162              
163             =head2 save_todo
164              
165             Save a todo.
166              
167             =head2 del_todo
168              
169             Delete a todo.
170              
171              
172             =head2 save_event
173              
174             Save an event.
175              
176             =head2 del_event
177              
178             Delete an event..
179              
180             =cut
181              
182              
183              
184             1;
185