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