2018-07-14 00:50:13 +02:00
|
|
|
|
:ruby
|
2019-04-27 15:12:42 +02:00
|
|
|
|
require 'icalendar/tzinfo'
|
|
|
|
|
|
2018-07-14 00:50:13 +02:00
|
|
|
|
# Create a calendar with an event (standard method)
|
|
|
|
|
cal = Icalendar::Calendar.new
|
|
|
|
|
@events.each do |event|
|
2019-04-27 14:44:29 +02:00
|
|
|
|
tzid = event.region.tzid
|
2019-04-27 17:10:45 +02:00
|
|
|
|
use_timezone = true
|
2019-04-27 15:53:54 +02:00
|
|
|
|
if cal.timezones.none? { |t| tzid == t.tzid }
|
|
|
|
|
# Only add this zone once
|
|
|
|
|
begin
|
|
|
|
|
tz = TZInfo::Timezone.get tzid
|
|
|
|
|
timezone = tz.ical_timezone event.start_time
|
|
|
|
|
cal.add_timezone timezone
|
|
|
|
|
rescue TZInfo::InvalidTimezoneIdentifier
|
|
|
|
|
# No need to add this tz
|
2019-04-27 17:10:45 +02:00
|
|
|
|
use_timezone = false
|
2019-04-27 15:53:54 +02:00
|
|
|
|
end
|
2019-04-27 15:39:06 +02:00
|
|
|
|
end
|
2019-04-27 15:12:42 +02:00
|
|
|
|
|
2018-07-14 00:50:13 +02:00
|
|
|
|
cal.event do |e|
|
2019-04-27 17:10:45 +02:00
|
|
|
|
if use_timezone
|
|
|
|
|
e.dtstamp = Icalendar::Values::DateTime.new event.decision_time, tzid: tzid
|
|
|
|
|
e.dtstart = Icalendar::Values::DateTime.new event.start_time, tzid: tzid
|
|
|
|
|
e.dtend = Icalendar::Values::DateTime.new event.end_time, tzid: tzid
|
|
|
|
|
else
|
|
|
|
|
e.dtstamp = Icalendar::Values::DateTime.new event.decision_time.localtime
|
|
|
|
|
e.dtstart = Icalendar::Values::DateTime.new event.start_time.localtime
|
|
|
|
|
e.dtend = Icalendar::Values::DateTime.new event.end_time.localtime
|
|
|
|
|
end
|
2018-07-14 18:17:21 +02:00
|
|
|
|
e.uid = "#{event.id}@#{request.domain}"
|
2018-07-14 00:50:13 +02:00
|
|
|
|
e.summary = event.title
|
2019-04-27 14:44:29 +02:00
|
|
|
|
e.description = to_markdown event.description.tr '\'', '’'
|
2018-07-14 00:50:13 +02:00
|
|
|
|
e.location = event.full_address.tr '\'', '’'
|
2019-04-27 14:44:29 +02:00
|
|
|
|
e.organizer = "mailto:#{event.contact}"
|
|
|
|
|
e.x_alt_desc = Icalendar::Values::Text.new event.description, FMTTYPE: 'text/html'
|
2018-07-14 00:50:13 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
cal.publish
|
|
|
|
|
|
|
|
|
|
= cal.to_ical
|