Class: Iriq::SegmentClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/iriq/segment_classifier.rb

Overview

Heuristic classifier for individual path segments and query values.

Returns a symbol from the known TYPES set. Order matters: the first matching rule wins.

Constant Summary collapse

TYPES =
%i[literal integer_id uuid date timestamp hash slug opaque_id].freeze
UUID_RE =
/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/.freeze
INTEGER_RE =
/\A\d+\z/.freeze
DATE_RE =
/\A\d{4}-\d{2}-\d{2}\z/.freeze
ISO_TIME_RE =
/\A\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(:\d{2})?(\.\d+)?(Z|[+\-]\d{2}:?\d{2})?\z/.freeze
HASH_RE =
/\A\h{32,}\z/.freeze
SLUG_RE =
/\A[a-z0-9]+(?:[-_][a-z0-9]+)+\z/.freeze
LITERAL_RE =
/\A[\p{L}][\p{L}\p{M}_]*\z/u.freeze
OPAQUE_RE =
/\A[A-Za-z0-9_\-.~]{4,}\z/.freeze
TS_SECONDS_RANGE =

Plausible UNIX timestamps (10 digit seconds or 13 digit ms) from roughly 2001 onward.

1_000_000_000..9_999_999_999
TS_MILLIS_RANGE =
1_000_000_000_000..9_999_999_999_999
CACHE_MAX =

Bounded memoization: classification of a given string is pure, so repeat segments (e.g. /users in countless paths) can be cached. Cap keeps the cache from unbounded growth when inputs are dominated by unique IDs.

10_000
DEFAULT =

Shared singleton — preferred default for callers that don’t bring their own classifier (saves a per-call allocation).

new

Instance Method Summary collapse

Constructor Details

#initializeSegmentClassifier

Returns a new instance of SegmentClassifier.



29
30
31
# File 'lib/iriq/segment_classifier.rb', line 29

def initialize
  @cache = {}
end

Instance Method Details

#classify(segment) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/iriq/segment_classifier.rb', line 33

def classify(segment)
  return :literal if segment.nil? || segment.empty?

  cached = @cache[segment]
  return cached if cached

  @cache.clear if @cache.size >= CACHE_MAX
  @cache[segment] = compute_classification(segment)
end

#variable?(type) ⇒ Boolean

Anything except :literal is considered variable for shape/explain.

Returns:

  • (Boolean)


44
45
46
# File 'lib/iriq/segment_classifier.rb', line 44

def variable?(type)
  type != :literal
end