Class: Threatinator::Parsers::XML::Parser

Inherits:
Threatinator::Parser show all
Defined in:
lib/threatinator/parsers/xml/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Threatinator::Parser

#eql?

Constructor Details

#initialize(opts = {}) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • opts (Hash) (defaults to: {})

    Parameters hash

Options Hash (opts):



16
17
18
19
20
# File 'lib/threatinator/parsers/xml/parser.rb', line 16

def initialize(opts = {})
  @pattern = opts.delete(:pattern) or raise ArgumentError.new("Missing argument :pattern")
  @max_cursor_depth = @pattern.max_depth - 1
  super(opts)
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



12
13
14
# File 'lib/threatinator/parsers/xml/parser.rb', line 12

def pattern
  @pattern
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
25
# File 'lib/threatinator/parsers/xml/parser.rb', line 22

def ==(other)
  @pattern == other.pattern && 
    super(other)
end

#run(io) {|record| ... } ⇒ Object

Parameters:

  • io (IO)

Yields:

  • (record)

    Gives one line to the block

Yield Parameters:

  • record (Threatinator::Parser::XML::Record)

    a record



30
31
32
33
34
35
36
37
38
39
# File 'lib/threatinator/parsers/xml/parser.rb', line 30

def run(io)
  stack = Path.new
  callback = lambda do |element|
    yield(Threatinator::Parsers::XML::Record.new(element))
  end

  doc = SAXDocument.new(@pattern, callback)
  parser = Nokogiri::XML::SAX::Parser.new(doc)
  parser.parse(io)
end