Class: Hashify

Inherits:
Object
  • Object
show all
Defined in:
lib/hashify.rb

Class Method Summary collapse

Class Method Details

.convert(node) ⇒ Object

Return a Hash from a Nokogiri::XML::Document.

node: An Nokogiri::XML::Document.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hashify.rb', line 7

def self.convert node
  children    = {}
  child_nodes = node.children

  if child_nodes.first.nil?
    children = nil
  elsif child_nodes.first.text?
    children = child_nodes.first.text
  else
    child_nodes.each do |child|
      convert(child).each_pair do |k, v|
        children[k] = if children.key? k
          children[k].is_a?(Array) ? (children[k] << v) : ([children[k], v])
        else
          v
        end
      end
    end
  end

  { node.name => children }
end