class Dizby::Access::Entry

Public Class Methods

new(str) click to toggle source
# File lib/dizby/access/entry.rb, line 12
def initialize(str)
  if str == '*' || str == 'all'
    @access = [:all]
  elsif str.include?('*')
    @access = [:name, pattern(str)]
  else
    begin
      @access = [:ip, IPAddr.new(str)]
    rescue ArgumentError
      @access = [:name, pattern(str)]
    end
  end
end

Private Class Methods

matches_ip?(addr, pattern) click to toggle source
# File lib/dizby/access/entry.rb, line 50
def matches_ip?(addr, pattern)
  ipaddr = IPAddr.new(addr[3])
  # map to ipv6 if entry is ipv6 and address is ipv4
  ipaddr = ipaddr.ipv4_mapped if pattern.ipv6? && ipaddr.ipv4?

  pattern.include?(ipaddr)
rescue ArgumentError
  false
end
matches_name?(addr, pattern) click to toggle source
# File lib/dizby/access/entry.rb, line 60
def matches_name?(addr, pattern)
  pattern =~ addr[2]
end
pattern(str) click to toggle source
# File lib/dizby/access/entry.rb, line 44
def pattern(str)
  pattern = str.split('.')
  pattern.map! { |segment| segment == '*' ? '.+' : segment }
  /^#{pattern.join('\\.')}$/
end

Public Instance Methods

matches?(addr) click to toggle source
# File lib/dizby/access/entry.rb, line 26
def matches?(addr)
  (scope, pattern) = @access

  case scope
  when :all
    true
  when :ip
    matches_ip?(addr, pattern)
  when :name
    matches_name?(addr, pattern)
  else
    false
  end
end