Sha256: 0ddc10b7ec71a0ebca40ccf3013b207473c147a09436165e2a69c855714101df

Contents?: true

Size: 654 Bytes

Versions: 14

Compression:

Stored size: 654 Bytes

Contents

# frozen_string_literal: true

module Validators
  class Hostname
    def self.valid?(host)
      host = host.to_s
      uri = URI(host)

      host.present? &&
        uri.host.nil? &&
        uri.scheme.nil? &&
        uri.fragment.nil? &&
        uri.query.nil? &&
        uri.path == host &&
        host.split(".").all? {|label| valid_label?(label) } &&
        host.size <= 255 &&
        Validators::TLD.host_with_valid_tld?(host)
    rescue URI::InvalidURIError
      false
    end

    def self.valid_label?(label)
      !label.start_with?("-") &&
        !label.match(/\A\d+\z/) &&
        label.match(/\A[a-z0-9-]{1,63}\z/i)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
validators-3.4.2 lib/validators/hostname.rb
validators-3.4.1 lib/validators/hostname.rb
validators-3.4.0 lib/validators/hostname.rb
validators-3.3.0 lib/validators/hostname.rb
validators-3.2.1 lib/validators/hostname.rb
validators-3.2.0 lib/validators/hostname.rb
validators-3.1.1 lib/validators/hostname.rb
validators-3.1.0 lib/validators/hostname.rb
validators-3.0.5 lib/validators/hostname.rb
validators-3.0.4 lib/validators/hostname.rb
validators-3.0.3 lib/validators/hostname.rb
validators-3.0.2 lib/validators/hostname.rb
validators-3.0.1 lib/validators/hostname.rb
validators-3.0.0 lib/validators/hostname.rb