OurCal/Provider/ICalendar.pm

filter: Statementstmt 34.0% 31 / 91 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 45.4% 10 / 22 Podpod 100% 9 / 9 totaltotal 35.2% 50 / 142 CC 12 SCAR 30.0
CC
12
Cov
28%
events
72.0
new
12.0
_fetch_data
6.0
CRAP 20.1
« OurCal/View/HTML.pm ↑ Summary OurCal/Provider/DBI.pm »
1 package OurCal::Provider::ICalendar;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Data::ICal::DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DateTime::Span;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use OurCal::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use OurCal::Provider;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 <param[s]>
44  
45 Requires an C<OurCal::Config> object as config param and a name param.
46  
47 =cut
48  
49  
50 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
51 0 my $class = shift;
52 0 my %what = @_;
53 0 my $conf = $what{config}->config($what{name});
54 0 my $file = $conf->{file};
55 0 ▶ if ($file !~ m!^http://!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file =~ m[^http://]) 0 0 0
56 0 $file = File::Spec->rel2abs($file);
57 0 $file = "file://$file";
58 }
59 0 $what{file} = $file;
60 0 ▶ if ($conf->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"cache"}) 0 0 0
61 0 $what{_cache} = OurCal::Provider->load_provider($conf->{cache}, $what{config});
62 }
63  
64 0 return bless \%what, $class;
65 }
66  
67 0 ▶sub users {
Subroutine users: not called CC=1 SCAR=6.9
Pod documented
68 0 return ();
69 }
70  
71 0 ▶sub todos {
Subroutine todos: not called CC=1 SCAR=6.9
Pod documented
72 0 return ();
73 }
74  
75 0 ▶sub events {
Subroutine events: not called CC=8 SCAR=42.8
Pod documented
76 0 my $self = shift;
77 0 my %opts = @_;
78 0 my $data = $self->_fetch_data;
79 0 ▶ return () unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
80 0 my $cal = Data::ICal->new( data => $data );
81 0 ▶ return () unless $cal;
Branch: 1 branch
BranchTrueFalseTotal
unless $cal 0 0 0
82 0 my @vals;
83 0 ▶ if (defined $opts{date}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"date"}) 0 0 0
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 ▶ last unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
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 ▶ @events = map { $_->explode(@vals) } @events if @vals;
Branch: 1 branch
BranchTrueFalseTotal
if @vals 0 0 0
100 0 @events = sort { $a->start->epoch <=> $b->start->epoch } @events;
101 0 ▶ @events = splice @events, 0, $opts{limit} if defined $opts{limit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"limit"} 0 0 0
102 0 return map { $self->_to_event($_) } @events;
103 }
104  
105  
106 0 ▶sub _fetch_data {
Subroutine _fetch_data: not called CC=2 SCAR=17.9
107 0 my $self = shift;
108 0 ▶ return get($self->{file}) unless defined $self->{_cache};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_cache"} 0 0 0
109 0 my $file = $self->{name}."@".md5_hex($self->{file});
110 0 ▶ return ($self->{_cache}->cache( $file, sub { get($self->{file}) }))[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 }
112  
113 0 ▶sub _to_event {
Subroutine _to_event: not called CC=2 SCAR=17.9
114 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 ▶ $what{recurring} = $event->property('rrule') or $event->property('rdate');
Branch: 1 branch
BranchTrueFalseTotal
$what{"recurring"} = $event->property("rrule") or $event->property("rdate") 0 0 0
121 0 $what{editable} = 0;
122 0 return OurCal::Event->new(%what);
123 }
124  
125 0 ▶sub has_events {
Subroutine has_events: not called CC=1 SCAR=6.9
Pod documented
126 0 my $self = shift;
127 0 return scalar($self->events(@_));
128 }
129  
130 0 ▶sub save_event {
Subroutine save_event: not called CC=1 SCAR=6.9
Pod documented
131 0 return 0;
132 }
133  
134 0 ▶sub del_event {
Subroutine del_event: not called CC=1 SCAR=6.9
Pod documented
135 0 return 0;
136 }
137  
138 0 ▶sub save_todo {
Subroutine save_todo: not called CC=1 SCAR=6.9
Pod documented
139 0 return 0;
140 }
141  
142 0 ▶sub del_todo {
Subroutine del_todo: not called CC=1 SCAR=6.9
Pod documented
143 0 return 0;
144 }
145  
146  
147 =head2 todos
148  
149 Returns all the todos on the system.
150  
151 =head2 has_events <param[s]>
152  
153 Returns whether there are events given the params.
154  
155 =head2 events <param[s]>
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 <OurCal::Todo>
164  
165 Save a todo.
166  
167 =head2 del_todo <OurCal::Todo>
168  
169 Delete a todo.
170  
171  
172 =head2 save_event <OurCal::Event>
173  
174 Save an event.
175  
176 =head2 del_event <OurCal::Event>
177  
178 Delete an event..
179  
180 =cut
181  
182  
183  
184 2 1;
185  
« OurCal/View/HTML.pm ↑ Summary OurCal/Provider/DBI.pm »