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.



31
32
33
34
# File 'lib/matcher.rb', line 31

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.



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

def trie
  @trie
end

Instance Method Details

#matches?(text) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/matcher.rb', line 36

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