Class: Matchers::IPMatcher

Inherits:
Object
  • Object
show all
Includes:
IP
Defined in:
lib/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patterns) ⇒ IPMatcher

Returns a new instance of IPMatcher.



28
29
30
31
# File 'lib/matcher.rb', line 28

def initialize(patterns)
  patterns = (patterns || []).compact.reject(&:empty?).map { |ip| IP.new(ip) }.map(&:to_binary)
  @trie = Trie.new patterns
end

Instance Attribute Details

#trieObject (readonly)

Returns the value of attribute trie.



25
26
27
# File 'lib/matcher.rb', line 25

def trie
  @trie
end

Instance Method Details

#matches?(text) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/matcher.rb', line 33

def matches?(text)
  return false if text.nil?
  ip = IPAddr.new(text).to_i.to_s(2).rjust(32, '0')
  trie.forward_match(ip)
end