Module: Botiasloop::HumanId

Defined in:
lib/botiasloop/human_id.rb

Overview

Generates human-readable identifiers for conversations Format: color-animal-XXX (e.g., blue-dog-123) All IDs are stored and compared in lowercase for case-insensitivity

Constant Summary collapse

MAX_RETRIES =
10

Class Method Summary collapse

Class Method Details

.build_idString

Build a single ID attempt

Returns:

  • (String)

    ID in format color-animal-XXX



50
51
52
53
54
55
56
# File 'lib/botiasloop/human_id.rb', line 50

def self.build_id
  color = FFaker::Color.name.downcase.tr(" ", "-")
  animal = FFaker::AnimalUS.common_name.downcase.tr(" ", "-")
  number = rand(100..999)

  "#{color}-#{animal}-#{number}"
end

.exists?(id) ⇒ Boolean

Check if an ID already exists in the database Case-insensitive comparison

Parameters:

  • id (String)

    ID to check

Returns:

  • (Boolean)

    True if ID exists



42
43
44
45
# File 'lib/botiasloop/human_id.rb', line 42

def self.exists?(id)
  normalized = normalize(id)
  Conversation.where(Sequel.function(:lower, :id) => normalized).count > 0
end

.generateString

Generate a unique human-readable ID Checks database for collisions and retries if needed

Returns:

  • (String)

    Unique ID in format color-animal-XXX

Raises:

  • (Error)

    If unable to generate unique ID after max retries



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/botiasloop/human_id.rb', line 17

def self.generate
  retries = 0

  loop do
    id = build_id
    return id unless exists?(id)

    retries += 1
    raise Error, "Failed to generate unique ID after #{MAX_RETRIES} attempts" if retries >= MAX_RETRIES
  end
end

.normalize(id) ⇒ String

Normalize an ID to lowercase for storage and comparison

Parameters:

  • id (String, nil)

    ID to normalize

Returns:

  • (String)

    Normalized lowercase ID



33
34
35
# File 'lib/botiasloop/human_id.rb', line 33

def self.normalize(id)
  id.to_s.downcase.strip
end