blib/lib/Mojolicious/Plugin/Ical.pm

filter: Statementstmt 100% 88 / 88 Branchbran 62.5% 5 / 8 Conditioncond 40.9% 25 / 61 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 63.5% 131 / 206 CC 33 SCAR 24.4
CC
33
Cov
75%
register
17.0
_render_ical
16.4
_event_to_properties
9.7
CRAP 11.5
↑ Summary
1 package Mojolicious::Plugin::Ical;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Text::vFile::asData;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.00';
9  
10 2 ▶sub register {
Subroutine register: called CC=12 SCAR=28.3
Pod documented
11 2 my ($self, $app, $config) = @_;
12  
13 2 ▶ $config->{handler} //= 'ical';
Condition50%1/2
$config->{"handler"} //= "ical"
l!l
02
MC/DC0%0/1
$config->{"handler"} //= "ical"
$config->{"handler"}
Truth table50%1/2
A
A= $config->{"handler"}
Aresult
11
00
14  
15 2 ▶ $self->{properties} = $config->{properties} || {};
Condition50%1/2
$config->{'properties'} || {}
l!l
20
MC/DC0%0/1
$config->{'properties'} || {}
$config->{'properties'}
Truth table50%1/2
A
A= $config->{'properties'}
Aresult
11
00
16 2 ▶ $self->{properties}{calscale} ||= 'GREGORIAN';
Condition50%1/2
$self->{"properties"}{"calscale"} ||= "GREGORIAN"
l!l
02
MC/DC0%0/1
$self->{"properties"}{"calscale"} ||= "GREGORIAN"
$self->{"properties"}{"calscale"}
Truth table50%1/2
A
A= $self->{"properties"}{"calscale"}
Aresult
11
00
17 2 ▶ $self->{properties}{method} ||= 'PUBLISH';
Condition50%1/2
$self->{"properties"}{"method"} ||= "PUBLISH"
l!l
02
MC/DC0%0/1
$self->{"properties"}{"method"} ||= "PUBLISH"
$self->{"properties"}{"method"}
Truth table50%1/2
A
A= $self->{"properties"}{"method"}
Aresult
11
00
18 2 ▶ $self->{properties}{prodid} ||= sprintf '-//%s//NONSGML %s//EN', Sys::Hostname::hostname, $app->moniker;
Condition33%1/3
$self->{"properties"}{"prodid"} ||= sprintf("-//%s//NONSGML %s//EN", Sys::Hostname::hostname(), $app->moniker)
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"properties"}{"prodid"} ||= sprintf("-//%s//NONSGML %s//EN", Sys::Hostname::hostname(), $app->moniker)
$self->{"properties"}{"prodid"} sprintf("-//%s//NONSGML %s//EN", Sys::Hostname::hostname(), $app->moniker)
Truth table33%1/3
A ||= B
A= $self->{"properties"}{"prodid"}B= sprintf("-//%s//NONSGML %s//EN", Sys::Hostname::hostname(), $app->moniker)
ABresult
1X1
011
000
19 2 ▶ $self->{properties}{version} ||= '2.0';
Condition50%1/2
$self->{"properties"}{"version"} ||= "2.0"
l!l
02
MC/DC0%0/1
$self->{"properties"}{"version"} ||= "2.0"
$self->{"properties"}{"version"}
Truth table50%1/2
A
A= $self->{"properties"}{"version"}
Aresult
11
00
20 2 ▶ $self->{properties}{x_wr_caldesc} ||= '';
Condition50%1/2
$self->{"properties"}{"x_wr_caldesc"} ||= ""
l!l
20
MC/DC0%0/1
$self->{"properties"}{"x_wr_caldesc"} ||= ""
$self->{"properties"}{"x_wr_caldesc"}
Truth table50%1/2
A
A= $self->{"properties"}{"x_wr_caldesc"}
Aresult
11
00
21 2 ▶ $self->{properties}{x_wr_calname} ||= $app->moniker;
Condition33%1/3
$self->{"properties"}{"x_wr_calname"} ||= $app->moniker
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"properties"}{"x_wr_calname"} ||= $app->moniker
$self->{"properties"}{"x_wr_calname"} $app->moniker
Truth table33%1/3
A ||= B
A= $self->{"properties"}{"x_wr_calname"}B= $app->moniker
ABresult
1X1
011
000
22 2 ▶ $self->{properties}{x_wr_timezone} ||= POSIX::strftime('%Z', localtime);
Condition33%1/3
$self->{"properties"}{"x_wr_timezone"} ||= POSIX::strftime("%Z", localtime)
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"properties"}{"x_wr_timezone"} ||= POSIX::strftime("%Z", localtime)
$self->{"properties"}{"x_wr_timezone"} POSIX::strftime("%Z", localtime)
Truth table33%1/3
A ||= B
A= $self->{"properties"}{"x_wr_timezone"}B= POSIX::strftime("%Z", localtime)
ABresult
1X1
011
000
23  
24 2 ▶ $self->{vfile} ||= Text::vFile::asData->new;
Condition33%1/3
$self->{"vfile"} ||= "Text::vFile::asData"->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"vfile"} ||= "Text::vFile::asData"->new
$self->{"vfile"} "Text::vFile::asData"->new
Truth table33%1/3
A ||= B
A= $self->{"vfile"}B= "Text::vFile::asData"->new
ABresult
1X1
011
000
25  
26 2 ▶ $app->helper('reply.ical' => sub { $self->_reply_ical(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
27 2 $app->types->type(ical => 'text/calendar');
28  
29 2 ▶ if ($config->{handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"handler"}) 2 0 2
30 $app->renderer->add_handler(
31 $config->{handler},
32 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
33 1 my ($renderer, $c, $output, $options) = @_;
34 1 ▶ return undef unless my $ical = $c->stash('ical');
Branch: 1 branch
BranchTrueFalseTotal
unless my $ical = $c->stash("ical") 0 1 1
35 1 $$output = join '', map {"$_\n"} $self->_render_ical($c, $ical);
36 1 return 1;
37 }
38 2 );
39 }
40 }
41  
42 2 ▶sub _event_to_properties {
Subroutine _event_to_properties: called CC=9 SCAR=22.7
43 2 my ($event, $defaults) = @_;
44 2 my $properties = {};
45  
46 2 for my $k (keys %$event) {
47 14 ▶ my $v = $event->{$k} //= '';
Condition50%1/2
$event->{$k} //= ""
l!l
140
MC/DC0%0/1
$event->{$k} //= ""
$event->{$k}
Truth table50%1/2
A
A= $event->{$k}
Aresult
11
00
48 14 my $p = _vkey($k);
49 14 ▶ if (UNIVERSAL::isa($v, 'Mojo::Date')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($v, "Mojo::Date")) 6 8 14
50 6 $v = $v->to_datetime;
51 6 $v =~ s![:-]!!g; # 1994-11-06T08:49:37Z => 19941106T084937Z
52 }
53 14 $properties->{$p} = [{value => $v}];
54 }
55  
56 2 ▶ $properties->{DTSTAMP} ||= [{value => $defaults->{now}}];
Condition50%1/2
$properties->{"DTSTAMP"} ||= [{"value", $defaults->{"now"}}]
l!l
02
MC/DC0%0/1
$properties->{"DTSTAMP"} ||= [{"value", $defaults->{"now"}}]
$properties->{"DTSTAMP"}
Truth table50%1/2
A
A= $properties->{"DTSTAMP"}
Aresult
11
00
57 2 ▶ $properties->{SEQUENCE} ||= [{value => 0}];
Condition50%1/2
$properties->{"SEQUENCE"} ||= [{"value", 0}]
l!l
20
MC/DC0%0/1
$properties->{"SEQUENCE"} ||= [{"value", 0}]
$properties->{"SEQUENCE"}
Truth table50%1/2
A
A= $properties->{"SEQUENCE"}
Aresult
11
00
58 2 ▶ $properties->{STATUS} ||= [{value => 'CONFIRMED'}];
Condition50%1/2
$properties->{"STATUS"} ||= [{"value", "CONFIRMED"}]
l!l
02
MC/DC0%0/1
$properties->{"STATUS"} ||= [{"value", "CONFIRMED"}]
$properties->{"STATUS"}
Truth table50%1/2
A
A= $properties->{"STATUS"}
Aresult
11
00
59 2 ▶ $properties->{TRANSP} ||= [{value => 'OPAQUE'}];
Condition50%1/2
$properties->{"TRANSP"} ||= [{"value", "OPAQUE"}]
l!l
02
MC/DC0%0/1
$properties->{"TRANSP"} ||= [{"value", "OPAQUE"}]
$properties->{"TRANSP"}
Truth table50%1/2
A
A= $properties->{"TRANSP"}
Aresult
11
00
60 2 ▶ $properties->{UID} ||= [{value => sprintf '%s@%s', _md5($event), $defaults->{hostname}}];
Condition50%1/2
$properties->{"UID"} ||= [{"value", sprintf("%s\@%s", _md5($event), $defaults->{"hostname"})}]
l!l
02
MC/DC0%0/1
$properties->{"UID"} ||= [{"value", sprintf("%s\@%s", _md5($event), $defaults->{"hostname"})}]
$properties->{"UID"}
Truth table50%1/2
A
A= $properties->{"UID"}
Aresult
11
00
61 2 $properties;
62 }
63  
64 2 ▶sub _render_ical {
Subroutine _render_ical: called CC=12 SCAR=28.0
65 2 my ($self, $c, $data) = @_;
66 2 ▶ my %properties = %{$data->{properties} || {}};
Condition50%1/2
$data->{'properties'} || {}
l!l
02
MC/DC0%0/1
$data->{'properties'} || {}
$data->{'properties'}
Truth table50%1/2
A
A= $data->{'properties'}
Aresult
11
00
67 2 my $ical = {};
68 2 my %defaults;
69  
70 2 $ical->{objects} = [];
71 2 $ical->{properties} = {};
72 2 $ical->{type} = 'VCALENDAR';
73  
74 2 ▶ $properties{calscale} ||= $self->{properties}{calscale};
Condition33%1/3
$properties{"calscale"} ||= $self->{"properties"}{"calscale"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$properties{"calscale"} ||= $self->{"properties"}{"calscale"}
$properties{"calscale"} $self->{"properties"}{"calscale"}
Truth table33%1/3
A ||= B
A= $properties{"calscale"}B= $self->{"properties"}{"calscale"}
ABresult
1X1
011
000
75 2 ▶ $properties{method} ||= $self->{properties}{method};
Condition33%1/3
$properties{"method"} ||= $self->{"properties"}{"method"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$properties{"method"} ||= $self->{"properties"}{"method"}
$properties{"method"} $self->{"properties"}{"method"}
Truth table33%1/3
A ||= B
A= $properties{"method"}B= $self->{"properties"}{"method"}
ABresult
1X1
011
000
76 2 ▶ $properties{prodid} ||= $self->{properties}{prodid};
Condition33%1/3
$properties{"prodid"} ||= $self->{"properties"}{"prodid"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$properties{"prodid"} ||= $self->{"properties"}{"prodid"}
$properties{"prodid"} $self->{"properties"}{"prodid"}
Truth table33%1/3
A ||= B
A= $properties{"prodid"}B= $self->{"properties"}{"prodid"}
ABresult
1X1
011
000
77 2 ▶ $properties{version} ||= $self->{properties}{version};
Condition33%1/3
$properties{"version"} ||= $self->{"properties"}{"version"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$properties{"version"} ||= $self->{"properties"}{"version"}
$properties{"version"} $self->{"properties"}{"version"}
Truth table33%1/3
A ||= B
A= $properties{"version"}B= $self->{"properties"}{"version"}
ABresult
1X1
011
000
78 2 ▶ $properties{x_wr_caldesc} ||= $self->{properties}{x_wr_caldesc};
Condition33%1/3
$properties{"x_wr_caldesc"} ||= $self->{"properties"}{"x_wr_caldesc"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$properties{"x_wr_caldesc"} ||= $self->{"properties"}{"x_wr_caldesc"}
$properties{"x_wr_caldesc"} $self->{"properties"}{"x_wr_caldesc"}
Truth table33%1/3
A ||= B
A= $properties{"x_wr_caldesc"}B= $self->{"properties"}{"x_wr_caldesc"}
ABresult
1X1
011
000
79 2 ▶ $properties{x_wr_calname} ||= $self->{properties}{x_wr_calname};
Condition33%1/3
$properties{"x_wr_calname"} ||= $self->{"properties"}{"x_wr_calname"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$properties{"x_wr_calname"} ||= $self->{"properties"}{"x_wr_calname"}
$properties{"x_wr_calname"} $self->{"properties"}{"x_wr_calname"}
Truth table33%1/3
A ||= B
A= $properties{"x_wr_calname"}B= $self->{"properties"}{"x_wr_calname"}
ABresult
1X1
011
000
80 2 ▶ $properties{x_wr_timezone} ||= $self->{properties}{x_wr_timezone};
Condition33%1/3
$properties{"x_wr_timezone"} ||= $self->{"properties"}{"x_wr_timezone"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$properties{"x_wr_timezone"} ||= $self->{"properties"}{"x_wr_timezone"}
$properties{"x_wr_timezone"} $self->{"properties"}{"x_wr_timezone"}
Truth table33%1/3
A ||= B
A= $properties{"x_wr_timezone"}B= $self->{"properties"}{"x_wr_timezone"}
ABresult
1X1
011
000
81  
82 2 for my $k (keys %properties) {
83 14 my $p = _vkey($k);
84 14 $ical->{properties}{$p} = [{value => $properties{$k}}];
85 }
86  
87 2 $defaults{hostname} = Sys::Hostname::hostname;
88 2 $defaults{now} = Mojo::Date->new->to_datetime;
89 2 $defaults{now} =~ s![:-]!!g; # 1994-11-06T08:49:37Z => 19941106T084937Z
90  
91 2 ▶ for my $event (@{$data->{events} || []}) {
Condition50%1/2
$data->{'events'} || []
l!l
20
MC/DC0%0/1
$data->{'events'} || []
$data->{'events'}
Truth table50%1/2
A
A= $data->{'events'}
Aresult
11
00
92 2 push @{$ical->{objects}}, {properties => _event_to_properties($event, \%defaults), type => 'VEVENT'};
93 }
94  
95 2 return $self->{vfile}->generate_lines($ical);
96 }
97  
98 1 ▶sub _reply_ical {
Subroutine _reply_ical: called CC=1 SCAR=0.0
99 1 my ($self, $c, $data) = @_;
100 1 $c->res->headers->content_type('text/calendar');
101 1 $c->render(text => join '', map {"$_\n"} $self->_render_ical($c, $data));
102 }
103  
104 2 ▶sub _md5 {
Subroutine _md5: called CC=1 SCAR=0.0
105 2 my $data = $_[0];
106 2 Mojo::Util::md5_sum(join ':', map {"$_=$data->{$_}"} grep { $_ ne 'dtstamp' } sort keys %$data);
107 }
108  
109 28 ▶sub _vkey {
Subroutine _vkey: called CC=2 SCAR=7.0
110 28 ▶ return $_[0] if $_[0] =~ /^[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^[A-Z]/u 0 28 28
111 28 local $_ = uc $_[0];
112 28 s!_!-!g;
113 28 $_;
114 }
115  
116 2 1;
117  
118 =encoding utf8
119  
120 =head1 NAME
121  
122 Mojolicious::Plugin::Ical - Generate .ical documents
123  
124 =head1 VERSION
125  
126 0.05
127  
128 =head1 SYNOPSIS
129  
130 =head2 Application
131  
132 use Mojolicious::Lite;
133 plugin ical => {
134 properties => {
135 calscale => "GREGORIAN" # default GREGORIAN
136 method => "REQUEST", # default PUBLISH
137 prodid => "-//ABC Corporation//NONSGML My Product//EN",
138 version => "1.0", # default to 2.0
139 x_wr_caldesc => "Some description",
140 x_wr_calname => "My calender",
141 x_wr_timezone => "EDT", # default to timezone for localhost
142 }
143 };
144  
145 get '/calendar' => sub {
146 my $c = shift;
147 $c->reply->ical({
148 events => [
149 {
150 created => $date,
151 description => $str, # http://www.kanzaki.com/docs/ical/description.html
152 dtend => $date,
153 dtstamp => $date, # UTC time format, defaults to "now"
154 dtstart => $date,
155 last_modified => $date, # defaults to "now"
156 location => $str, # http://www.kanzaki.com/docs/ical/location.html
157 sequence => $int, # default 0
158 status => $str, # default CONFIRMED
159 summary => $str, # http://www.kanzaki.com/docs/ical/summary.html
160 transp => $str, # default OPAQUE
161 uid => $str, # default to md5 of the values @hostname
162 },
163 ...
164 ],
165 });
166 };
167  
168 # or using respond_to()
169 get '/events' => sub {
170 my $c = shift;
171 my $ical = { events => [...] };
172 $c->respond_to(
173 ical => {handler => 'ical', ical => $ical},
174 json => {json => $ical}
175 );
176 };
177  
178 =head1 DESCRIPTION
179  
180 L<Mojolicious::Plugin::Ical> is a L<Mojolicious> plugin for generating
181 L<iCalendar|http://www.kanzaki.com/docs/ical/> documents.
182  
183 This plugin will...
184  
185 =over 4
186  
187 =item *
188  
189 Add the helper L</reply.ical>.
190  
191 =item *
192  
193 Add ".ical" type to L<Mojolicious/types>.
194  
195 =item *
196  
197 Add a handler "ical" to L<Mojolicious/renderer>.
198  
199 =back
200  
201 =head1 HELPERS
202  
203 =head2 reply.ical
204  
205 $c = $c->reply->ical({ events => [...], properties => {...} });
206  
207 Will render a iCal document with the Content-Type "text/calender".
208  
209 C<events> is an array ref of calendar events.
210 C<properties> will override the defaults given to L</register>.
211  
212 See L</SYNOPSIS> for more details.
213  
214 =head1 METHODS
215  
216 =head2 register
217  
218 plugin ical => {properties => {...}};
219  
220 Register L</reply.ical> helper.
221  
222 =head1 COPYRIGHT AND LICENSE
223  
224 Copyright (C) 2014, Jan Henning Thorsen
225  
226 This program is free software, you can redistribute it and/or modify it under
227 the terms of the Artistic License version 2.0.
228  
229 =head1 AUTHOR
230  
231 Jan Henning Thorsen - C<jhthorsen@cpan.org>
232  
233 =cut
↑ Summary