Class: Threatinator::Parsers::XML::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/threatinator/parsers/xml/pattern.rb

Overview

Implements path matching behavior for use with the XML parser. Aims to support a small subset of XPath behaviors, specifically for matching elements.

Instance Method Summary collapse

Constructor Details

#initialize(pathspec) ⇒ Pattern

Returns a new instance of Pattern.

Parameters:

  • pathspec (String)

    A specification of a path match.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/threatinator/parsers/xml/pattern.rb', line 10

def initialize(pathspec)
  parts = pathspec.split('/')
  leader_count = 0
  while parts[0] == ''
    leader_count += 1
    parts.shift
  end
  @path = Threatinator::Parsers::XML::Path.new(parts)
  @anchored = true

  if leader_count == 1
    @anchored = true
  elsif leader_count == 2
    @anchored = false
  else
    raise ArgumentError.new('pathspec must begin with "/" or "//"')
  end
end

Instance Method Details

#==(other) ⇒ Object



47
48
49
# File 'lib/threatinator/parsers/xml/pattern.rb', line 47

def ==(other)
  _internal_data == other._internal_data
end

#_internal_dataObject



43
44
45
# File 'lib/threatinator/parsers/xml/pattern.rb', line 43

def _internal_data
  [@path, @anchored]
end

#match?(path) ⇒ Boolean

Returns true if the pattern matches, false otherwise.

Parameters:

Returns:

  • (Boolean)

    true if the pattern matches, false otherwise.



35
36
37
38
39
40
41
# File 'lib/threatinator/parsers/xml/pattern.rb', line 35

def match?(path)
  if @anchored == true
    @path == path
  else 
    path.end_with?(@path)
  end
end

#max_depthObject



29
30
31
# File 'lib/threatinator/parsers/xml/pattern.rb', line 29

def max_depth
  @anchored == true ? @path.length : Float::INFINITY
end