Class: UniversalValidators::IpValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/universal_validators/ip_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(ip, mask_presence = false) ⇒ IpValidator

Returns a new instance of IpValidator.



3
4
5
6
# File 'lib/universal_validators/ip_validator.rb', line 3

def initialize(ip, mask_presence = false)
  @ip = ip.to_s
  @mask_presence = mask_presence
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
# File 'lib/universal_validators/ip_validator.rb', line 8

def valid?
  begin
    check_format_with_netmask if @mask_presence
    check_format_without_netmask unless @mask_presence
  rescue
    return
  end

  @ip.split('.').select { |pair| pair.to_i > 255 }.length == 0
end