Class: MiniDefender::Rules::Ip
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') ⇒ Ip
Returns a new instance of Ip.
8
9
10
11
12
|
# File 'lib/mini_defender/rules/ip.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/ip.rb', line 18
def self.make(args)
new(args[0] || 'any')
end
|
.signature ⇒ Object
14
15
16
|
# File 'lib/mini_defender/rules/ip.rb', line 14
def self.signature
'ip'
end
|
Instance Method Details
#message(attribute, value, validator) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/mini_defender/rules/ip.rb', line 33
def message(attribute, value, validator)
case @mode
when 'public'
"The value must be a valid public IP address."
when 'private'
"The value must be a valid private IP address."
else
"The value must be a valid IP address."
end
end
|
#passes?(attribute, value, validator) ⇒ Boolean
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/mini_defender/rules/ip.rb', line 22
def passes?(attribute, value, validator)
ip = IPAddr.new(value.to_s)
(
@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
|