Class: Tilia::VObject::Splitter::ICalendar

Inherits:
Object
  • Object
show all
Includes:
SplitterInterface
Defined in:
lib/tilia/v_object/splitter/i_calendar.rb

Overview

Splitter.

This class is responsible for splitting up iCalendar objects.

This class expects a single VCALENDAR object with one or more calendar-objects inside. Objects with identical UID’s will be combined into a single object.

Instance Method Summary collapse

Constructor Details

#initialize(input, options = 0) ⇒ ICalendar

Constructor.

The splitter should receive an readable file stream as it’s input.

Parameters:

  • input (resource)
  • options (Fixnum) (defaults to: 0)

    Parser options, see the OPTIONS constants.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tilia/v_object/splitter/i_calendar.rb', line 31

def initialize(input, options = 0)
  @vtimezones = {}
  @objects = {}

  data = Reader.read(input, options)

  unless data.is_a?(Component::VCalendar)
    fail ParseException, 'Supplied input could not be parsed as VCALENDAR.'
  end

  data.children.each do |component|
    next unless component.is_a? Component

    if component.name == 'VTIMEZONE'
      @vtimezones[component['TZID'].to_s] = component
      next
    end

    # Get component UID for recurring Events search
    if component['UID'].blank?
      component['UID'] = "#{Digest::SHA1.hexdigest(Time.now.to_s + rand.to_s)}-vobjectimport"
    end

    uid = component['UID'].to_s

    # Take care of recurring events
    @objects[uid] = Component::VCalendar.new unless @objects.key?(uid)

    @objects[uid].add(component.dup)
  end
end

Instance Method Details

#nextSabre\VObject\Component?

Every time self.next is called, a new object will be parsed, until we hit the end of the stream.

When the end is reached, null will be returned.

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tilia/v_object/splitter/i_calendar.rb', line 69

def next
  key = @objects.keys.first

  if key
    object = @objects.delete(key)

    # create our baseobject
    object['VERSION'] = '2.0'
    object['PRODID'] = "-//Tilia//Tilia VObject #{Version::VERSION}//EN"
    object['CALSCALE'] = 'GREGORIAN'

    # add vtimezone information to obj (if we have it)
    @vtimezones.keys.each do |vtimezone|
      object.add(@vtimezones[vtimezone])
    end

    return object
  else
    return nil
  end

  nil
end