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

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

Class Method Summary collapse

Class Method Details

.parse(text) ⇒ Object



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

def parse(text)
  return nil if text.nil? || text.strip.empty?

  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)
    next unless parsed

    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