Class: MiniDefender::Rules::Ipv4
Constant Summary
collapse
- MODES =
%w[any public private]
Class Method Summary
collapse
Instance Method Summary
collapse
#active?, available?, #bails?, #coerce, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message
Constructor Details
#initialize(mode = 'any') ⇒ Ipv4
Returns a new instance of Ipv4.
8
9
10
11
12
|
# File 'lib/mini_defender/rules/ipv4.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/ipv4.rb', line 18
def self.make(args)
new(args[0] || 'any')
end
|
.signature ⇒ Object
14
15
16
|
# File 'lib/mini_defender/rules/ipv4.rb', line 14
def self.signature
'ipv4'
end
|
Instance Method Details
#message(attribute, value, validator) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/mini_defender/rules/ipv4.rb', line 33
def message(attribute, value, validator)
case @mode
when 'public'
"The value must be a valid public IPv4 address."
when 'private'
"The value must be a valid private IPv4 address."
else
"The value must be a valid IPv4 address."
end
end
|
#passes?(attribute, value, validator) ⇒ Boolean
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/mini_defender/rules/ipv4.rb', line 22
def passes?(attribute, value, validator)
ip = IPAddr.new(value.to_s)
ip.ipv4? && (
@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
|