rack-ai 0.1.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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +55 -0
- data/CHANGELOG.md +65 -0
- data/LICENSE +21 -0
- data/README.md +687 -0
- data/ROADMAP.md +203 -0
- data/Rakefile +40 -0
- data/benchmarks/performance_benchmark.rb +283 -0
- data/examples/rails_integration.rb +301 -0
- data/examples/sinatra_integration.rb +458 -0
- data/lib/rack/ai/configuration.rb +208 -0
- data/lib/rack/ai/features/caching.rb +278 -0
- data/lib/rack/ai/features/classification.rb +67 -0
- data/lib/rack/ai/features/enhancement.rb +219 -0
- data/lib/rack/ai/features/logging.rb +238 -0
- data/lib/rack/ai/features/moderation.rb +104 -0
- data/lib/rack/ai/features/routing.rb +143 -0
- data/lib/rack/ai/features/security.rb +275 -0
- data/lib/rack/ai/middleware.rb +268 -0
- data/lib/rack/ai/providers/base.rb +107 -0
- data/lib/rack/ai/providers/huggingface.rb +259 -0
- data/lib/rack/ai/providers/local.rb +152 -0
- data/lib/rack/ai/providers/openai.rb +246 -0
- data/lib/rack/ai/utils/logger.rb +111 -0
- data/lib/rack/ai/utils/metrics.rb +220 -0
- data/lib/rack/ai/utils/sanitizer.rb +200 -0
- data/lib/rack/ai/version.rb +7 -0
- data/lib/rack/ai.rb +48 -0
- data/rack-ai.gemspec +51 -0
- metadata +290 -0
data/README.md
ADDED
@@ -0,0 +1,687 @@
|
|
1
|
+
# Rack::AI
|
2
|
+
|
3
|
+
A next-generation RubyGem that extends Rack with AI-powered capabilities for request classification, content moderation, security analysis, and intelligent caching.
|
4
|
+
|
5
|
+
[](https://badge.fury.io/rb/rack-ai)
|
6
|
+
[](https://github.com/ahmetxhero/rack-ai/actions)
|
7
|
+
[](https://coveralls.io/github/ahmetxhero/rack-ai?branch=main)
|
8
|
+
|
9
|
+
## โ **Support My Open Source Work** โ
|
10
|
+
|
11
|
+
**Love this project? Buy me a coffee and help me continue building innovative security solutions!**
|
12
|
+
|
13
|
+
[](https://buymeacoffee.com/ahmetxhero)
|
14
|
+
[](https://github.com/sponsors/ahmetxhero)
|
15
|
+
[](https://ko-fi.com/ahmetxhero)
|
16
|
+
|
17
|
+
**Your support makes a difference! ๐ Every contribution helps me dedicate more time to open source development.**
|
18
|
+
|
19
|
+
---
|
20
|
+
|
21
|
+
## ๐ Hey, This is Ahmet KAHRAMAN
|
22
|
+
|
23
|
+
**๐ป Mobile Developer & Cyber Security Expert**
|
24
|
+
**๐ Living in Ankara, Turkey ๐น๐ท**
|
25
|
+
**๐ 10+ years experience in Public Sector IT**
|
26
|
+
**๐ฌ Mantra: "Security first, innovation always"**
|
27
|
+
|
28
|
+
### ๐ Connect With Me
|
29
|
+
|
30
|
+
| Platform | Link | Description |
|
31
|
+
|----------|------|-------------|
|
32
|
+
| ๐ **Portfolio** | [ahmetxhero.web.app](https://ahmetxhero.web.app) | My home on the net |
|
33
|
+
| ๐ผ **LinkedIn** | [linkedin.com/in/ahmetxhero](https://linkedin.com/in/ahmetxhero) | Professional network |
|
34
|
+
| ๐ฅ **YouTube** | [@ahmetxhero](https://youtube.com/@ahmetxhero) | Haven't you subscribed yet? |
|
35
|
+
| ๐ **Medium** | [ahmetxhero.medium.com](https://ahmetxhero.medium.com) | Technical articles |
|
36
|
+
| ๐ค **Twitter** | [@ahmetxhero](https://x.com/ahmetxhero) | Tech thoughts & updates |
|
37
|
+
| ๐ท **Instagram** | [instagram.com/ahmetxhero](https://instagram.com/ahmetxhero) | Behind the scenes |
|
38
|
+
| ๐ฎ **Twitch** | [twitch.tv/ahmetxhero](https://twitch.tv/ahmetxhero) | Live coding sessions |
|
39
|
+
| ๐จ **Dribbble** | [dribbble.com/ahmetxhero](https://dribbble.com/ahmetxhero) | Design portfolio |
|
40
|
+
| ๐จ **Figma** | [figma.com/@ahmetxhero](https://figma.com/@ahmetxhero) | UI/UX designs |
|
41
|
+
| ๐ง **Email** | ahmetxhero@gmail.com | You can reach me here |
|
42
|
+
|
43
|
+
### ๐ ๏ธ Tech Stack & Expertise
|
44
|
+
|
45
|
+
#### Mobile Development
|
46
|
+
- **iOS:** Swift, Objective-C, UIKit, SwiftUI
|
47
|
+
- **Android:** Java, Kotlin, Android SDK
|
48
|
+
- **Cross-Platform:** Flutter, React Native
|
49
|
+
- **Frameworks:** Xamarin, Ionic
|
50
|
+
|
51
|
+
#### Cybersecurity & Forensics
|
52
|
+
- **Digital Forensics:** EnCase, FTK, Autopsy, Volatility
|
53
|
+
- **Penetration Testing:** Metasploit, Burp Suite, Nmap
|
54
|
+
- **Security Tools:** Wireshark, OWASP ZAP, Nessus
|
55
|
+
- **Programming:** Python, PowerShell, Bash
|
56
|
+
|
57
|
+
#### System Administration
|
58
|
+
- **Operating Systems:** Windows Server, Linux (Ubuntu, CentOS)
|
59
|
+
- **Virtualization:** VMware, Hyper-V, VirtualBox
|
60
|
+
- **Cloud Platforms:** AWS, Azure, Google Cloud
|
61
|
+
- **Networking:** TCP/IP, DNS, DHCP, VPN
|
62
|
+
|
63
|
+
### ๐ Professional Experience
|
64
|
+
|
65
|
+
#### Current Role
|
66
|
+
**Mobile Developer / Mobile System Operator**
|
67
|
+
*Gendarmerie General Command* | July 2024 - Present
|
68
|
+
๐ Ankara, Turkey
|
69
|
+
|
70
|
+
#### Previous Experience
|
71
|
+
- **IT Expert** | Gendarmerie General Command (2015-2024)
|
72
|
+
- **Founder** | Sadฤฑk Internet Cafe (2012-2014)
|
73
|
+
|
74
|
+
### ๐ Education
|
75
|
+
- **Master's Degree in Forensic Informatics** | Gazi University (2021-2023)
|
76
|
+
- **Bachelor's Degree in Health Management** | Anadolu University (2017-2021)
|
77
|
+
- **Associate's Degree in Computer Programming** | Atatรผrk University (2020-2023)
|
78
|
+
- **Associate's Degree in Occupational Health & Safety** | Atatรผrk University (2016-2019)
|
79
|
+
|
80
|
+
### ๐ Certifications & Achievements
|
81
|
+
- Microsoft Certified Professional
|
82
|
+
- Certified Ethical Hacker (CEH)
|
83
|
+
- Digital Forensics Expert
|
84
|
+
- iOS & Swift Development
|
85
|
+
- Flutter Development
|
86
|
+
- Android Development
|
87
|
+
|
88
|
+
### ๐ฏ Current Focus
|
89
|
+
- ๐ **Cybersecurity:** Developing secure mobile applications
|
90
|
+
- ๐ฑ **Mobile Development:** Creating innovative iOS and Android apps
|
91
|
+
- ๐ **Digital Forensics:** Advancing forensic investigation techniques
|
92
|
+
- ๐ **Knowledge Sharing:** Contributing to the tech community
|
93
|
+
- ๐ **Open Source:** Building tools for the developer community
|
94
|
+
|
95
|
+
### ๐ฃ๏ธ Speaking & Content Creation
|
96
|
+
- ๐ฅ I have my own YouTube channel - **@ahmetxhero**
|
97
|
+
- ๐ฃ๏ธ I enjoy speaking at tech events. Interested in having me speak at your event?
|
98
|
+
- ๐ค I'm passionate about mobile development and cybersecurity solutions
|
99
|
+
|
100
|
+
**"Building secure, innovative solutions for a better digital future" ๐**
|
101
|
+
|
102
|
+
---
|
103
|
+
|
104
|
+
## Features
|
105
|
+
|
106
|
+
- **๐ค Request Classification**: Automatically classify requests as human, bot, spam, or suspicious
|
107
|
+
- **๐ก๏ธ Security Analysis**: Detect SQL injection, XSS, prompt injection, and other security threats
|
108
|
+
- **๐ Content Moderation**: Real-time toxicity and policy violation detection
|
109
|
+
- **โก Smart Caching**: AI-powered predictive caching and prefetching
|
110
|
+
- **๐ฏ Intelligent Routing**: Route requests based on AI analysis results
|
111
|
+
- **๐ Enhanced Logging**: AI-generated insights and traffic pattern analysis
|
112
|
+
- **โจ Content Enhancement**: Automatic SEO, readability, and accessibility improvements
|
113
|
+
- **๐ Multiple AI Providers**: Support for OpenAI, HuggingFace, and local AI models
|
114
|
+
- **๐ Production Ready**: Fail-safe mode, async processing, and comprehensive monitoring
|
115
|
+
|
116
|
+
## Installation
|
117
|
+
|
118
|
+
Add this line to your application's Gemfile:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
gem 'rack-ai'
|
122
|
+
```
|
123
|
+
|
124
|
+
And then execute:
|
125
|
+
|
126
|
+
```bash
|
127
|
+
$ bundle install
|
128
|
+
```
|
129
|
+
|
130
|
+
Or install it yourself as:
|
131
|
+
|
132
|
+
```bash
|
133
|
+
$ gem install rack-ai
|
134
|
+
```
|
135
|
+
|
136
|
+
## Quick Start
|
137
|
+
|
138
|
+
### Basic Usage
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
require 'rack/ai'
|
142
|
+
|
143
|
+
# Configure Rack::AI
|
144
|
+
Rack::AI.configure do |config|
|
145
|
+
config.provider = :openai
|
146
|
+
config.api_key = ENV['OPENAI_API_KEY']
|
147
|
+
config.features = [:classification, :security, :moderation]
|
148
|
+
end
|
149
|
+
|
150
|
+
# Add to your Rack application
|
151
|
+
use Rack::AI::Middleware
|
152
|
+
```
|
153
|
+
|
154
|
+
### Rails Integration
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
# config/application.rb
|
158
|
+
class Application < Rails::Application
|
159
|
+
config.middleware.use Rack::AI::Middleware,
|
160
|
+
provider: :openai,
|
161
|
+
api_key: ENV['OPENAI_API_KEY'],
|
162
|
+
features: [:classification, :moderation, :security]
|
163
|
+
end
|
164
|
+
```
|
165
|
+
|
166
|
+
### Sinatra Integration
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
require 'sinatra'
|
170
|
+
require 'rack/ai'
|
171
|
+
|
172
|
+
class MyApp < Sinatra::Base
|
173
|
+
use Rack::AI::Middleware,
|
174
|
+
provider: :openai,
|
175
|
+
api_key: ENV['OPENAI_API_KEY'],
|
176
|
+
features: [:classification, :security]
|
177
|
+
|
178
|
+
get '/' do
|
179
|
+
ai_results = request.env['rack.ai'][:results]
|
180
|
+
"Classification: #{ai_results[:classification][:classification]}"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
```
|
184
|
+
|
185
|
+
## Configuration
|
186
|
+
|
187
|
+
### Global Configuration
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
Rack::AI.configure do |config|
|
191
|
+
# Provider settings
|
192
|
+
config.provider = :openai # :openai, :huggingface, :local
|
193
|
+
config.api_key = ENV['OPENAI_API_KEY']
|
194
|
+
config.timeout = 30
|
195
|
+
config.retries = 3
|
196
|
+
|
197
|
+
# Feature toggles
|
198
|
+
config.features = [:classification, :moderation, :security, :caching]
|
199
|
+
config.fail_safe = true
|
200
|
+
config.async_processing = true
|
201
|
+
|
202
|
+
# Security settings
|
203
|
+
config.sanitize_logs = true
|
204
|
+
config.allowed_data_types = [:headers, :query_params]
|
205
|
+
|
206
|
+
# Feature-specific configuration
|
207
|
+
config.classification.confidence_threshold = 0.8
|
208
|
+
config.moderation.toxicity_threshold = 0.7
|
209
|
+
config.caching.redis_url = ENV['REDIS_URL']
|
210
|
+
end
|
211
|
+
```
|
212
|
+
|
213
|
+
### Environment-Specific Configuration
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
# config/environments/production.rb
|
217
|
+
Rack::AI.configure do |config|
|
218
|
+
config.provider = :openai
|
219
|
+
config.timeout = 10 # Shorter timeout in production
|
220
|
+
config.explain_decisions = false
|
221
|
+
config.sanitize_logs = true
|
222
|
+
end
|
223
|
+
|
224
|
+
# config/environments/development.rb
|
225
|
+
Rack::AI.configure do |config|
|
226
|
+
config.provider = :local
|
227
|
+
config.api_url = 'http://localhost:8080'
|
228
|
+
config.explain_decisions = true
|
229
|
+
config.log_level = :debug
|
230
|
+
end
|
231
|
+
```
|
232
|
+
|
233
|
+
## AI Providers
|
234
|
+
|
235
|
+
### OpenAI
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
Rack::AI.configure do |config|
|
239
|
+
config.provider = :openai
|
240
|
+
config.api_key = ENV['OPENAI_API_KEY']
|
241
|
+
# Optional: config.api_url = 'https://api.openai.com/v1'
|
242
|
+
end
|
243
|
+
```
|
244
|
+
|
245
|
+
### HuggingFace
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
Rack::AI.configure do |config|
|
249
|
+
config.provider = :huggingface
|
250
|
+
config.api_key = ENV['HUGGINGFACE_API_KEY']
|
251
|
+
end
|
252
|
+
```
|
253
|
+
|
254
|
+
### Local AI Model
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
Rack::AI.configure do |config|
|
258
|
+
config.provider = :local
|
259
|
+
config.api_url = 'http://localhost:8080'
|
260
|
+
end
|
261
|
+
```
|
262
|
+
|
263
|
+
## Features
|
264
|
+
|
265
|
+
### Request Classification
|
266
|
+
|
267
|
+
Automatically classifies incoming requests:
|
268
|
+
|
269
|
+
```ruby
|
270
|
+
# Access classification results
|
271
|
+
ai_results = request.env['rack.ai'][:results]
|
272
|
+
classification = ai_results[:classification]
|
273
|
+
|
274
|
+
case classification[:classification]
|
275
|
+
when :human
|
276
|
+
# Handle human user
|
277
|
+
when :bot
|
278
|
+
# Handle bot request
|
279
|
+
when :spam
|
280
|
+
# Block or handle spam
|
281
|
+
when :suspicious
|
282
|
+
# Apply additional security measures
|
283
|
+
end
|
284
|
+
```
|
285
|
+
|
286
|
+
### Security Analysis
|
287
|
+
|
288
|
+
Detects various security threats:
|
289
|
+
|
290
|
+
```ruby
|
291
|
+
security = ai_results[:security]
|
292
|
+
|
293
|
+
if security[:threat_level] == :high
|
294
|
+
# Block request immediately
|
295
|
+
halt 403, 'Security threat detected'
|
296
|
+
elsif security[:threat_level] == :medium
|
297
|
+
# Apply additional verification
|
298
|
+
require_captcha
|
299
|
+
end
|
300
|
+
|
301
|
+
# Check specific threats
|
302
|
+
if security[:injection_detection][:threats].include?('sql_injection')
|
303
|
+
log_security_incident(request)
|
304
|
+
end
|
305
|
+
```
|
306
|
+
|
307
|
+
### Content Moderation
|
308
|
+
|
309
|
+
Real-time content analysis:
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
moderation = ai_results[:moderation]
|
313
|
+
|
314
|
+
if moderation[:flagged]
|
315
|
+
categories = moderation[:categories]
|
316
|
+
|
317
|
+
if categories['hate']
|
318
|
+
reject_content('Hate speech detected')
|
319
|
+
elsif categories['violence']
|
320
|
+
flag_for_review(content)
|
321
|
+
end
|
322
|
+
end
|
323
|
+
```
|
324
|
+
|
325
|
+
### Smart Caching
|
326
|
+
|
327
|
+
AI-powered caching decisions:
|
328
|
+
|
329
|
+
```ruby
|
330
|
+
caching = ai_results[:caching]
|
331
|
+
|
332
|
+
if caching[:should_prefetch]
|
333
|
+
# Schedule prefetching for predicted requests
|
334
|
+
PrefetchJob.perform_later(caching[:pattern_analysis])
|
335
|
+
end
|
336
|
+
|
337
|
+
if caching[:cache_hit]
|
338
|
+
# Use cached AI analysis
|
339
|
+
cached_result = caching[:cached_result]
|
340
|
+
end
|
341
|
+
```
|
342
|
+
|
343
|
+
## Advanced Usage
|
344
|
+
|
345
|
+
### Custom AI Processing
|
346
|
+
|
347
|
+
```ruby
|
348
|
+
class CustomAIMiddleware
|
349
|
+
def initialize(app)
|
350
|
+
@app = app
|
351
|
+
end
|
352
|
+
|
353
|
+
def call(env)
|
354
|
+
# Add custom context for AI processing
|
355
|
+
env['rack.ai.custom'] = {
|
356
|
+
user_tier: extract_user_tier(env),
|
357
|
+
api_version: extract_api_version(env)
|
358
|
+
}
|
359
|
+
|
360
|
+
status, headers, body = @app.call(env)
|
361
|
+
|
362
|
+
# Process AI results
|
363
|
+
ai_results = env['rack.ai'][:results]
|
364
|
+
handle_ai_insights(ai_results)
|
365
|
+
|
366
|
+
[status, headers, body]
|
367
|
+
end
|
368
|
+
end
|
369
|
+
```
|
370
|
+
|
371
|
+
### Conditional AI Processing
|
372
|
+
|
373
|
+
```ruby
|
374
|
+
use Rack::AI::Middleware,
|
375
|
+
provider: :openai,
|
376
|
+
api_key: ENV['OPENAI_API_KEY'],
|
377
|
+
features: [:classification, :security],
|
378
|
+
condition: ->(env) {
|
379
|
+
# Only process API requests
|
380
|
+
env['PATH_INFO'].start_with?('/api/')
|
381
|
+
}
|
382
|
+
```
|
383
|
+
|
384
|
+
### Multiple AI Providers
|
385
|
+
|
386
|
+
```ruby
|
387
|
+
# Primary provider with fallback
|
388
|
+
primary_config = {
|
389
|
+
provider: :openai,
|
390
|
+
api_key: ENV['OPENAI_API_KEY']
|
391
|
+
}
|
392
|
+
|
393
|
+
fallback_config = {
|
394
|
+
provider: :local,
|
395
|
+
api_url: 'http://localhost:8080'
|
396
|
+
}
|
397
|
+
|
398
|
+
use Rack::AI::Middleware, primary_config
|
399
|
+
use Rack::AI::FallbackMiddleware, fallback_config
|
400
|
+
```
|
401
|
+
|
402
|
+
## Monitoring and Metrics
|
403
|
+
|
404
|
+
### Built-in Metrics
|
405
|
+
|
406
|
+
```ruby
|
407
|
+
# Access metrics
|
408
|
+
metrics = Rack::AI::Utils::Metrics
|
409
|
+
|
410
|
+
# Request processing metrics
|
411
|
+
processing_time = metrics.get_histogram_stats('rack_ai.processing_time')
|
412
|
+
request_count = metrics.get_counter('rack_ai.requests_processed')
|
413
|
+
|
414
|
+
# Feature-specific metrics
|
415
|
+
classification_accuracy = metrics.get_histogram_stats('rack_ai.feature.classification.confidence')
|
416
|
+
security_threats = metrics.get_counter('rack_ai.feature.security.threats_detected')
|
417
|
+
```
|
418
|
+
|
419
|
+
### Prometheus Integration
|
420
|
+
|
421
|
+
```ruby
|
422
|
+
# Export metrics in Prometheus format
|
423
|
+
get '/metrics' do
|
424
|
+
content_type 'text/plain'
|
425
|
+
Rack::AI::Utils::Metrics.export_prometheus_format
|
426
|
+
end
|
427
|
+
```
|
428
|
+
|
429
|
+
### Custom Logging
|
430
|
+
|
431
|
+
```ruby
|
432
|
+
# Enhanced logging with AI context
|
433
|
+
class AILogger < Rack::AI::Utils::Logger
|
434
|
+
def self.log_request(env, ai_results)
|
435
|
+
info('AI-enhanced request processed', {
|
436
|
+
path: env['PATH_INFO'],
|
437
|
+
classification: ai_results[:classification][:classification],
|
438
|
+
threat_level: ai_results[:security][:threat_level],
|
439
|
+
processing_time: ai_results[:processing_time]
|
440
|
+
})
|
441
|
+
end
|
442
|
+
end
|
443
|
+
```
|
444
|
+
|
445
|
+
## Performance Considerations
|
446
|
+
|
447
|
+
### Async Processing
|
448
|
+
|
449
|
+
```ruby
|
450
|
+
# Enable async processing for better performance
|
451
|
+
Rack::AI.configure do |config|
|
452
|
+
config.async_processing = true
|
453
|
+
config.timeout = 5 # Shorter timeout for async
|
454
|
+
end
|
455
|
+
```
|
456
|
+
|
457
|
+
### Feature Selection
|
458
|
+
|
459
|
+
```ruby
|
460
|
+
# Enable only necessary features
|
461
|
+
Rack::AI.configure do |config|
|
462
|
+
# Lightweight configuration
|
463
|
+
config.features = [:classification, :security]
|
464
|
+
|
465
|
+
# Heavy configuration (includes caching, enhancement)
|
466
|
+
# config.features = [:classification, :moderation, :security, :caching, :enhancement]
|
467
|
+
end
|
468
|
+
```
|
469
|
+
|
470
|
+
### Caching AI Results
|
471
|
+
|
472
|
+
```ruby
|
473
|
+
# Cache AI analysis results
|
474
|
+
Rack::AI.configure do |config|
|
475
|
+
config.cache_enabled = true
|
476
|
+
config.cache_ttl = 3600 # 1 hour
|
477
|
+
config.caching.redis_url = ENV['REDIS_URL']
|
478
|
+
end
|
479
|
+
```
|
480
|
+
|
481
|
+
## Error Handling
|
482
|
+
|
483
|
+
### Fail-Safe Mode
|
484
|
+
|
485
|
+
```ruby
|
486
|
+
# Graceful degradation when AI services are unavailable
|
487
|
+
Rack::AI.configure do |config|
|
488
|
+
config.fail_safe = true # Continue processing even if AI fails
|
489
|
+
config.timeout = 10 # Reasonable timeout
|
490
|
+
config.retries = 2 # Limited retries
|
491
|
+
end
|
492
|
+
```
|
493
|
+
|
494
|
+
### Custom Error Handling
|
495
|
+
|
496
|
+
```ruby
|
497
|
+
class AIErrorHandler
|
498
|
+
def initialize(app)
|
499
|
+
@app = app
|
500
|
+
end
|
501
|
+
|
502
|
+
def call(env)
|
503
|
+
@app.call(env)
|
504
|
+
rescue Rack::AI::ProviderError => e
|
505
|
+
# Handle AI provider errors
|
506
|
+
[503, {}, ['AI service temporarily unavailable']]
|
507
|
+
rescue Rack::AI::ConfigurationError => e
|
508
|
+
# Handle configuration errors
|
509
|
+
[500, {}, ['AI configuration error']]
|
510
|
+
end
|
511
|
+
end
|
512
|
+
```
|
513
|
+
|
514
|
+
## Security Best Practices
|
515
|
+
|
516
|
+
### Data Sanitization
|
517
|
+
|
518
|
+
```ruby
|
519
|
+
Rack::AI.configure do |config|
|
520
|
+
# Sanitize sensitive data before sending to AI
|
521
|
+
config.sanitize_logs = true
|
522
|
+
config.allowed_data_types = [:headers, :query_params]
|
523
|
+
config.blocked_data_types = [:body, :cookies]
|
524
|
+
end
|
525
|
+
```
|
526
|
+
|
527
|
+
### API Key Management
|
528
|
+
|
529
|
+
```ruby
|
530
|
+
# Use environment variables for API keys
|
531
|
+
Rack::AI.configure do |config|
|
532
|
+
config.api_key = ENV['OPENAI_API_KEY']
|
533
|
+
# Never hardcode API keys in your application
|
534
|
+
end
|
535
|
+
```
|
536
|
+
|
537
|
+
### Rate Limiting
|
538
|
+
|
539
|
+
```ruby
|
540
|
+
Rack::AI.configure do |config|
|
541
|
+
config.rate_limit = 1000 # Requests per minute
|
542
|
+
config.security.rate_limit = 100 # Lower limit for security analysis
|
543
|
+
end
|
544
|
+
```
|
545
|
+
|
546
|
+
## Testing
|
547
|
+
|
548
|
+
### Test Configuration
|
549
|
+
|
550
|
+
```ruby
|
551
|
+
# spec/spec_helper.rb
|
552
|
+
RSpec.configure do |config|
|
553
|
+
config.before(:each) do
|
554
|
+
Rack::AI.configure do |config|
|
555
|
+
config.provider = :local
|
556
|
+
config.api_url = 'http://localhost:8080'
|
557
|
+
config.features = [] # Disable AI in tests
|
558
|
+
config.fail_safe = true
|
559
|
+
end
|
560
|
+
end
|
561
|
+
end
|
562
|
+
```
|
563
|
+
|
564
|
+
### Mocking AI Responses
|
565
|
+
|
566
|
+
```ruby
|
567
|
+
# Mock AI provider responses
|
568
|
+
RSpec.describe 'AI Integration' do
|
569
|
+
before do
|
570
|
+
allow_any_instance_of(Rack::AI::Providers::OpenAI)
|
571
|
+
.to receive(:classify_request)
|
572
|
+
.and_return({
|
573
|
+
classification: :human,
|
574
|
+
confidence: 0.9,
|
575
|
+
provider: :openai
|
576
|
+
})
|
577
|
+
end
|
578
|
+
|
579
|
+
it 'processes requests with AI' do
|
580
|
+
get '/api/test'
|
581
|
+
expect(last_response.headers['X-AI-Classification']).to eq('human')
|
582
|
+
end
|
583
|
+
end
|
584
|
+
```
|
585
|
+
|
586
|
+
## Deployment
|
587
|
+
|
588
|
+
### Production Checklist
|
589
|
+
|
590
|
+
- [ ] Set appropriate API keys via environment variables
|
591
|
+
- [ ] Configure fail-safe mode and timeouts
|
592
|
+
- [ ] Enable metrics and monitoring
|
593
|
+
- [ ] Set up Redis for caching (if using caching features)
|
594
|
+
- [ ] Configure log sanitization
|
595
|
+
- [ ] Test AI provider connectivity
|
596
|
+
- [ ] Set up alerting for AI service failures
|
597
|
+
|
598
|
+
### Docker Configuration
|
599
|
+
|
600
|
+
```dockerfile
|
601
|
+
# Dockerfile
|
602
|
+
FROM ruby:3.2
|
603
|
+
|
604
|
+
# Install dependencies
|
605
|
+
COPY Gemfile* ./
|
606
|
+
RUN bundle install
|
607
|
+
|
608
|
+
# Copy application
|
609
|
+
COPY . .
|
610
|
+
|
611
|
+
# Set environment variables
|
612
|
+
ENV OPENAI_API_KEY=${OPENAI_API_KEY}
|
613
|
+
ENV REDIS_URL=${REDIS_URL}
|
614
|
+
ENV RACK_AI_LOG_LEVEL=info
|
615
|
+
|
616
|
+
CMD ["bundle", "exec", "rackup"]
|
617
|
+
```
|
618
|
+
|
619
|
+
### Kubernetes Deployment
|
620
|
+
|
621
|
+
```yaml
|
622
|
+
# deployment.yaml
|
623
|
+
apiVersion: apps/v1
|
624
|
+
kind: Deployment
|
625
|
+
metadata:
|
626
|
+
name: rack-ai-app
|
627
|
+
spec:
|
628
|
+
replicas: 3
|
629
|
+
selector:
|
630
|
+
matchLabels:
|
631
|
+
app: rack-ai-app
|
632
|
+
template:
|
633
|
+
metadata:
|
634
|
+
labels:
|
635
|
+
app: rack-ai-app
|
636
|
+
spec:
|
637
|
+
containers:
|
638
|
+
- name: app
|
639
|
+
image: your-app:latest
|
640
|
+
env:
|
641
|
+
- name: OPENAI_API_KEY
|
642
|
+
valueFrom:
|
643
|
+
secretKeyRef:
|
644
|
+
name: ai-secrets
|
645
|
+
key: openai-api-key
|
646
|
+
- name: REDIS_URL
|
647
|
+
value: "redis://redis-service:6379"
|
648
|
+
```
|
649
|
+
|
650
|
+
## Contributing
|
651
|
+
|
652
|
+
1. Fork the repository
|
653
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
654
|
+
3. Commit your changes (`git commit -am 'Add amazing feature'`)
|
655
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
656
|
+
5. Open a Pull Request
|
657
|
+
|
658
|
+
### Development Setup
|
659
|
+
|
660
|
+
```bash
|
661
|
+
git clone https://github.com/rack-ai/rack-ai.git
|
662
|
+
cd rack-ai
|
663
|
+
bundle install
|
664
|
+
bundle exec rspec
|
665
|
+
bundle exec rubocop
|
666
|
+
```
|
667
|
+
|
668
|
+
### Running Benchmarks
|
669
|
+
|
670
|
+
```bash
|
671
|
+
bundle exec ruby benchmarks/performance_benchmark.rb
|
672
|
+
```
|
673
|
+
|
674
|
+
## License
|
675
|
+
|
676
|
+
This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
677
|
+
|
678
|
+
## Support
|
679
|
+
|
680
|
+
- ๐ [Documentation](https://rack-ai.github.io/rack-ai/)
|
681
|
+
- ๐ [Issue Tracker](https://github.com/rack-ai/rack-ai/issues)
|
682
|
+
- ๐ฌ [Discussions](https://github.com/rack-ai/rack-ai/discussions)
|
683
|
+
- ๐ง [Email Support](mailto:support@rack-ai.dev)
|
684
|
+
|
685
|
+
## Roadmap
|
686
|
+
|
687
|
+
See our [roadmap](ROADMAP.md) for planned features and improvements.
|