Module: Protocol::Caldav::Xml

Defined in:
lib/protocol/caldav/xml.rb

Class Method Summary collapse

Class Method Details

.escape(str) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/protocol/caldav/xml.rb', line 8

def escape(str)
  if str
    str.to_s
       .gsub('&', '&')
       .gsub('<', '&lt;')
       .gsub('>', '&gt;')
       .gsub('"', '&quot;')
  else
    ''
  end
end

.extract_attr(xml, tag, attr) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/protocol/caldav/xml.rb', line 34

def extract_attr(xml, tag, attr)
  if xml.nil? || xml.empty?
    nil
  else
    match = xml.match(/<[^>]*#{Regexp.escape(tag)}[^>]*#{Regexp.escape(attr)}="([^"]*)"/)
    match ? match[1] : nil
  end
end

.extract_value(xml, tag) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/protocol/caldav/xml.rb', line 20

def extract_value(xml, tag)
  if xml.nil? || xml.empty?
    nil
  else
    match = xml.match(/<[^>]*#{Regexp.escape(tag)}[^>]*>([^<]*)</)
    if match
      value = match[1].strip
      value.empty? ? nil : value
    else
      nil
    end
  end
end