Class: Dizby::Access::Entry
- Inherits:
-
Object
- Object
- Dizby::Access::Entry
- Defined in:
- lib/dizby/access/entry.rb
Class Method Summary collapse
- .matches_ip?(addr, pattern) ⇒ Boolean private
- .matches_name?(addr, pattern) ⇒ Boolean private
- .pattern(str) private
Instance Method Summary collapse
-
#initialize(str) ⇒ Entry
constructor
A new instance of Entry.
- #matches?(addr) ⇒ Boolean
Constructor Details
#initialize(str) ⇒ Entry
Returns a new instance of Entry.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dizby/access/entry.rb', line 9 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)
47 48 49 50 51 52 53 54 55 |
# File 'lib/dizby/access/entry.rb', line 47 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)
57 58 59 |
# File 'lib/dizby/access/entry.rb', line 57 def matches_name?(addr, pattern) pattern =~ addr[2] end |
.pattern(str) (private)
41 42 43 44 45 |
# File 'lib/dizby/access/entry.rb', line 41 def pattern(str) pattern = str.split('.') pattern.map! { |segment| (segment == '*') ? '.+' : segment } /^#{pattern.join('\\.')}$/ end |
Instance Method Details
#matches?(addr) ⇒ Boolean
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dizby/access/entry.rb', line 23 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 |