Class: Dizby::Access::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/dizby/access/entry.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Entry

Returns a new instance of Entry.


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dizby/access/entry.rb', line 7

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

Class Method Details

.matches_ip?(addr, pattern) ⇒ Boolean (private)

Returns:

  • (Boolean)

45
46
47
48
49
50
51
52
53
# File 'lib/dizby/access/entry.rb', line 45

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) ⇒ Boolean (private)

Returns:

  • (Boolean)

55
56
57
# File 'lib/dizby/access/entry.rb', line 55

def matches_name?(addr, pattern)
  pattern =~ addr[2]
end

.pattern(str) (private)


39
40
41
42
43
# File 'lib/dizby/access/entry.rb', line 39

def pattern(str)
  pattern = str.split('.')
  pattern.map! { |segment| (segment == '*') ? '.+' : segment }
  /^#{pattern.join('\\.')}$/
end

Instance Method Details

#matches?(addr) ⇒ Boolean

Returns:

  • (Boolean)

21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dizby/access/entry.rb', line 21

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