Class: ValidateHostname

Inherits:
Object
  • Object
show all
Defined in:
lib/valid_hostname/validate_hostname.rb

Class Method Summary collapse

Class Method Details

.allowed_characters(allow_underscore: false) ⇒ Object



12
13
14
15
16
# File 'lib/valid_hostname/validate_hostname.rb', line 12

def allowed_characters(allow_underscore: false)
  valid_chars = +'a-z0-9\-'
  valid_chars << '_' if allow_underscore
  valid_chars
end

.allowed_tldsObject



5
6
7
8
9
10
# File 'lib/valid_hostname/validate_hostname.rb', line 5

def allowed_tlds
  @allowed_tlds ||= begin
    config = File.expand_path '../../config/valid_hostname.yml', __dir__
    YAML.load_file(config)['allowed_tlds']
  end
end

.default_optionsObject

use for valid?



19
20
21
22
23
24
25
26
# File 'lib/valid_hostname/validate_hostname.rb', line 19

def default_options
  { allow_underscore: false,
    require_valid_tld: false,
    allow_numeric_hostname: false,
    allow_wildcard_hostname: false,
    allow_root_label: false,
    verbose: true }
end

.valid?(value, user_options = nil) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/valid_hostname/validate_hostname.rb', line 28

def valid?(value, user_options = nil)
  value ||= ''

  # works with ruby 2.7 and => 3
  options = ValidateHostname.default_options.merge(user_options || {})

  valid_length?(value) &&
    valid_label_length?(value) &&
    valid_hyphens?(value) &&
    valid_dots?(value) &&
    valid_trailing_dot?(value,
                        allow_root_label: options[:allow_root_label]) &&
    valid_characters?(value,
                      allow_underscore: options[:allow_underscore],
                      allow_wildcard_hostname: options[:allow_wildcard_hostname]) &&
    valid_numeric_only?(value,
                        allow_numeric_hostname: options[:allow_numeric_hostname]) &&
    valid_tld?(value,
               valid_tlds: nil, require_valid_tld: options[:require_valid_tld])
end

.valid_characters?(value, allow_underscore: false, allow_wildcard_hostname: false) ⇒ Boolean

hostname can only contain characters: a-z, 0-9, hyphen, optional underscore, optional asterisk

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/valid_hostname/validate_hostname.rb', line 77

def valid_characters?(value, allow_underscore: false, allow_wildcard_hostname: false)
  return true unless check_string value

  valid_chars = allowed_characters allow_underscore: allow_underscore
  labels = value.split '.'
  labels.each_with_index do |label, index|
    # Take care of wildcard first label
    next if allow_wildcard_hostname && label == '*' && index.zero?
    next if /\A[#{valid_chars}]+\z/i.match?(label)

    return false
  end

  true
end

.valid_dots?(value) ⇒ Boolean

hostname may not contain consecutive dots

Returns:

  • (Boolean)


103
104
105
# File 'lib/valid_hostname/validate_hostname.rb', line 103

def valid_dots?(value)
  !/\.\./.match?(value)
end

.valid_hyphens?(value) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/valid_hostname/validate_hostname.rb', line 66

def valid_hyphens?(value)
  labels = value.split '.'
  labels.each do |label|
    return false if label =~ /^-/i || label =~ /-$/
  end

  true
end

.valid_label_length?(value) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
# File 'lib/valid_hostname/validate_hostname.rb', line 55

def valid_label_length?(value)
  return false unless check_string value

  labels = value.split '.'
  labels.each do |label|
    return false unless label.length.between? 1, 63
  end

  true
end

.valid_length?(value) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/valid_hostname/validate_hostname.rb', line 49

def valid_length?(value)
  return false unless check_string value

  value.length.between? 1, 255
end

.valid_numeric_only?(value, allow_numeric_hostname: false) ⇒ Boolean

the unqualified hostname portion cannot consist of numeric values only

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# File 'lib/valid_hostname/validate_hostname.rb', line 94

def valid_numeric_only?(value, allow_numeric_hostname: false)
  return true if allow_numeric_hostname == true
  return true unless check_string value

  first_label = value.split('.').first
  first_label !~ /\A\d+\z/
end

.valid_tld?(value, valid_tlds: nil, require_valid_tld: true) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
124
# File 'lib/valid_hostname/validate_hostname.rb', line 114

def valid_tld?(value, valid_tlds: nil, require_valid_tld: true)
  return true if !check_string(value) || value == '.'
  return true if require_valid_tld != true

  labels = value.split '.'
  return true if labels.count.zero?
  return false if valid_tlds && valid_tlds.empty?

  valid_tlds = valid_tlds.nil? ? allowed_tlds : valid_tlds.map(&:downcase)
  valid_tlds.include? labels.last.downcase
end

.valid_trailing_dot?(value, allow_root_label: false) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
# File 'lib/valid_hostname/validate_hostname.rb', line 107

def valid_trailing_dot?(value, allow_root_label: false)
  return true if allow_root_label != false
  return true unless check_string value

  value[-1] != '.'
end