module MingleEvents::Xml

Public Instance Methods

attr(element, attr_name) click to toggle source
   # File lib/mingle_events/xml.rb
48 def attr(element, attr_name)
49   raise 'element selection is empty!' if element.nil?
50   element.node[attr_name]
51 end
attributes(element) click to toggle source
   # File lib/mingle_events/xml.rb
65 def attributes(element)
66   element.node.attribute_nodes.inject({}) do |memo, a|
67     memo[a.name] = a.value
68     memo
69   end
70 end
children(element) click to toggle source
   # File lib/mingle_events/xml.rb
53 def children(element)
54   element.node.children.select { |e| e.is_a?(Nokogiri::XML::Element) }.map { |n| Element.new(n, element.namespaces) }
55 end
inner_text(element, xpath=nil) click to toggle source
   # File lib/mingle_events/xml.rb
29 def inner_text(element, xpath=nil)
30   return inner_text(select(element, xpath)) if xpath
31   return nil if attr(element, "nil") == "true"
32   element.node.inner_text
33 end
optional_inner_text(parent_element, xpath) click to toggle source
   # File lib/mingle_events/xml.rb
35 def optional_inner_text(parent_element, xpath)
36   element = select(parent_element, xpath)
37   element.node.nil? ? nil : element.inner_text
38 end
parse(str, namespaces={}) click to toggle source
   # File lib/mingle_events/xml.rb
25 def parse(str, namespaces={})
26   Element.new(Nokogiri::XML(str), namespaces)
27 end
patching_namespaces(node) click to toggle source
    # File lib/mingle_events/xml.rb
 96 def patching_namespaces(node)
 97   ns_scopes = node.namespace_scopes
 98   return node if ns_scopes.empty?
 99 
100   result = node.clone
101   ns_scopes.each do |ns|
102     result.add_namespace_definition(ns.prefix, ns.href)
103   end
104   result
105 end
raw_xml(element) click to toggle source
   # File lib/mingle_events/xml.rb
61 def raw_xml(element)
62   patching_namespaces(element.node).to_s
63 end
select(element, xpath) click to toggle source
   # File lib/mingle_events/xml.rb
40 def select(element, xpath)
41   Element.new(element.node.at(xpath, element.namespaces), element.namespaces)
42 end
select_all(element, xpath) click to toggle source
   # File lib/mingle_events/xml.rb
44 def select_all(element, xpath)
45   element.node.search(xpath, element.namespaces).map { |n| Element.new(n, element.namespaces) }
46 end
tag_name(element) click to toggle source
   # File lib/mingle_events/xml.rb
57 def tag_name(element)
58   element.node.name
59 end
to_hash(element) click to toggle source
   # File lib/mingle_events/xml.rb
72 def to_hash(element)
73   { tag_name(element).to_sym  => to_hash_attributes(element) }
74 end
to_hash_attributes(element) click to toggle source
   # File lib/mingle_events/xml.rb
76 def to_hash_attributes(element)
77   attrs = attributes(element).inject({}) do |memo, pair|
78     name, value = pair
79     memo[name.to_sym] = value
80     memo
81   end
82 
83   return nil if attrs[:nil] == "true"
84 
85   children = children(element)
86   if children.any?
87     return children.inject(attrs) do |memo, child|
88       memo[ tag_name(child).to_sym ] = to_hash_attributes(child)
89       memo
90     end
91   end
92   inner_text = inner_text(element)
93   inner_text && inner_text.strip != "" ? inner_text : attrs
94 end