Class: Validators::Hostname

Inherits:
Object
  • Object
show all
Defined in:
lib/validators/hostname.rb

Class Method Summary collapse

Class Method Details

.valid?(host) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/validators/hostname.rb', line 3

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

.valid_label?(label) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/validators/hostname.rb', line 20

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