Schedule/Vcal.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Schedule/Post/Simple.pm ↑ Summary
1 package LJ::Schedule::Vcal;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Data::Ical;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use Data::Dumper;
11  
12 our $SECS_IN_DAY = 60 * 60 * 24;
13 our $SECS_IN_WEEK = 60 * 60 * 24 * 7;
14 our @DAY_NAMES = qw(Sun Mon Tue Wed Thu Fri Sat);
15  
16 =head1 NAME
17  
18 LJ::Schedule::Vcal - The default calendar module for LJ::Schedule
19  
20 =head1 VERSION
21  
22 Version 0.6
23  
24 =cut
25  
26 our $VERSION = '0.6';
27  
28 =head1 SYNOPSIS
29  
30 This module is used internally by LJ::Schedule, and shouldn't need to be
31 used directly.
32  
33 =head1 AUTHOR
34  
35 Ben Evans, C<< <ben at bpfh.net> >>
36  
37 =head1 BUGS
38  
39 Please report any bugs or feature requests to
40 C<bug-lj-schedule-vcal at rt.cpan.org>, or through the web interface at
41 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LJ-Schedule>.
42 I will be notified, and then you'll automatically be notified of progress on
43 your bug as I make changes.
44  
45 =head1 SUPPORT
46  
47 You can find documentation for this module with the perldoc command.
48  
49 perldoc LJ::Schedule
50  
51 You can also look for information at:
52  
53 =over 4
54  
55 =item * AnnoCPAN: Annotated CPAN documentation
56  
57 L<http://annocpan.org/dist/LJ-Schedule>
58  
59 =item * CPAN Ratings
60  
61 L<http://cpanratings.perl.org/d/LJ-Schedule>
62  
63 =item * RT: CPAN's request tracker
64  
65 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LJ-Schedule>
66  
67 =item * Search CPAN
68  
69 L<http://search.cpan.org/dist/LJ-Schedule>
70  
71 =back
72  
73 =head1 ACKNOWLEDGEMENTS
74  
75 =head1 COPYRIGHT & LICENSE
76  
77 Copyright 2006 Ben Evans, all rights reserved.
78  
79 This program is free software; you can redistribute it and/or modify it
80 under the same terms as Perl itself.
81  
82 =cut
83  
84 #
85 # Default constructor
86 #
87 sub new {
88 my ($pkg, $params) = @_;
89 my $self = {};
90  
91 $self = $params if (ref($params) eq 'HASH');
92 bless $self, $pkg;
93  
94 $self->{cal} = Data::ICal->new(filename => $self->{filename});
95  
96 #print $cal->as_string();
97 # print Dumper $cal;
98  
99 $self->{ent} = $self->{cal}->entries();
100 $self->{evts} = [];
101  
102 return $self;
103 }
104  
105 # Helper method which returns the events (evts)
106 sub evts { return (shift)->{evts}; }
107  
108 #
109 # Loops over the events in the Vcal file, processing each one in turn
110 #
111 sub prep_cal_for_lj {
112 my $self = shift;
113  
114 my $ra_ent = $self->{ent};
115 my $ra_evts = $self->{evts};
116  
117 EVENT: foreach my $ent (@$ra_ent) {
118 #print Dumper $ent;
119  
120 my $ra = $self->process_event($ent);
121 push @$ra_evts, @$ra;
122 }
123  
124 @$ra_evts = sort { $a->{tval} <=> $b->{tval} } @$ra_evts;
125  
126 # return 0;
127 }
128  
129 #
130 # Returns 1 if the event is a single-day event, 0 otherwise
131 #
132 sub is_single_day {
133 my $self = shift;
134 my $ent = shift;
135  
136 my $start = $self->get_tval($ent);
137 my $end = $self->get_tval($ent, 'dtend');
138  
139 # All-day events have no end date defined
140 return 1 if !defined $end;
141  
142 my $diff = $end - $start;
143  
144 # print "Start: $start ; End: $end ; Diff: $diff\n";
145  
146 # It seems as though the Treo translates multi-day events to
147 # several single day events before export.
148 #
149 # Nonetheless, this code is here in case that ever changes.
150 #
151 return 1 if $diff < $SECS_IN_DAY;
152  
153 # print STDERR Dumper $ent;
154  
155 return 0;
156 }
157  
158 #
159 # Gets the tval in seconds-since-epoch for a given event
160 #
161 sub get_tval {
162 my $self = shift;
163 my $ent = shift;
164 my $t = shift || "dtstart";
165  
166 my $rh_props = $ent->properties();
167 my $ra_prop_start = $rh_props->{$t};
168  
169 my $value;
170 PROP: foreach my $start (@$ra_prop_start) {
171 my $key = $start->key();
172 if (lc($key) eq $t) {
173 $value = $start->value();
174 last PROP;
175 }
176 }
177  
178 my $date = str2time($value);
179  
180 return $date;
181 }
182  
183 #
184 # Returns an arrayref of hashrefs of event details for a given future
185 # event. Returns [] for a past event.
186 #
187 sub process_event {
188 my $self = shift;
189 my $ent = shift;
190  
191 my $rh_props = $ent->properties();
192  
193 if ($self->is_single_day($ent)) {
194 my $tval = $self->get_tval($ent);
195  
196 my $now = time();
197  
198 return [] if $tval < $now;
199 my $date = time2str($LJ::Schedule::DATE_FMT, $tval);
200  
201 # print STDERR "Date: $date ; Tval: $tval ; Now: $now ; ";
202  
203 my $ra_prop_summ = $rh_props->{summary};
204  
205 my $value;
206 SUMMARY: foreach my $summ (@$ra_prop_summ) {
207 my $key = $summ->key();
208 if (lc($key) eq "summary") {
209 $value = $summ->value();
210 last SUMMARY;
211 }
212 }
213 # print "$value\n";
214 my $rh = {tval => $tval, summary => $value, date => $date};
215  
216 return [$rh];
217 } else {
218 }
219  
220 }
221  
222  
223 1; # End of LJ::Schedule::Vcal
« Schedule/Post/Simple.pm ↑ Summary