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.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dizby/access/entry.rb', line 13 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)
51 52 53 54 55 56 57 58 59 |
# File 'lib/dizby/access/entry.rb', line 51 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)
61 62 63 |
# File 'lib/dizby/access/entry.rb', line 61 def matches_name?(addr, pattern) pattern =~ addr[2] end |
.pattern(str) (private)
45 46 47 48 49 |
# File 'lib/dizby/access/entry.rb', line 45 def pattern(str) pattern = str.split('.') pattern.map! { |segment| (segment == '*') ? '.+' : segment } /^#{pattern.join('\\.')}$/ end |
Instance Method Details
#matches?(addr) ⇒ Boolean
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dizby/access/entry.rb', line 27 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 |