Class: Validators::ReservedSubdomains
- Inherits:
-
Object
- Object
- Validators::ReservedSubdomains
- Defined in:
- lib/validators/reserved_subdomains.rb
Constant Summary collapse
- FILE_PATH =
File.("../../data/reserved_subdomains.json", __dir__)
Class Method Summary collapse
- .all ⇒ Object
- .match?(matcher, hostname) ⇒ Boolean
- .match_any?(matchers, hostname) ⇒ Boolean
- .normalize(hostname) ⇒ Object
- .parse(matcher) ⇒ Object
- .parse_list(matchers) ⇒ Object
- .reserved?(hostname, matchers = nil) ⇒ Boolean
Class Method Details
.all ⇒ Object
13 14 15 16 17 |
# File 'lib/validators/reserved_subdomains.rb', line 13 def self.all @all ||= JSON .parse(File.read(FILE_PATH)) .map {|matcher| parse(matcher) } end |
.match?(matcher, hostname) ⇒ Boolean
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/validators/reserved_subdomains.rb', line 38 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
29 30 31 32 |
# File 'lib/validators/reserved_subdomains.rb', line 29 def self.match_any?(matchers, hostname) hostname = normalize(hostname) matchers.any? {|matcher| match?(matcher, hostname) } end |
.normalize(hostname) ⇒ Object
34 35 36 |
# File 'lib/validators/reserved_subdomains.rb', line 34 def self.normalize(hostname) hostname.downcase.gsub(/[_-]/, "") end |
.parse(matcher) ⇒ Object
19 20 21 22 23 |
# File 'lib/validators/reserved_subdomains.rb', line 19 def self.parse(matcher) return matcher unless matcher.start_with?("/") Regexp.compile(matcher[%r{/(.*?)/}, 1]) end |
.parse_list(matchers) ⇒ Object
25 26 27 |
# File 'lib/validators/reserved_subdomains.rb', line 25 def self.parse_list(matchers) matchers.map {|matcher| parse(matcher) } end |
.reserved?(hostname, matchers = nil) ⇒ 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 |