Class: Icalendar::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/icalendar/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, strict = false) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/icalendar/parser.rb', line 7

def initialize(source, strict = false)
  if source.respond_to? :gets
    @source = source
  elsif source.respond_to? :to_s
    @source = StringIO.new source.to_s, 'r'
  else
    msg = 'Icalendar::Parser.new must be called with a String or IO object'
    Icalendar.fatal msg
    fail ArgumentError, msg
  end
  read_in_data
  @strict = strict
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/icalendar/parser.rb', line 5

def source
  @source
end

#strictObject (readonly)

Returns the value of attribute strict.



5
6
7
# File 'lib/icalendar/parser.rb', line 5

def strict
  @strict
end

Instance Method Details

#get_wrapper_class(component, fields) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/icalendar/parser.rb', line 72

def get_wrapper_class(component, fields)
  klass = component.class.default_property_types[fields[:name]]
  if !fields[:params]['value'].nil?
    klass_name = fields[:params].delete('value').first
    unless klass_name.upcase == klass.value_type
      klass_name = klass_name.downcase.gsub(/(?:\A|-)(.)/) { |m| m[-1].upcase }
      klass = Icalendar::Values.const_get klass_name if Icalendar::Values.const_defined?(klass_name)
    end
  end
  klass
end

#parseObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/icalendar/parser.rb', line 21

def parse
  source.rewind
  read_in_data
  calendars = []
  while (fields = next_fields)
    if fields[:name] == 'begin' && fields[:value].downcase == 'vcalendar'
      calendars << parse_component(Calendar.new)
    end
  end
  calendars
end

#parse_property(component, fields = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/icalendar/parser.rb', line 33

def parse_property(component, fields = nil)
  fields = next_fields if fields.nil?
  prop_value = wrap_property_value component, fields
  prop_name = %w(class method).include?(fields[:name]) ? "ip_#{fields[:name]}" : fields[:name]
  begin
    method_name = if component.class.multiple_properties.include? prop_name
      "append_#{prop_name}"
    else
      "#{prop_name}="
    end
    component.send method_name, prop_value
  rescue NoMethodError => nme
    if strict?
      Icalendar.logger.error "No method \"#{method_name}\" for component #{component}"
      raise nme
    else
      Icalendar.logger.warn "No method \"#{method_name}\" for component #{component}. Appending to custom."
      component.append_custom_property prop_name, prop_value
    end
  end
end

#strict?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/icalendar/parser.rb', line 84

def strict?
  !!@strict
end

#wrap_property_value(component, fields) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/icalendar/parser.rb', line 55

def wrap_property_value(component, fields)
  klass = get_wrapper_class component, fields
  if klass.value_type != 'RECUR' && fields[:value] =~ /(?<!\\)([,;])/
    delimiter = $1
    Icalendar::Values::Array.new fields[:value].split(/(?<!\\)[;,]/),
                                 klass,
                                 fields[:params],
                                 delimiter: delimiter
  else
    klass.new fields[:value], fields[:params]
  end
rescue Icalendar::Values::DateTime::FormatError => fe
  raise fe if strict?
  fields[:params]['value'] = ['DATE']
  retry
end