class UniversalValidators::IpValidator

Public Class Methods

new(ip, mask_presence = false) click to toggle source
# File lib/universal_validators/ip_validator.rb, line 3
def initialize(ip, mask_presence = false)
  @ip = ip.to_s
  @mask_presence = mask_presence
end

Public Instance Methods

valid?() click to toggle source
# 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

Private Instance Methods

check_format_with_netmask() click to toggle source
# File lib/universal_validators/ip_validator.rb, line 25
def check_format_with_netmask
  fail ArgumentError unless @ip.match(/\A[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}\Z/)
  fail ArgumentError if @ip.split('/')[1].to_i > 32
end
check_format_without_netmask() click to toggle source
# File lib/universal_validators/ip_validator.rb, line 21
def check_format_without_netmask
  fail ArgumentError unless @ip.match(/\A[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\Z/)
end