Class: Validators::ReservedSubdomains

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

Constant Summary collapse

FILE_PATH =
File.expand_path("../../data/reserved_subdomains.txt", __dir__)

Class Method Summary collapse

Class Method Details

.allObject



13
14
15
# File 'lib/validators/reserved_subdomains.rb', line 13

def self.all
  @all ||= File.read(FILE_PATH).lines.map {|matcher| parse(matcher.chomp) }
end

.match?(matcher, hostname) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
# File 'lib/validators/reserved_subdomains.rb', line 36

def self.match?(matcher, hostname)
  case matcher
  when String
    matcher == hostname
  when Regexp
    hostname =~ matcher
  else
    raise "Unknown matcher type: #{matcher.class}"
  end
end

.match_any?(matchers, hostname) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/validators/reserved_subdomains.rb', line 27

def self.match_any?(matchers, hostname)
  hostname = normalize(hostname)
  matchers.any? {|matcher| match?(matcher, hostname) }
end

.normalize(hostname) ⇒ Object



32
33
34
# File 'lib/validators/reserved_subdomains.rb', line 32

def self.normalize(hostname)
  hostname.downcase.gsub(/[_-]/, "")
end

.parse(matcher) ⇒ Object



17
18
19
20
21
# File 'lib/validators/reserved_subdomains.rb', line 17

def self.parse(matcher)
  return matcher unless matcher.start_with?("/")

  Regexp.compile(matcher[%r{/(.*?)/}, 1])
end

.parse_list(matchers) ⇒ Object



23
24
25
# File 'lib/validators/reserved_subdomains.rb', line 23

def self.parse_list(matchers)
  matchers.map {|matcher| parse(matcher) }
end

.reserved?(hostname, matchers = nil) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
# File 'lib/validators/reserved_subdomains.rb', line 7

def self.reserved?(hostname, matchers = nil)
  matchers = parse_list(matchers) if matchers
  matchers ||= all
  match_any?(matchers, hostname)
end