Module: Splunker::Models::XmlProcessor

Extended by:
XmlProcessor
Included in:
XmlProcessor
Defined in:
lib/splunker/models/xml_processor.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/splunker/models/xml_processor.rb', line 81

def method_missing(method, *args, &block)
  if self.respond_to?(method)
    self.send(method, *args, &block)
  else
    hash = args.first
    node = args[1]
    # TODO: Only text nodes
    hash[node.name] = node.text
    hash
  end
end

Instance Method Details

#hashify(xml_doc) ⇒ Object

TODO: Process dates as dates. How to handle, in hash, things like action.email = 0 when

action.email also needs to == hash...

Process links and other non hash nodes as NOT text.



8
9
10
11
12
13
14
# File 'lib/splunker/models/xml_processor.rb', line 8

def hashify(xml_doc)
  hash = {}
  top_node(xml_doc).children.each do |t_node|
    hash = self.send("process_#{t_node.name}".to_sym, hash, t_node)
  end
  hash
end

#nested_hash(name_array, text, hash) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/splunker/models/xml_processor.rb', line 56

def nested_hash(name_array, text, hash)
  e = name_array.shift
  return text if e.nil?
  #hash[e] ||= {} TODO!
  hash[e] = {} unless hash[e].is_a?(Hash)
  hash[e] = nested_hash(name_array, text, hash[e])
  hash
end

#place_in_nested_hash(name, text, hash) ⇒ Object



51
52
53
54
# File 'lib/splunker/models/xml_processor.rb', line 51

def place_in_nested_hash(name, text, hash)
  nested_hash(name.split("."), text, hash)
  hash
end

#process_content(hash, node) ⇒ Object

Defined custom processors here. Hopefully, there ain’t many!



19
20
21
22
23
24
25
26
# File 'lib/splunker/models/xml_processor.rb', line 19

def process_content(hash, node)
  # So far, looks like content == dict.
  node.xpath("./s:dict/s:key").each do |key|
    hash = place_in_nested_hash(key.attribute("name").value, 
             key.text, hash)
  end
  hash
end

#process_results(hash, node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/splunker/models/xml_processor.rb', line 28

def process_results(hash, node)
  # TODO: Process meta. Ugh.
  hash["meta"] = Array(node.xpath("./meta").children)

  # Process results, yay.
  hash["results"] = []

  node.xpath("./result").each do |r|
    result = {}
    r.xpath("./field").each do |f|
      next unless f.attributes.include?("k")
      result[f.attribute("k").value] = f.text.strip
    end
    hash["results"] << result
  end

  hash
end

#process_text(hash, node) ⇒ Object



47
48
49
# File 'lib/splunker/models/xml_processor.rb', line 47

def process_text(hash, node)
  hash
end

#top_node(xml_doc) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/splunker/models/xml_processor.rb', line 65

def top_node(xml_doc)
  # Handle the various types of XML structure
  ["/xmlns:feed/xmlns:entry",
   "/xmlns:entry"].each do |path|
    begin
      if xml_doc.xpath("#{path}/*").size > 0
        return xml_doc.xpath(path)
      end
    rescue Nokogiri::XML::XPath::SyntaxError => e
      # Ignore...
    end
  end

  xml_doc
end