Class: Googlecalendar::ICALParser

Inherits:
Object
  • Object
show all
Defined in:
lib/googlecalendar/ical.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#calendarObject (readonly)

Returns the value of attribute calendar.



3
4
5
# File 'lib/googlecalendar/ical.rb', line 3

def calendar
  @calendar
end

Instance Method Details

#handle_begin(value) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/googlecalendar/ical.rb', line 37

def handle_begin(value)
  if value == "VCALENDAR"
    handle_vcalendar_begin(value)
  elsif value == "VEVENT"
    handle_vevent_begin(value)
  end
end

#handle_element(line) ⇒ Object

An ICal line consist of 2 part XXX: YYYY Get the first part of the line and after a small transformation call the appropriate method example:

line is: VCALENDAR:BEGIN 
         VERSION:VALUE
method call: handle_vcalendar_version

kind of work like a sax parser…



23
24
25
26
27
28
29
30
31
# File 'lib/googlecalendar/ical.rb', line 23

def handle_element(line)
  pair = line.split(':', 2) # avoid problems when summary contains ':'
  name = pair[0]
  value = pair[1]
  handler_method = @method_prefix + name.split(';')[0].tr('-', '_').downcase
  if self.respond_to? handler_method
    self.send(handler_method, value.chomp)
  end
end

#handle_vcalendar_begin(value) ⇒ Object



49
50
51
52
# File 'lib/googlecalendar/ical.rb', line 49

def handle_vcalendar_begin(value)
  @calendar = Calendar.new
  @method_prefix = @method_prefix + value.downcase + "_"
end

#handle_vcalendar_calscale(value) ⇒ Object



62
63
64
# File 'lib/googlecalendar/ical.rb', line 62

def handle_vcalendar_calscale(value)
  @calendar.scale = value
end

#handle_vcalendar_end(value) ⇒ Object



45
46
47
# File 'lib/googlecalendar/ical.rb', line 45

def handle_vcalendar_end(value)
  reset_prefix
end

#handle_vcalendar_method(value) ⇒ Object



66
67
68
69
70
# File 'lib/googlecalendar/ical.rb', line 66

def handle_vcalendar_method(value)
  @calendar.method = value
  # FIXME don't like to do this!
  reset_prefix
end

#handle_vcalendar_version(value) ⇒ Object

def handle_vcalendar_prodid(value)

  @calendar.product_id = value
end


58
59
60
# File 'lib/googlecalendar/ical.rb', line 58

def handle_vcalendar_version(value)
  @calendar.version = value
end

#handle_vevent_begin(value) ⇒ Object



72
73
74
75
76
# File 'lib/googlecalendar/ical.rb', line 72

def handle_vevent_begin(value)
    event = Event.new
    @calendar.add event
    @method_prefix = @method_prefix + value.downcase + "_"
end

#handle_vevent_class(value) ⇒ Object



94
95
96
# File 'lib/googlecalendar/ical.rb', line 94

def handle_vevent_class(value)
  @calendar.events.last.class_name = value
end

#handle_vevent_created(value) ⇒ Object



98
99
100
# File 'lib/googlecalendar/ical.rb', line 98

def handle_vevent_created(value)
  @calendar.events.last.created = DateTime.parse(value)
end

#handle_vevent_description(value) ⇒ Object



118
119
120
# File 'lib/googlecalendar/ical.rb', line 118

def handle_vevent_description(value)
  @calendar.events.last.description = value
end

#handle_vevent_dtend(value) ⇒ Object



86
87
88
# File 'lib/googlecalendar/ical.rb', line 86

def handle_vevent_dtend(value)
  @calendar.events.last.end_date = Date.parse(value)
end

#handle_vevent_dtstamp(value) ⇒ Object



90
91
92
# File 'lib/googlecalendar/ical.rb', line 90

def handle_vevent_dtstamp(value)
  @calendar.events.last.time_stamp = DateTime.parse(value)
end

#handle_vevent_dtstart(value) ⇒ Object



82
83
84
# File 'lib/googlecalendar/ical.rb', line 82

def handle_vevent_dtstart(value)
  @calendar.events.last.start_date = Date.parse(value)
end

#handle_vevent_end(value) ⇒ Object



78
79
80
# File 'lib/googlecalendar/ical.rb', line 78

def handle_vevent_end(value)
  reset_prefix
end

#handle_vevent_last_modified(value) ⇒ Object



102
103
104
# File 'lib/googlecalendar/ical.rb', line 102

def handle_vevent_last_modified(value)
  @calendar.events.last.last_modified = DateTime.parse(value)
end

#handle_vevent_location(value) ⇒ Object



122
123
124
# File 'lib/googlecalendar/ical.rb', line 122

def handle_vevent_location(value)
  @calendar.events.last.location = value
end

#handle_vevent_rrule(value) ⇒ Object



114
115
116
# File 'lib/googlecalendar/ical.rb', line 114

def handle_vevent_rrule(value)
  @calendar.events.last.rrule = value
end

#handle_vevent_status(value) ⇒ Object



106
107
108
# File 'lib/googlecalendar/ical.rb', line 106

def handle_vevent_status(value)
  @calendar.events.last.status = value
end

#handle_vevent_summary(value) ⇒ Object



110
111
112
# File 'lib/googlecalendar/ical.rb', line 110

def handle_vevent_summary(value)
  @calendar.events.last.summary = value
end

#parse(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/googlecalendar/ical.rb', line 5

def parse(data)
  lines = data.split("\n")
  
  reset_prefix
  lines.each do |line| 
    handle_element(line)
  end
  
  return @calendar
end

#reset_prefixObject



33
34
35
# File 'lib/googlecalendar/ical.rb', line 33

def reset_prefix
  @method_prefix = "handle_"
end