vibe-sort 0.2.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/README.md ADDED
@@ -0,0 +1,211 @@
1
+ # VibeSort Documentation
2
+
3
+ Welcome to the VibeSort documentation! This directory contains comprehensive guides for using, understanding, and contributing to the VibeSort gem.
4
+
5
+ ## 📚 Documentation Overview
6
+
7
+ ### [Architecture Overview](architecture.md)
8
+
9
+ **Target Audience:** Developers who want to understand how VibeSort works internally
10
+
11
+ **Contents:**
12
+ - System design and component architecture
13
+ - Class responsibilities and interactions
14
+ - Request flow from user to OpenAI API
15
+ - Error handling strategies
16
+ - Design decisions and rationale
17
+ - Future enhancement ideas
18
+
19
+ **When to read:** Before contributing code or when you need to understand the internal structure.
20
+
21
+ ---
22
+
23
+ ### [API Reference](api_reference.md)
24
+
25
+ **Target Audience:** Developers using VibeSort in their applications
26
+
27
+ **Contents:**
28
+ - Complete API documentation for all public classes and methods
29
+ - Parameter descriptions and return values
30
+ - Usage examples for each method
31
+ - Error messages and their meanings
32
+ - Response format specifications
33
+ - Thread safety considerations
34
+
35
+ **When to read:** When integrating VibeSort into your project or looking up specific method signatures.
36
+
37
+ ---
38
+
39
+ ### [Development Guide](development.md)
40
+
41
+ **Target Audience:** Contributors and maintainers
42
+
43
+ **Contents:**
44
+ - Project setup instructions
45
+ - Running tests and debugging
46
+ - Code style guidelines
47
+ - Release process
48
+ - Common development tasks
49
+ - Troubleshooting tips
50
+
51
+ **When to read:** Before contributing to the project or when setting up your development environment.
52
+
53
+ ---
54
+
55
+ ## 🚀 Quick Links
56
+
57
+ ### For Users
58
+
59
+ - **Getting Started:** See the main [README](../README.md)
60
+ - **Installation:** [README - Installation](../README.md#-installation)
61
+ - **Usage Examples:** [README - Usage](../README.md#-usage) or [API Reference](api_reference.md#usage-examples)
62
+ - **Error Handling:** [API Reference - Error Messages](api_reference.md#error-messages)
63
+
64
+ ### For Contributors
65
+
66
+ - **Setup:** [Development Guide - Getting Started](development.md#getting-started)
67
+ - **Running Tests:** [Development Guide - Running Tests](development.md#running-tests)
68
+ - **Code Style:** [Development Guide - Code Style](development.md#code-style)
69
+ - **Pull Requests:** [Development Guide - Best Practices](development.md#best-practices)
70
+
71
+ ### For Maintainers
72
+
73
+ - **Architecture:** [Architecture Overview](architecture.md)
74
+ - **Release Process:** [Development Guide - Release Process](development.md#release-process)
75
+ - **Design Decisions:** [Architecture - Design Decisions](architecture.md#design-decisions)
76
+
77
+ ---
78
+
79
+ ## 📖 Reading Guide
80
+
81
+ ### I want to use VibeSort in my project
82
+
83
+ 1. Start with the [README](../README.md) for a quick overview
84
+ 2. Check the [API Reference](api_reference.md) for detailed method documentation
85
+ 3. Review usage examples in both documents
86
+ 4. Understand error handling from the [API Reference](api_reference.md#error-messages)
87
+
88
+ ### I want to contribute to VibeSort
89
+
90
+ 1. Read the [Development Guide](development.md) for setup instructions
91
+ 2. Review the [Architecture Overview](architecture.md) to understand the codebase
92
+ 3. Follow the code style guidelines in the [Development Guide](development.md#code-style)
93
+ 4. Write tests (see [Development Guide - Writing Tests](development.md#writing-tests))
94
+
95
+ ### I want to understand how VibeSort works
96
+
97
+ 1. Start with the [Architecture Overview](architecture.md)
98
+ 2. Review the [Request Flow](architecture.md#request-flow) section
99
+ 3. Check the [Design Decisions](architecture.md#design-decisions) section
100
+ 4. Look at the source code in `lib/vibe_sort/`
101
+
102
+ ### I encountered an error
103
+
104
+ 1. Check [API Reference - Error Messages](api_reference.md#error-messages)
105
+ 2. Review [Development Guide - Troubleshooting](development.md#troubleshooting)
106
+ 3. Open an issue on GitHub if the error persists
107
+
108
+ ---
109
+
110
+ ## 🏗️ Architecture at a Glance
111
+
112
+ ```
113
+ User Application
114
+
115
+ VibeSort::Client (validates input, handles errors)
116
+
117
+ VibeSort::Configuration (stores API key, settings)
118
+
119
+ VibeSort::Sorter (communicates with OpenAI)
120
+
121
+ OpenAI API (GPT model processes request)
122
+
123
+ Sorted Array (returned to user)
124
+ ```
125
+
126
+ ---
127
+
128
+ ## 📝 Example Usage
129
+
130
+ ```ruby
131
+ require 'vibe_sort'
132
+
133
+ # Initialize client
134
+ client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
135
+
136
+ # Sort an array
137
+ result = client.sort([34, 1, 99, 15, 8])
138
+
139
+ if result[:success]
140
+ puts result[:sorted_array]
141
+ # => [1, 8, 15, 34, 99]
142
+ else
143
+ puts result[:error]
144
+ end
145
+ ```
146
+
147
+ For more examples, see the [README](../README.md) or [API Reference](api_reference.md).
148
+
149
+ ---
150
+
151
+ ## 🔧 Key Concepts
152
+
153
+ ### Configuration
154
+
155
+ VibeSort requires an OpenAI API key and optionally accepts a temperature parameter:
156
+
157
+ - **API Key:** Your OpenAI authentication token
158
+ - **Temperature:** Controls model randomness (0.0 = deterministic, 2.0 = creative)
159
+
160
+ ### Response Format
161
+
162
+ All operations return a consistent hash:
163
+
164
+ ```ruby
165
+ {
166
+ success: true/false,
167
+ sorted_array: [...],
168
+ error: "..." # only on failure
169
+ }
170
+ ```
171
+
172
+ ### Error Handling
173
+
174
+ VibeSort never raises exceptions to user code. All errors are caught and returned in the response hash.
175
+
176
+ ---
177
+
178
+ ## 🤝 Contributing
179
+
180
+ We welcome contributions! Please see:
181
+
182
+ - [Development Guide](development.md) for technical setup
183
+ - [GitHub Issues](https://github.com/chayut/vibe-sort/issues) for bugs and features
184
+ - [Pull Request Template](https://github.com/chayut/vibe-sort/pulls) for submitting changes
185
+
186
+ ---
187
+
188
+ ## 📞 Support
189
+
190
+ - 🐛 [Report a Bug](https://github.com/chayut/vibe-sort/issues/new?labels=bug)
191
+ - 💡 [Request a Feature](https://github.com/chayut/vibe-sort/issues/new?labels=enhancement)
192
+ - 📖 [Read the Docs](README.md)
193
+ - 💬 [Discussions](https://github.com/chayut/vibe-sort/discussions)
194
+
195
+ ---
196
+
197
+ ## 📜 License
198
+
199
+ VibeSort is released under the [MIT License](../LICENSE.txt).
200
+
201
+ ---
202
+
203
+ ## 🙏 Acknowledgments
204
+
205
+ - Built with [Faraday](https://lostisland.github.io/faraday/)
206
+ - Powered by [OpenAI](https://openai.com/)
207
+ - Inspired by the possibilities of AI
208
+
209
+ ---
210
+
211
+ **Happy Sorting! 🌀**
@@ -0,0 +1,440 @@
1
+ # API Reference
2
+
3
+ ## VibeSort::Client
4
+
5
+ The main public interface for the VibeSort gem.
6
+
7
+ ### Class Methods
8
+
9
+ #### `new(api_key:, temperature: 0.0)`
10
+
11
+ Creates a new VibeSort client instance.
12
+
13
+ **Parameters:**
14
+
15
+ - `api_key` (String, required): Your OpenAI API key
16
+ - `temperature` (Float, optional): Model temperature setting (default: 0.0)
17
+ - Range: 0.0 to 2.0
18
+ - Lower values (0.0-0.3): More deterministic and consistent
19
+ - Higher values (0.7-2.0): More random and creative
20
+
21
+ **Returns:** `VibeSort::Client` instance
22
+
23
+ **Raises:**
24
+
25
+ - `ArgumentError`: If api_key is nil or empty
26
+
27
+ **Example:**
28
+
29
+ ```ruby
30
+ # Basic initialization
31
+ client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
32
+
33
+ # With custom temperature
34
+ client = VibeSort::Client.new(
35
+ api_key: ENV['OPENAI_API_KEY'],
36
+ temperature: 0.2
37
+ )
38
+ ```
39
+
40
+ ### Instance Methods
41
+
42
+ #### `sort(array)`
43
+
44
+ Sorts an array of numbers and/or strings using OpenAI's API.
45
+
46
+ **Parameters:**
47
+
48
+ - `array` (Array, required): Array of numbers and/or strings to sort
49
+
50
+ **Returns:** Hash with the following keys:
51
+
52
+ - `:success` (Boolean): `true` if sorting succeeded, `false` otherwise
53
+ - `:sorted_array` (Array): The sorted array (empty on failure)
54
+ - `:error` (String): Error message (only present when `success` is `false`)
55
+
56
+ **Example:**
57
+
58
+ ```ruby
59
+ client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
60
+
61
+ # Successful sort with numbers
62
+ result = client.sort([5, 2, 8, 1, 9])
63
+ # => { success: true, sorted_array: [1, 2, 5, 8, 9] }
64
+
65
+ # Successful sort with strings
66
+ result = client.sort(["banana", "Apple", "cherry"])
67
+ # => { success: true, sorted_array: ["Apple", "banana", "cherry"] }
68
+
69
+ # Successful sort with mixed types
70
+ result = client.sort([42, "hello", 8, "world"])
71
+ # => { success: true, sorted_array: [8, 42, "hello", "world"] }
72
+
73
+ # Invalid input (unsupported type)
74
+ result = client.sort([1, :symbol, 3])
75
+ # => { success: false, sorted_array: [], error: "Input must be an array of numbers or strings" }
76
+
77
+ # Empty array
78
+ result = client.sort([])
79
+ # => { success: false, sorted_array: [], error: "Input must be an array of numbers or strings" }
80
+ ```
81
+
82
+ **Supported Types:**
83
+
84
+ - Integers: `1, 2, 3, -5, 1000`
85
+ - Floats: `1.5, 3.14, -2.7`
86
+ - Strings: `"hello", "world", "Apple"`
87
+ - Mixed: `[1, 2.5, "hello", 3, "world"]`
88
+
89
+ **Sorting Rules:**
90
+
91
+ - Numbers are sorted in ascending numerical order
92
+ - Strings are sorted in ascending alphabetical order (case-sensitive)
93
+ - In mixed arrays, numbers come before strings
94
+
95
+ ---
96
+
97
+ ## VibeSort::Configuration
98
+
99
+ Configuration object for VibeSort. Usually not used directly by end users.
100
+
101
+ ### Class Methods
102
+
103
+ #### `new(api_key:, temperature: 0.0)`
104
+
105
+ Creates a new configuration object.
106
+
107
+ **Parameters:**
108
+
109
+ - `api_key` (String, required): OpenAI API key
110
+ - `temperature` (Float, optional): Model temperature (default: 0.0)
111
+
112
+ **Raises:**
113
+
114
+ - `ArgumentError`: If api_key is nil or empty
115
+
116
+ **Example:**
117
+
118
+ ```ruby
119
+ config = VibeSort::Configuration.new(
120
+ api_key: "sk-...",
121
+ temperature: 0.0
122
+ )
123
+ ```
124
+
125
+ ### Instance Attributes
126
+
127
+ #### `api_key`
128
+
129
+ Returns the configured API key (read-only).
130
+
131
+ **Returns:** String
132
+
133
+ #### `temperature`
134
+
135
+ Returns the configured temperature setting (read-only).
136
+
137
+ **Returns:** Float
138
+
139
+ ---
140
+
141
+ ## VibeSort::Sorter
142
+
143
+ Internal class that handles API communication. Not intended for direct use.
144
+
145
+ ### Class Methods
146
+
147
+ #### `new(config)`
148
+
149
+ Creates a new sorter instance.
150
+
151
+ **Parameters:**
152
+
153
+ - `config` (VibeSort::Configuration): Configuration object
154
+
155
+ ### Instance Methods
156
+
157
+ #### `perform(array)`
158
+
159
+ Performs the sorting operation via OpenAI API.
160
+
161
+ **Parameters:**
162
+
163
+ - `array` (Array): Array of numbers and/or strings to sort
164
+
165
+ **Returns:** Hash with `:success`, `:sorted_array`, and optional `:error` keys
166
+
167
+ **Raises:**
168
+
169
+ - `VibeSort::ApiError`: On API failures or invalid responses
170
+
171
+ ### Constants
172
+
173
+ #### `OPENAI_API_URL`
174
+
175
+ The OpenAI API endpoint URL.
176
+
177
+ **Value:** `"https://api.openai.com/v1/chat/completions"`
178
+
179
+ #### `DEFAULT_MODEL`
180
+
181
+ The default GPT model used for sorting.
182
+
183
+ **Value:** `"gpt-3.5-turbo-1106"`
184
+
185
+ ---
186
+
187
+ ## VibeSort::ApiError
188
+
189
+ Custom exception class for API-related errors.
190
+
191
+ ### Class Methods
192
+
193
+ #### `new(message, response = nil)`
194
+
195
+ Creates a new API error.
196
+
197
+ **Parameters:**
198
+
199
+ - `message` (String, required): Error message
200
+ - `response` (Faraday::Response, optional): HTTP response object
201
+
202
+ **Example:**
203
+
204
+ ```ruby
205
+ raise VibeSort::ApiError.new("Invalid API key", response)
206
+ ```
207
+
208
+ ### Instance Attributes
209
+
210
+ #### `message`
211
+
212
+ Returns the error message.
213
+
214
+ **Returns:** String
215
+
216
+ #### `response`
217
+
218
+ Returns the HTTP response object (if available).
219
+
220
+ **Returns:** Faraday::Response or nil
221
+
222
+ ---
223
+
224
+ ## Response Hash Format
225
+
226
+ All sorting operations return a consistent hash structure:
227
+
228
+ ### Success Response
229
+
230
+ ```ruby
231
+ {
232
+ success: true,
233
+ sorted_array: [1, 2, 3, 4, 5]
234
+ }
235
+ ```
236
+
237
+ **Keys:**
238
+
239
+ - `success` (Boolean): Always `true` for successful operations
240
+ - `sorted_array` (Array): The sorted array in ascending order (numbers before strings)
241
+
242
+ ### Error Response
243
+
244
+ ```ruby
245
+ {
246
+ success: false,
247
+ sorted_array: [],
248
+ error: "Error message here"
249
+ }
250
+ ```
251
+
252
+ **Keys:**
253
+
254
+ - `success` (Boolean): Always `false` for failed operations
255
+ - `sorted_array` (Array): Always empty on failure
256
+ - `error` (String): Human-readable error message
257
+
258
+ ---
259
+
260
+ ## Error Messages
261
+
262
+ ### Input Validation Errors
263
+
264
+ | Error Message | Cause | Solution |
265
+ |---------------|-------|----------|
266
+ | "Input must be an array of numbers or strings" | Input is not an array | Pass an array |
267
+ | "Input must be an array of numbers or strings" | Array is empty | Pass non-empty array |
268
+ | "Input must be an array of numbers or strings" | Array contains unsupported types (symbols, objects, etc.) | Use only numbers and strings |
269
+
270
+ ### API Errors
271
+
272
+ | Error Message Pattern | Cause | Solution |
273
+ |----------------------|-------|----------|
274
+ | "OpenAI API error: Invalid API key" | Invalid or missing API key | Check API key |
275
+ | "OpenAI API error: Rate limit exceeded" | Too many requests | Wait and retry |
276
+ | "OpenAI API error: HTTP {status}" | HTTP error | Check API status |
277
+
278
+ ### Response Parsing Errors
279
+
280
+ | Error Message Pattern | Cause | Solution |
281
+ |----------------------|-------|----------|
282
+ | "Failed to parse JSON response: ..." | Malformed JSON | Retry request |
283
+ | "Response does not contain a valid 'sorted_array'" | Missing key | Report bug |
284
+ | "Sorted array contains invalid values (must be numbers or strings)" | Invalid response | Report bug |
285
+ | "Invalid response structure" | Unexpected format | Report bug |
286
+
287
+ ### Unexpected Errors
288
+
289
+ | Error Message Pattern | Cause | Solution |
290
+ |----------------------|-------|----------|
291
+ | "Unexpected error: ..." | Network issues, bugs, etc. | Check logs, retry |
292
+
293
+ ---
294
+
295
+ ## Usage Examples
296
+
297
+ ### Basic Usage
298
+
299
+ ```ruby
300
+ require 'vibe_sort'
301
+
302
+ client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
303
+ result = client.sort([5, 2, 8, 1, 9])
304
+
305
+ if result[:success]
306
+ puts result[:sorted_array]
307
+ # => [1, 2, 5, 8, 9]
308
+ end
309
+ ```
310
+
311
+ ### Error Handling
312
+
313
+ ```ruby
314
+ client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
315
+ result = client.sort([5, :symbol, 8])
316
+
317
+ unless result[:success]
318
+ puts "Error: #{result[:error]}"
319
+ # => Error: Input must be an array of numbers or strings
320
+ end
321
+ ```
322
+
323
+ ### With Custom Temperature
324
+
325
+ ```ruby
326
+ # More deterministic (recommended)
327
+ client = VibeSort::Client.new(
328
+ api_key: ENV['OPENAI_API_KEY'],
329
+ temperature: 0.0
330
+ )
331
+
332
+ # More random (not recommended for sorting!)
333
+ client = VibeSort::Client.new(
334
+ api_key: ENV['OPENAI_API_KEY'],
335
+ temperature: 1.0
336
+ )
337
+ ```
338
+
339
+ ### Different Number Types
340
+
341
+ ```ruby
342
+ client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
343
+
344
+ # Integers
345
+ client.sort([5, 2, 8, 1, 9])
346
+ # => { success: true, sorted_array: [1, 2, 5, 8, 9] }
347
+
348
+ # Floats
349
+ client.sort([3.14, 1.5, 2.7])
350
+ # => { success: true, sorted_array: [1.5, 2.7, 3.14] }
351
+
352
+ # Mixed numbers
353
+ client.sort([5, 2.5, 8, 1.2, 9])
354
+ # => { success: true, sorted_array: [1.2, 2.5, 5, 8, 9] }
355
+
356
+ # Negative numbers
357
+ client.sort([-5, 2, -8, 1, 9])
358
+ # => { success: true, sorted_array: [-8, -5, 1, 2, 9] }
359
+
360
+ # Strings
361
+ client.sort(["banana", "Apple", "cherry"])
362
+ # => { success: true, sorted_array: ["Apple", "banana", "cherry"] }
363
+
364
+ # Mixed types (numbers before strings)
365
+ client.sort([42, "hello", 8, "world", 15.5])
366
+ # => { success: true, sorted_array: [8, 15.5, 42, "hello", "world"] }
367
+ ```
368
+
369
+ ### Conditional Logic
370
+
371
+ ```ruby
372
+ client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
373
+ numbers = gets.chomp.split.map(&:to_i)
374
+
375
+ result = client.sort(numbers)
376
+
377
+ case result[:success]
378
+ when true
379
+ puts "Sorted: #{result[:sorted_array].join(', ')}"
380
+ when false
381
+ warn "Failed to sort: #{result[:error]}"
382
+ exit 1
383
+ end
384
+ ```
385
+
386
+ ---
387
+
388
+ ## Thread Safety
389
+
390
+ VibeSort::Client instances are **not thread-safe**. If you need to sort arrays concurrently:
391
+
392
+ 1. Create separate client instances per thread
393
+ 2. Use a thread-safe queue for results
394
+ 3. Consider connection pooling for high-concurrency scenarios
395
+
396
+ **Example:**
397
+
398
+ ```ruby
399
+ api_key = ENV['OPENAI_API_KEY']
400
+
401
+ threads = 5.times.map do |i|
402
+ Thread.new do
403
+ client = VibeSort::Client.new(api_key: api_key)
404
+ client.sort([rand(100), rand(100), rand(100)])
405
+ end
406
+ end
407
+
408
+ results = threads.map(&:value)
409
+ ```
410
+
411
+ ---
412
+
413
+ ## Environment Variables
414
+
415
+ ### OPENAI_API_KEY
416
+
417
+ Your OpenAI API key. Required for all operations.
418
+
419
+ **Setup:**
420
+
421
+ ```bash
422
+ export OPENAI_API_KEY='sk-your-key-here'
423
+ ```
424
+
425
+ **Usage:**
426
+
427
+ ```ruby
428
+ client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
429
+ ```
430
+
431
+ ---
432
+
433
+ ## Version
434
+
435
+ Current version: `0.1.0`
436
+
437
+ ```ruby
438
+ VibeSort::VERSION
439
+ # => "0.1.0"
440
+ ```