class Mhc::Converter::Icalendar

Public Instance Methods

to_icalendar(event) click to toggle source
# File lib/mhc/converter.rb, line 79
def to_icalendar(event)
  icalendar = RiCal.Event do |iev|
    iev.rrule         = event.recurrence_condition.to_ics(dtstart(event), event.duration.last) if event.recurring?
    iev.exdates       = [exdates(event)] if exdates(event)
    iev.rdates        = [rdates(event)]  if rdates(event)
    iev.created       = created(event).utc.strftime("%Y%m%dT%H%M%SZ")
    iev.categories    = event.categories.to_a unless event.categories.empty?
    iev.location      = event.location.to_s unless event.location.to_s.empty?
    iev.last_modified = last_modified(event).utc.strftime("%Y%m%dT%H%M%SZ")
    iev.uid           = event.uid.to_s
    iev.dtstart       = dtstart(event)
    iev.dtend         = dtend(event)
    iev.summary       = event.subject.to_s
    iev.description   = event.description.to_s
    iev.sequence      = (event.sequence.to_i || 0)
    iev.dtstamp       = ::Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
    iev.add_x_property("X-SC-Recurrence-Tag", event.recurrence_tag.to_s) if event.recurrence_tag.to_s != ""
    iev.add_x_property("X-SC-Mission-Tag", event.mission_tag.to_s) if event.mission_tag.to_s != ""
  end
  return icalendar
end
to_ics(event) click to toggle source
# File lib/mhc/converter.rb, line 68
def to_ics(event)
  return to_icalendar(event).to_s
end
to_ics_string(event) click to toggle source
# File lib/mhc/converter.rb, line 72
def to_ics_string(event)
  ical = RiCal.Calendar
  ical.prodid = Mhc::PRODID
  ical.events << to_icalendar(event)
  return ical.to_s
end

Private Instance Methods

created(event) click to toggle source
# File lib/mhc/converter.rb, line 164
def created(event)
  if event.path
    File.ctime(event.path)
  else
    ::Time.utc(2014, 1, 1)
  end
end
dtend(event) click to toggle source
# File lib/mhc/converter.rb, line 126
def dtend(event)
  if event.recurring?
    Mhc::OccurrenceEnumerator.new(event, empty_dates, empty_dates, event.recurrence_condition, event.duration).first.dtend
  else
    Mhc::OccurrenceEnumerator.new(event, event.dates, empty_dates, empty_condition, empty_duration).first.dtend
  end
end
dtstart(event) click to toggle source

DTSTART (RFC5445:iCalendar) has these two meanings: 1) first ocurrence date of recurrence events 2) start date of a single-shot event

In MHC, DTSTART should be calculated as:

if a MHC article has a Cond: field,

+ DTSTART is calculated from Duration: and Cond: field.
+ Additional Day: field is recognized as RDATE.

else

+ DTSTART is calculated from a first entry of Days: field.
+ Remains in Day: field is recognized as RDATE.

end

# File lib/mhc/converter.rb, line 118
def dtstart(event)
  if event.recurring?
    Mhc::OccurrenceEnumerator.new(event, empty_dates, empty_dates, event.recurrence_condition, event.duration).first.dtstart
  else
    Mhc::OccurrenceEnumerator.new(event, event.dates, empty_dates, empty_condition, empty_duration).first.dtstart
  end
end
empty_condition() click to toggle source
# File lib/mhc/converter.rb, line 160
def empty_condition
  Mhc::PropertyValue::RecurrenceCondition.new
end
empty_dates() click to toggle source
# File lib/mhc/converter.rb, line 156
def empty_dates
  Mhc::PropertyValue::List.new(Mhc::PropertyValue::Range.new(Mhc::PropertyValue::Date.new))
end
empty_duration() click to toggle source
# File lib/mhc/converter.rb, line 152
def empty_duration
  Mhc::PropertyValue::Range.new(Mhc::PropertyValue::Date)
end
exdates(event) click to toggle source
# File lib/mhc/converter.rb, line 146
def exdates(event)
  return nil if event.exceptions.empty?
  ocs = Mhc::OccurrenceEnumerator.new(event, event.exceptions, empty_dates, empty_condition, empty_duration).map {|oc| oc.dtstart }
  return ocs
end
last_modified(event) click to toggle source
# File lib/mhc/converter.rb, line 172
def last_modified(event)
  if event.path
    File.mtime(event.path)
  else
    ::Time.utc(2014, 1, 1)
  end
end
rdates(event) click to toggle source
# File lib/mhc/converter.rb, line 134
def rdates(event)
  return nil if event.dates.empty?
  ocs = Mhc::OccurrenceEnumerator.new(event, event.dates, empty_dates, empty_condition, empty_duration).map {|oc| oc.dtstart}
  if event.recurring?
    ocs
  else
    ocs = ocs[1..-1]
    return nil if ocs.empty?
    return ocs
  end
end