Class: MiniDefender::Rules::Ipv6

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/ipv6.rb

Constant Summary collapse

MODES =
%w[any public private]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #coerce, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message

Constructor Details

#initialize(mode = 'any') ⇒ Ipv6

Returns a new instance of Ipv6.

Raises:

  • (ArgumentError)


8
9
10
11
12
# File 'lib/mini_defender/rules/ipv6.rb', line 8

def initialize(mode = 'any')
  raise ArgumentError, 'Invalid mode' unless MODES.include?(mode)

  @mode = mode
end

Class Method Details

.make(args) ⇒ Object



18
19
20
# File 'lib/mini_defender/rules/ipv6.rb', line 18

def self.make(args)
  new(args[0] || 'any')
end

.signatureObject



14
15
16
# File 'lib/mini_defender/rules/ipv6.rb', line 14

def self.signature
  'ipv6'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/mini_defender/rules/ipv6.rb', line 33

def message(attribute, value, validator)
  case @mode
  when 'public'
    "The value must be a valid public IPv6 address."
  when 'private'
    "The value must be a valid private IPv6 address."
  else
    "The value must be a valid IPv6 address."
  end
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



22
23
24
25
26
27
28
29
30
31
# File 'lib/mini_defender/rules/ipv6.rb', line 22

def passes?(attribute, value, validator)
  ip = IPAddr.new(value.to_s)
  ip.ipv6? && (
    @mode == 'any' ||
    @mode == 'private' && (ip.private? || ip.link_local? || ip.loopback?) ||
    @mode == 'public' && !(ip.private? || ip.link_local? || ip.loopback?)
  )
rescue IPAddr::InvalidAddressError
  false
end