opaque_id 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/docs/use-cases.md ADDED
@@ -0,0 +1,569 @@
1
+ ---
2
+ layout: default
3
+ title: Use Cases
4
+ nav_order: 10
5
+ description: "Real-world examples and applications for OpaqueId"
6
+ permalink: /use-cases/
7
+ ---
8
+
9
+ # Use Cases
10
+
11
+ OpaqueId is designed for a wide range of applications where secure, unpredictable identifiers are needed. This guide covers real-world use cases with practical examples and implementation patterns.
12
+
13
+ ## E-Commerce Applications
14
+
15
+ ### Order Management
16
+
17
+ E-commerce platforms need secure order identifiers that don't reveal business information.
18
+
19
+ ```ruby
20
+ class Order < ApplicationRecord
21
+ include OpaqueId::Model
22
+
23
+ # Short, numeric order numbers for customer service
24
+ self.opaque_id_column = :order_number
25
+ self.opaque_id_length = 8
26
+ self.opaque_id_alphabet = "0123456789"
27
+ end
28
+
29
+ # Usage
30
+ order = Order.create!(user_id: 1, total: 99.99)
31
+ puts order.order_number
32
+ # => "12345678"
33
+
34
+ # Customer service can reference orders easily
35
+ # "Please provide order number 12345678"
36
+ ```
37
+
38
+ ### Product Management
39
+
40
+ Products need secure identifiers for inventory and catalog management.
41
+
42
+ ```ruby
43
+ class Product < ApplicationRecord
44
+ include OpaqueId::Model
45
+
46
+ # Medium-length alphanumeric SKUs
47
+ self.opaque_id_column = :sku
48
+ self.opaque_id_length = 12
49
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
50
+ end
51
+
52
+ # Usage
53
+ product = Product.create!(name: "Wireless Headphones", price: 199.99)
54
+ puts product.sku
55
+ # => "V1StGXR8_Z5j"
56
+
57
+ # Inventory management
58
+ # "SKU V1StGXR8_Z5j is out of stock"
59
+ ```
60
+
61
+ ### Customer Management
62
+
63
+ Customer accounts need secure identifiers for privacy and security.
64
+
65
+ ```ruby
66
+ class Customer < ApplicationRecord
67
+ include OpaqueId::Model
68
+
69
+ # Standard length for customer IDs
70
+ self.opaque_id_length = 21
71
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
72
+ end
73
+
74
+ # Usage
75
+ customer = Customer.create!(name: "John Doe", email: "john@example.com")
76
+ puts customer.opaque_id
77
+ # => "V1StGXR8_Z5jdHi6B-myT"
78
+
79
+ # Customer support
80
+ # "Customer ID V1StGXR8_Z5jdHi6B-myT has an issue with order 12345678"
81
+ ```
82
+
83
+ ## API Development
84
+
85
+ ### API Key Management
86
+
87
+ APIs need secure keys for authentication and authorization.
88
+
89
+ ```ruby
90
+ class ApiKey < ApplicationRecord
91
+ include OpaqueId::Model
92
+
93
+ # Long, secure API keys
94
+ self.opaque_id_length = 32
95
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
96
+ self.opaque_id_require_letter_start = true
97
+ end
98
+
99
+ # Usage
100
+ api_key = ApiKey.create!(user_id: 1, name: "Mobile App")
101
+ puts api_key.opaque_id
102
+ # => "V1StGXR8_Z5jdHi6B-myT1234567890"
103
+
104
+ # API authentication
105
+ # Authorization: Bearer V1StGXR8_Z5jdHi6B-myT1234567890
106
+ ```
107
+
108
+ ### API Token Management
109
+
110
+ Short-lived tokens for session management and temporary access.
111
+
112
+ ```ruby
113
+ class ApiToken < ApplicationRecord
114
+ include OpaqueId::Model
115
+
116
+ # Medium-length tokens
117
+ self.opaque_id_column = :token
118
+ self.opaque_id_length = 24
119
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
120
+ end
121
+
122
+ # Usage
123
+ token = ApiToken.create!(user_id: 1, expires_at: 1.hour.from_now)
124
+ puts token.token
125
+ # => "V1StGXR8_Z5jdHi6B-myT123"
126
+
127
+ # API requests
128
+ # Authorization: Token V1StGXR8_Z5jdHi6B-myT123
129
+ ```
130
+
131
+ ### Webhook Management
132
+
133
+ Webhooks need secure identifiers for verification and tracking.
134
+
135
+ ```ruby
136
+ class Webhook < ApplicationRecord
137
+ include OpaqueId::Model
138
+
139
+ # Medium-length webhook IDs
140
+ self.opaque_id_length = 16
141
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
142
+ end
143
+
144
+ # Usage
145
+ webhook = Webhook.create!(url: "https://example.com/webhook", events: ["order.created"])
146
+ puts webhook.opaque_id
147
+ # => "V1StGXR8_Z5jdHi"
148
+
149
+ # Webhook delivery
150
+ # POST https://example.com/webhook
151
+ # X-Webhook-ID: V1StGXR8_Z5jdHi
152
+ ```
153
+
154
+ ## Content Management Systems
155
+
156
+ ### Article Management
157
+
158
+ Articles need URL-friendly identifiers for SEO and sharing.
159
+
160
+ ```ruby
161
+ class Article < ApplicationRecord
162
+ include OpaqueId::Model
163
+
164
+ # Short, URL-safe slugs
165
+ self.opaque_id_column = :slug
166
+ self.opaque_id_length = 8
167
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
168
+ end
169
+
170
+ # Usage
171
+ article = Article.create!(title: "Getting Started with OpaqueId", content: "...")
172
+ puts article.slug
173
+ # => "V1StGXR8"
174
+
175
+ # URL generation
176
+ # https://example.com/articles/V1StGXR8
177
+ ```
178
+
179
+ ### Comment Management
180
+
181
+ Comments need secure identifiers for moderation and tracking.
182
+
183
+ ```ruby
184
+ class Comment < ApplicationRecord
185
+ include OpaqueId::Model
186
+
187
+ # Standard length for comments
188
+ self.opaque_id_length = 15
189
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
190
+ end
191
+
192
+ # Usage
193
+ comment = Comment.create!(article_id: 1, user_id: 1, content: "Great article!")
194
+ puts comment.opaque_id
195
+ # => "V1StGXR8_Z5jdHi"
196
+
197
+ # Comment moderation
198
+ # "Comment V1StGXR8_Z5jdHi needs review"
199
+ ```
200
+
201
+ ### Media Management
202
+
203
+ Media files need secure identifiers for access control and tracking.
204
+
205
+ ```ruby
206
+ class MediaFile < ApplicationRecord
207
+ include OpaqueId::Model
208
+
209
+ # Medium-length media IDs
210
+ self.opaque_id_length = 18
211
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
212
+ end
213
+
214
+ # Usage
215
+ media = MediaFile.create!(filename: "image.jpg", size: 1024000)
216
+ puts media.opaque_id
217
+ # => "V1StGXR8_Z5jdHi6B-my"
218
+
219
+ # Media access
220
+ # https://example.com/media/V1StGXR8_Z5jdHi6B-my
221
+ ```
222
+
223
+ ## User Management
224
+
225
+ ### User Accounts
226
+
227
+ User accounts need secure identifiers for privacy and security.
228
+
229
+ ```ruby
230
+ class User < ApplicationRecord
231
+ include OpaqueId::Model
232
+
233
+ # Standard length for user IDs
234
+ self.opaque_id_length = 21
235
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
236
+ end
237
+
238
+ # Usage
239
+ user = User.create!(name: "John Doe", email: "john@example.com")
240
+ puts user.opaque_id
241
+ # => "V1StGXR8_Z5jdHi6B-myT"
242
+
243
+ # User profile URLs
244
+ # https://example.com/users/V1StGXR8_Z5jdHi6B-myT
245
+ ```
246
+
247
+ ### User Sessions
248
+
249
+ Sessions need secure identifiers for tracking and management.
250
+
251
+ ```ruby
252
+ class UserSession < ApplicationRecord
253
+ include OpaqueId::Model
254
+
255
+ # Long, secure session IDs
256
+ self.opaque_id_column = :session_id
257
+ self.opaque_id_length = 32
258
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
259
+ end
260
+
261
+ # Usage
262
+ session = UserSession.create!(user_id: 1, expires_at: 30.days.from_now)
263
+ puts session.session_id
264
+ # => "V1StGXR8_Z5jdHi6B-myT1234567890"
265
+
266
+ # Session management
267
+ # "Session V1StGXR8_Z5jdHi6B-myT1234567890 expired"
268
+ ```
269
+
270
+ ### User Preferences
271
+
272
+ User preferences need secure identifiers for privacy.
273
+
274
+ ```ruby
275
+ class UserPreference < ApplicationRecord
276
+ include OpaqueId::Model
277
+
278
+ # Medium-length preference IDs
279
+ self.opaque_id_length = 15
280
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
281
+ end
282
+
283
+ # Usage
284
+ preference = UserPreference.create!(user_id: 1, key: "theme", value: "dark")
285
+ puts preference.opaque_id
286
+ # => "V1StGXR8_Z5jdHi"
287
+
288
+ # Preference management
289
+ # "Preference V1StGXR8_Z5jdHi updated"
290
+ ```
291
+
292
+ ## Background Job Systems
293
+
294
+ ### Job Management
295
+
296
+ Background jobs need secure identifiers for tracking and debugging.
297
+
298
+ ```ruby
299
+ class BackgroundJob < ApplicationRecord
300
+ include OpaqueId::Model
301
+
302
+ # Medium-length job IDs
303
+ self.opaque_id_length = 18
304
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
305
+ end
306
+
307
+ # Usage
308
+ job = BackgroundJob.create!(job_type: "email_delivery", status: "pending")
309
+ puts job.opaque_id
310
+ # => "V1StGXR8_Z5jdHi6B-my"
311
+
312
+ # Job monitoring
313
+ # "Job V1StGXR8_Z5jdHi6B-my failed after 3 retries"
314
+ ```
315
+
316
+ ### Queue Management
317
+
318
+ Message queues need secure identifiers for message tracking.
319
+
320
+ ```ruby
321
+ class QueueMessage < ApplicationRecord
322
+ include OpaqueId::Model
323
+
324
+ # Medium-length message IDs
325
+ self.opaque_id_length = 16
326
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
327
+ end
328
+
329
+ # Usage
330
+ message = QueueMessage.create!(queue_name: "email_queue", payload: {...})
331
+ puts message.opaque_id
332
+ # => "V1StGXR8_Z5jdHi"
333
+
334
+ # Message processing
335
+ # "Message V1StGXR8_Z5jdHi processed successfully"
336
+ ```
337
+
338
+ ## Short URL Services
339
+
340
+ ### URL Shortening
341
+
342
+ Short URLs need compact, secure identifiers.
343
+
344
+ ```ruby
345
+ class ShortUrl < ApplicationRecord
346
+ include OpaqueId::Model
347
+
348
+ # Very short URLs for sharing
349
+ self.opaque_id_length = 6
350
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
351
+ end
352
+
353
+ # Usage
354
+ short_url = ShortUrl.create!(original_url: "https://example.com/very/long/url")
355
+ puts short_url.opaque_id
356
+ # => "V1StGX"
357
+
358
+ # Short URL generation
359
+ # https://short.ly/V1StGX
360
+ ```
361
+
362
+ ### URL Analytics
363
+
364
+ URL analytics need secure identifiers for tracking.
365
+
366
+ ```ruby
367
+ class UrlAnalytic < ApplicationRecord
368
+ include OpaqueId::Model
369
+
370
+ # Medium-length analytic IDs
371
+ self.opaque_id_length = 15
372
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
373
+ end
374
+
375
+ # Usage
376
+ analytic = UrlAnalytic.create!(short_url_id: 1, ip_address: "192.168.1.1")
377
+ puts analytic.opaque_id
378
+ # => "V1StGXR8_Z5jdHi"
379
+
380
+ # Analytics tracking
381
+ # "Analytic V1StGXR8_Z5jdHi recorded for URL V1StGX"
382
+ ```
383
+
384
+ ## Real-Time Applications
385
+
386
+ ### Chat Systems
387
+
388
+ Chat messages need secure identifiers for real-time communication.
389
+
390
+ ```ruby
391
+ class ChatMessage < ApplicationRecord
392
+ include OpaqueId::Model
393
+
394
+ # Medium-length message IDs
395
+ self.opaque_id_length = 18
396
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
397
+ end
398
+
399
+ # Usage
400
+ message = ChatMessage.create!(room_id: 1, user_id: 1, content: "Hello!")
401
+ puts message.opaque_id
402
+ # => "V1StGXR8_Z5jdHi6B-my"
403
+
404
+ # Real-time messaging
405
+ # "Message V1StGXR8_Z5jdHi6B-my sent to room 1"
406
+ ```
407
+
408
+ ### Notification Systems
409
+
410
+ Notifications need secure identifiers for delivery tracking.
411
+
412
+ ```ruby
413
+ class Notification < ApplicationRecord
414
+ include OpaqueId::Model
415
+
416
+ # Medium-length notification IDs
417
+ self.opaque_id_length = 16
418
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
419
+ end
420
+
421
+ # Usage
422
+ notification = Notification.create!(user_id: 1, type: "email", content: "New message")
423
+ puts notification.opaque_id
424
+ # => "V1StGXR8_Z5jdHi"
425
+
426
+ # Notification delivery
427
+ # "Notification V1StGXR8_Z5jdHi delivered to user 1"
428
+ ```
429
+
430
+ ## Analytics and Tracking
431
+
432
+ ### Event Tracking
433
+
434
+ Events need secure identifiers for analytics and tracking.
435
+
436
+ ```ruby
437
+ class Event < ApplicationRecord
438
+ include OpaqueId::Model
439
+
440
+ # Medium-length event IDs
441
+ self.opaque_id_length = 15
442
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
443
+ end
444
+
445
+ # Usage
446
+ event = Event.create!(user_id: 1, event_type: "page_view", properties: {...})
447
+ puts event.opaque_id
448
+ # => "V1StGXR8_Z5jdHi"
449
+
450
+ # Event tracking
451
+ # "Event V1StGXR8_Z5jdHi recorded for user 1"
452
+ ```
453
+
454
+ ### Conversion Tracking
455
+
456
+ Conversions need secure identifiers for attribution and analysis.
457
+
458
+ ```ruby
459
+ class Conversion < ApplicationRecord
460
+ include OpaqueId::Model
461
+
462
+ # Medium-length conversion IDs
463
+ self.opaque_id_length = 18
464
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
465
+ end
466
+
467
+ # Usage
468
+ conversion = Conversion.create!(user_id: 1, campaign_id: 1, value: 99.99)
469
+ puts conversion.opaque_id
470
+ # => "V1StGXR8_Z5jdHi6B-my"
471
+
472
+ # Conversion tracking
473
+ # "Conversion V1StGXR8_Z5jdHi6B-my attributed to campaign 1"
474
+ ```
475
+
476
+ ## Use Case Summary
477
+
478
+ ### Security Levels
479
+
480
+ | Use Case | ID Length | Alphabet | Security Level | Example |
481
+ | ------------- | --------- | ------------ | -------------- | ------------------------------- |
482
+ | API Keys | 32 | Alphanumeric | Very High | V1StGXR8_Z5jdHi6B-myT1234567890 |
483
+ | User IDs | 21 | Standard | High | V1StGXR8_Z5jdHi6B-myT |
484
+ | Order Numbers | 8 | Numeric | Medium | 12345678 |
485
+ | Short URLs | 6 | Alphanumeric | Low | V1StGX |
486
+
487
+ ### Performance Considerations
488
+
489
+ | Use Case | Volume | Performance | Memory |
490
+ | ------------- | ------------- | ------------------- | ------ |
491
+ | High Volume | 1M+ IDs/day | Fast Path (64-char) | Low |
492
+ | Medium Volume | 100K+ IDs/day | Standard Path | Low |
493
+ | Low Volume | <100K IDs/day | Any Path | Low |
494
+
495
+ ### Implementation Patterns
496
+
497
+ #### 1. High Security Applications
498
+
499
+ ```ruby
500
+ # API keys, authentication tokens, sensitive data
501
+ class SecureResource < ApplicationRecord
502
+ include OpaqueId::Model
503
+
504
+ self.opaque_id_length = 32
505
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
506
+ self.opaque_id_require_letter_start = true
507
+ end
508
+ ```
509
+
510
+ #### 2. General Purpose Applications
511
+
512
+ ```ruby
513
+ # User IDs, content IDs, general identifiers
514
+ class GeneralResource < ApplicationRecord
515
+ include OpaqueId::Model
516
+
517
+ self.opaque_id_length = 21
518
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
519
+ end
520
+ ```
521
+
522
+ #### 3. Public-Facing Applications
523
+
524
+ ```ruby
525
+ # URLs, public identifiers, user-facing IDs
526
+ class PublicResource < ApplicationRecord
527
+ include OpaqueId::Model
528
+
529
+ self.opaque_id_length = 8
530
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
531
+ end
532
+ ```
533
+
534
+ ## Best Practices by Use Case
535
+
536
+ ### 1. Choose Appropriate Length
537
+
538
+ - **High Security**: 21+ characters
539
+ - **Medium Security**: 15+ characters
540
+ - **Low Security**: 8+ characters
541
+
542
+ ### 2. Select Suitable Alphabet
543
+
544
+ - **URL-Safe**: Alphanumeric or Standard
545
+ - **Human-Readable**: Alphanumeric
546
+ - **High Performance**: Standard (64 characters)
547
+ - **Numeric Only**: Custom numeric alphabet
548
+
549
+ ### 3. Consider Performance Requirements
550
+
551
+ - **High Volume**: Use 64-character alphabets
552
+ - **Medium Volume**: Use standard alphabets
553
+ - **Low Volume**: Any alphabet works
554
+
555
+ ### 4. Implement Proper Security
556
+
557
+ - **Authentication**: Required for sensitive resources
558
+ - **Authorization**: Check access permissions
559
+ - **Rate Limiting**: Prevent abuse
560
+ - **Monitoring**: Track usage patterns
561
+
562
+ ## Next Steps
563
+
564
+ Now that you understand use cases:
565
+
566
+ 1. **Explore [Configuration](configuration.md)** for use case-specific configuration
567
+ 2. **Check out [Security](security.md)** for security considerations
568
+ 3. **Review [Performance](performance.md)** for performance optimization
569
+ 4. **Read [API Reference](api-reference.md)** for complete implementation details
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpaqueId
4
- VERSION = '1.2.0'
4
+ VERSION = '1.3.0'
5
5
  end