Module: Kameleoon::Utils::Domain

Defined in:
lib/kameleoon/utils.rb

Constant Summary collapse

HTTP =
'http://'
HTTPS =
'https://'
REGEX_DOMAIN =
/^(\.?(([a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])|[a-zA-Z\d]))
(\.(([a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])|[a-zA-Z\d])){1,126}$/x.freeze
LOCALHOST =
'localhost'

Class Method Summary collapse

Class Method Details

.validate_top_level_domain(top_level_domain) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kameleoon/utils.rb', line 80

def self.validate_top_level_domain(top_level_domain)
  return nil if top_level_domain.nil? || top_level_domain.empty?

  top_level_domain = top_level_domain.downcase

  [HTTP, HTTPS].each do |protocol|
    next unless top_level_domain.start_with?(protocol)

    top_level_domain = top_level_domain[protocol.length..]
    Logging::KameleoonLogger.warning(
      "The top-level domain contains '%s'. Domain after protocol trimming: '%s'", protocol, top_level_domain
    )
    break
  end

  if !REGEX_DOMAIN.match?(top_level_domain) && top_level_domain != LOCALHOST
    Logging::KameleoonLogger.error(
      "The top-level domain '%s' is invalid. The value has been set as provided, but it does not meet " \
        'the required format for proper SDK functionality. Please check the domain for correctness.',
      top_level_domain
    )
    return top_level_domain
  end

  top_level_domain
end