Class: Oober::Extractor::Stix

Inherits:
Hashie::Dash
  • Object
show all
Defined in:
lib/oober/extractor/stix.rb

Instance Method Summary collapse

Instance Method Details

#extractObject



27
28
29
# File 'lib/oober/extractor/stix.rb', line 27

def extract
  selected.map {|node| extract_selection(node)}
end

#extract_selection(node) ⇒ Object



31
32
33
34
# File 'lib/oober/extractor/stix.rb', line 31

def extract_selection(node)
  mapped_values = extractions.map {|extraction| extract_value(extraction.merge(node: node)) }
  Hash[*mapped_values.flatten].merge(defaults)
end

#extract_value(node: nil, origin: nil, target: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/oober/extractor/stix.rb', line 36

def extract_value(node: nil, origin: nil, target: nil)
  target_key = target.to_sym
  target_val = ''
  begin
    target_val = [*node.xpath(origin)].first.to_s.strip
  rescue Exception => e
    STDERR.puts "FAILED TO EXTRACT #{origin}"
    STDERR.puts e.backtrace
    STDERR.puts e
  end
  [target_key, target_val]
end

#parse_optionsObject



23
24
25
# File 'lib/oober/extractor/stix.rb', line 23

def parse_options
  @parse_options ||= Nokogiri::XML::ParseOptions::DEFAULT_XML|Nokogiri::XML::ParseOptions::NOBLANKS
end

#selectedObject



15
16
17
18
19
20
21
# File 'lib/oober/extractor/stix.rb', line 15

def selected
  @selected ||= begin
    xml.xpath(self.select).to_a
  rescue Nokogiri::XML::XPath::SyntaxError => e
    []
  end
end

#xmlObject



11
12
13
# File 'lib/oober/extractor/stix.rb', line 11

def xml
  @xml ||= Nokogiri::XML(data,nil,nil,parse_options)
end