Class: Validator::IpAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/validator/ip_address.rb

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ IpAddress

Returns a new instance of IpAddress.



5
6
7
# File 'lib/validator/ip_address.rb', line 5

def initialize(value)
  @value = value
end

Instance Method Details

#has_prefix?Boolean

check if ip has prefix

Returns:

  • (Boolean)


10
11
12
# File 'lib/validator/ip_address.rb', line 10

def has_prefix?
  @value =~ /\//
end

#is_ipv4?Boolean

IPv4 determination by . (dot)

Returns:

  • (Boolean)


27
28
29
# File 'lib/validator/ip_address.rb', line 27

def is_ipv4?
  @value =~ /\./
end

#valid?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/validator/ip_address.rb', line 47

def valid?
  valid_ipv4? || valid_ipv6?
end

#valid_ipv4?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/validator/ip_address.rb', line 39

def valid_ipv4?
  IPAddress(@value).ipv4? rescue false
end

#valid_ipv4_prefix?Boolean

IPv4 addresses which are only 32 bits long

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/validator/ip_address.rb', line 15

def valid_ipv4_prefix?
  prefix = get_prefix
  (prefix >= 1 and prefix <= 32)
end

#valid_ipv6?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/validator/ip_address.rb', line 43

def valid_ipv6?
  IPAddress(@value).ipv6? rescue false
end

#valid_ipv6_prefix?Boolean

IPv6 addresses are 128 bits long

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/validator/ip_address.rb', line 21

def valid_ipv6_prefix?
  prefix = get_prefix
  (prefix >= 1 and prefix <= 128)
end

#valid_prefix?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/validator/ip_address.rb', line 31

def valid_prefix?
  if is_ipv4?
    valid_ipv4_prefix?
  else
    valid_ipv6_prefix?
  end
end