File Coverage

blib/lib/DateTime/TimeZone/ICal/Parsing.pm
Criterion Covered Total %
statement 30 41 73.1
branch 0 8 0.0
condition 0 2 0.0
subroutine 10 11 90.9
pod 1 1 100.0
total 41 63 65.0


line stmt bran cond sub pod time code
1             package DateTime::TimeZone::ICal::Parsing;
2              
3 2     2   1164 use 5.010;
  2         4  
  2         66  
4 2     2   13 use strict;
  2         3  
  2         61  
5 2     2   7 use warnings FATAL => 'all';
  2         3  
  2         71  
6              
7 2     2   6 use Moo::Role;
  2         3  
  2         10  
8 2     2   530 use namespace::autoclean;
  2         3  
  2         34  
9              
10 2     2   901 use DateTime;
  2         91509  
  2         61  
11 2     2   1042 use DateTime::Span;
  2         70525  
  2         79  
12 2     2   22 use DateTime::Set;
  2         4  
  2         46  
13 2     2   1705 use DateTime::Format::ICal;
  2         28776  
  2         69  
14 2     2   1314 use URI;
  2         7421  
  2         1102  
15              
16             =head1 NAME
17              
18             DateTime::TimeZone::ICal::Parsing - Role for iCal parsing operations
19              
20             =head1 VERSION
21              
22             Version 0.01
23              
24             =cut
25              
26             our $VERSION = '0.01';
27              
28             # this is the subset of relevant properties and their default types
29             my %PROPS = (
30             COMMENT => 'TEXT',
31             DTSTART => 'DATE-TIME',
32             TZID => 'TEXT',
33             TZNAME => 'TEXT',
34             TZOFFSETFROM => 'UTC-OFFSET',
35             TZOFFSETTO => 'UTC-OFFSET',
36             TZURL => 'URI',
37             RDATE => 'DATE-TIME',
38             RRULE => 'RECUR',
39             'LAST-MODIFIED' => 'DATE-TIME',
40             );
41              
42             # these are different from above
43             my %TYPES = (
44             DATE => sub { DateTime::Format::ICal->parse_datetime($_[1]) },
45             'DATE-TIME' => sub { DateTime::Format::ICal->parse_datetime($_[1]) },
46             DURATION => sub { DateTime::Format::ICal->parse_duration($_[1]) },
47             FLOAT => sub {
48             my ($x) = ($_[1] =~ /^\s*([+-]?\d+(?:\.\d+))\s*$/); $x },
49             INTEGER => sub {
50             my ($x) = ($_[1] =~ /^\s*([+-]?\d+)\s*$/); $x },
51             PERIOD => sub {
52             my ($start, $end) = split m!\s*/+\s*!, $_[1];
53             $start = DateTime::Format::ICal->parse_datetime($start);
54             if ($end =~ /^[Pp]/) {
55             $end = DateTime::Format::ICal->parse_duration($end);
56             $end = $start + $end;
57             }
58             else {
59             $end = DateTime::Format::ICal->parse_datetime($end);
60             }
61              
62             DateTime::Span->from_datetimes(start => $start, end => $end);
63             },
64             RECUR => sub { $_[1] }, # leave this for now
65             TEXT => sub { $_[1] },
66             URI => sub { URI->new($_[1]) },
67             'UTC-OFFSET' => sub { my ($x) = ($_[1] =~ /^\s*([+-]?\d{4})\s*/); $x },
68             );
69              
70             =head1 SYNOPSIS
71              
72             with 'DateTime::TimeZone::ICal::Parsing';
73              
74             # ...
75              
76             =head1 METHODS
77              
78             =head2 parse_val
79              
80             Parses a property value and returns a more interesting and usable
81             datatype.
82              
83             =cut
84              
85             sub parse_val {
86 0     0 1   my ($self, $prop) = @_;
87 0 0         return unless $prop;
88              
89 0           my $val = $prop->value;
90 0 0         return unless defined $prop->value;
91              
92 0           my $name = $prop->key;
93 0   0       my $type = $prop->parameters->{VALUE} || $PROPS{uc $name} || 'TEXT';
94              
95 0           $val = eval { $TYPES{$type}->($self, $val) };
  0            
96 0 0         warn "$name $type $@" if $@;
97 0 0         return if $@;
98              
99 0           $val;
100             }
101              
102             =head1 AUTHOR
103              
104             Dorian Taylor, C<< >>
105              
106             =head1 BUGS
107              
108             Please report any bugs or feature requests to
109             C, or through the web
110             interface at
111             L.
112             I will be notified, and then you'll automatically be notified of
113             progress on your bug as I make changes.
114              
115             =head1 SUPPORT
116              
117             You can find documentation for this module with the perldoc command.
118              
119             perldoc DateTime::TimeZone::ICal::Parsing
120              
121             You can also look for information at:
122              
123             =over 4
124              
125             =item * RT: CPAN's request tracker (report bugs here)
126              
127             L
128              
129             =item * AnnoCPAN: Annotated CPAN documentation
130              
131             L
132              
133             =item * CPAN Ratings
134              
135             L
136              
137             =item * Search CPAN
138              
139             L
140              
141             =back
142              
143             =head1 LICENSE AND COPYRIGHT
144              
145             Copyright 2015 Dorian Taylor.
146              
147             Licensed under the Apache License, Version 2.0 (the "License"); you
148             may not use this file except in compliance with the License. You may
149             obtain a copy of the License at
150             L .
151              
152             Unless required by applicable law or agreed to in writing, software
153             distributed under the License is distributed on an "AS IS" BASIS,
154             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
155             implied. See the License for the specific language governing
156             permissions and limitations under the License.
157              
158             =cut
159              
160             1; # End of DateTime::TimeZone::ICal::Parsing