Module: Legion::Apollo::Helpers::TagNormalizer

Extended by:
Logging::Helper
Defined in:
lib/legion/apollo/helpers/tag_normalizer.rb

Overview

Pure-function tag normalization: lowercase, strip invalid chars, dedup, truncate.

Constant Summary collapse

MAX_TAG_LENGTH =
64
MAX_TAGS =
20

Class Method Summary collapse

Class Method Details

.normalize(tags) ⇒ Object

rubocop:disable Metrics/MethodLength



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/apollo/helpers/tag_normalizer.rb', line 17

def normalize(tags) # rubocop:disable Metrics/MethodLength
  return [] unless tags.is_a?(Array)

  tags
    .map { |t| normalize_one(t) }
    .compact
    .uniq
    .first(MAX_TAGS)
rescue StandardError => e
  handle_exception(
    e,
    level:     :debug,
    operation: 'apollo.helpers.tag_normalizer.normalize',
    tag_count: Array(tags).size
  )
  []
end

.normalize_one(tag) ⇒ Object

rubocop:disable Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/apollo/helpers/tag_normalizer.rb', line 35

def normalize_one(tag) # rubocop:disable Metrics/MethodLength
  return nil if tag.nil?

  normalized = tag.to_s.strip.downcase.gsub(/[^a-z0-9_:-]/, '_').squeeze('_')
  normalized = normalized[0, MAX_TAG_LENGTH] if normalized.length > MAX_TAG_LENGTH
  normalized.empty? ? nil : normalized
rescue StandardError => e
  handle_exception(
    e,
    level:     :debug,
    operation: 'apollo.helpers.tag_normalizer.normalize_one',
    tag_class: tag.class.to_s
  )
  nil
end