Module: OpaqueId
- Defined in:
- lib/opaque_id.rb,
lib/opaque_id/model.rb,
lib/opaque_id/version.rb,
lib/opaque_id/configuration.rb,
lib/generators/opaque_id/install_generator.rb
Defined Under Namespace
Modules: Generators, Model Classes: Configuration, ConfigurationError, Error, GenerationError
Constant Summary collapse
- SLUG_LIKE_ALPHABET =
Slug-like alphabet for URL-safe, double-click selectable IDs (36 characters)
'0123456789abcdefghijklmnopqrstuvwxyz'- STANDARD_ALPHABET =
Standard URL-safe alphabet (64 characters)
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'- ALPHANUMERIC_ALPHABET =
Alphanumeric alphabet (62 characters)
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'- VERSION =
'2.0.0'
Class Method Summary collapse
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.generate(size: 18, alphabet: SLUG_LIKE_ALPHABET) ⇒ Object
Generate a cryptographically secure random ID.
- .reset_configuration! ⇒ Object
Class Method Details
.configuration ⇒ Object
23 24 25 |
# File 'lib/opaque_id.rb', line 23 def configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
27 28 29 |
# File 'lib/opaque_id.rb', line 27 def configure yield(configuration) end |
.generate(size: 18, alphabet: SLUG_LIKE_ALPHABET) ⇒ Object
Generate a cryptographically secure random ID
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/opaque_id.rb', line 36 def generate(size: 18, alphabet: SLUG_LIKE_ALPHABET) raise ConfigurationError, 'Size must be positive' unless size.positive? raise ConfigurationError, 'Alphabet cannot be empty' if alphabet.nil? || alphabet.empty? alphabet_size = alphabet.size # Handle edge case: single character alphabet return alphabet * size if alphabet_size == 1 return generate_fast(size, alphabet) if alphabet_size == 64 generate_unbiased(size, alphabet, alphabet_size) end |
.reset_configuration! ⇒ Object
31 32 33 |
# File 'lib/opaque_id.rb', line 31 def reset_configuration! @configuration = Configuration.new end |