Module: Protocol::Caldav::Xml

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

Class Method Summary collapse

Class Method Details

.escape(str) ⇒ Object



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

def escape(str)
  return '' unless str

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

.extract_attr(xml, tag, attr) ⇒ Object



31
32
33
34
35
36
# File 'lib/protocol/caldav/xml.rb', line 31

def extract_attr(xml, tag, attr)
  return nil if xml.nil? || xml.empty?

  match = xml.match(/<[^>]*#{Regexp.escape(tag)}[^>]*#{Regexp.escape(attr)}="([^"]*)"/)
  match ? match[1] : nil
end

.extract_value(xml, tag) ⇒ Object



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

def extract_value(xml, tag)
  return nil if xml.nil? || xml.empty?

  match = xml.match(/<[^>]*#{Regexp.escape(tag)}[^>]*>([^<]*)</)
  return nil unless match

  value = match[1].strip
  value.empty? ? nil : value
end