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