Sha256: a18fef6c1c40ff319717956efe7f7f1700e9108a2d6f2c0abcfa770287bd7ea6

Contents?: true

Size: 926 Bytes

Versions: 20

Compression:

Stored size: 926 Bytes

Contents

module Validators
  module Ip
    extend self

    # Extracted from Ruby 1.8.7
    def v4?(addr)
      matches = addr.match(/\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/)
      matches && matches.captures.all? {|i| i.to_i < 256}
    end

    # Extracted from Ruby 1.8.7
    def v6?(addr)
      # IPv6 (normal)
      return true if /\A[\da-f]{1,4}(:[\da-f]{1,4})*\Z/i =~ addr
      return true if /\A[\da-f]{1,4}(:[\da-f]{1,4})*::([\da-f]{1,4}(:[\da-f]{1,4})*)?\Z/i =~ addr
      return true if /\A::([\da-f]{1,4}(:[\da-f]{1,4})*)?\Z/i =~ addr
      # IPv6 (IPv4 compat)
      return true if /\A[\da-f]{1,4}(:[\da-f]{1,4})*:/i =~ addr && v4?($')
      return true if /\A[\da-f]{1,4}(:[\da-f]{1,4})*::([\da-f]{1,4}(:[\da-f]{1,4})*:)?/i =~ addr && v4?($')
      return true if /\A::([\da-f]{1,4}(:[\da-f]{1,4})*:)?/i =~ addr && v4?($')

      false
    end

    def valid?(addr)
      v4?(addr) || v6?(addr)
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
validators-2.8.1 lib/validators/ip.rb
validators-2.8.0 lib/validators/ip.rb
validators-2.7.0 lib/validators/ip.rb
validators-2.6.0 lib/validators/ip.rb
validators-2.5.4 lib/validators/ip.rb
validators-2.5.3 lib/validators/ip.rb
validators-2.5.2 lib/validators/ip.rb
validators-2.5.1 lib/validators/ip.rb
validators-2.5.0 lib/validators/ip.rb
validators-2.4.2 lib/validators/ip.rb
validators-2.4.1 lib/validators/ip.rb
validators-2.4.0 lib/validators/ip.rb
validators-2.3.0 lib/validators/ip.rb
validators-2.2.0 lib/validators/ip.rb
validators-2.1.0 lib/validators/ip.rb
validators-2.0.0 lib/validators/ip.rb
validators-1.0.0 lib/validators/ip.rb
validators-0.1.3 lib/validators/ip.rb
validators-0.1.2 lib/validators/ip.rb
validators-0.1.1 lib/validators/ip.rb