Module: Protocol::Caldav::Vcard::Parser

Defined in:
lib/protocol/caldav/vcard/parser.rb

Class Method Summary collapse

Class Method Details

.parse(text) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/protocol/caldav/vcard/parser.rb', line 11

def parse(text)
  if text.nil? || text.strip.empty?
    nil
  else
    text = text.sub(/\A\xEF\xBB\xBF/, '')
    lines = ContentLine.unfold(text).split("\n").map(&:strip).reject(&:empty?)
    props = []
    inside = false
    lines.each do |line|
      parsed = ContentLine.parse_line(line)
      unless parsed
        next
      end

      name, params, value = parsed

      if name.casecmp?('BEGIN') && value.strip.casecmp?('VCARD')
        inside = true
      elsif name.casecmp?('END') && value.strip.casecmp?('VCARD')
        break
      elsif inside
        props << Ical::Property.new(name: name, params: params, value: value)
      end
    end
    props.empty? ? nil : Card.new(properties: props)
  end
end