mistral_translator 0.1.0 → 0.2.1
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 +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +189 -121
- data/README_TESTING.md +33 -0
- data/SECURITY.md +157 -0
- data/docs/.nojekyll +2 -0
- data/docs/404.html +30 -0
- data/docs/README.md +153 -0
- data/docs/advanced-usage/batch-processing.md +158 -0
- data/docs/advanced-usage/error-handling.md +106 -0
- data/docs/advanced-usage/monitoring.md +133 -0
- data/docs/advanced-usage/summarization.md +86 -0
- data/docs/advanced-usage/translations.md +141 -0
- data/docs/api-reference/callbacks.md +231 -0
- data/docs/api-reference/configuration.md +74 -0
- data/docs/api-reference/errors.md +673 -0
- data/docs/api-reference/methods.md +539 -0
- data/docs/getting-started.md +179 -0
- data/docs/index.html +27 -0
- data/docs/installation.md +142 -0
- data/docs/migration-0.1.0-to-0.2.0.md +61 -0
- data/docs/rails-integration/adapters.md +84 -0
- data/docs/rails-integration/controllers.md +107 -0
- data/docs/rails-integration/jobs.md +97 -0
- data/docs/rails-integration/setup.md +339 -0
- data/examples/basic_usage.rb +129 -102
- data/examples/batch-job.rb +511 -0
- data/examples/monitoring-setup.rb +499 -0
- data/examples/rails-model.rb +399 -0
- data/lib/mistral_translator/adapters.rb +261 -0
- data/lib/mistral_translator/client.rb +103 -100
- data/lib/mistral_translator/client_helpers.rb +161 -0
- data/lib/mistral_translator/configuration.rb +171 -1
- data/lib/mistral_translator/errors.rb +16 -0
- data/lib/mistral_translator/helpers.rb +292 -0
- data/lib/mistral_translator/helpers_extensions.rb +150 -0
- data/lib/mistral_translator/levenshtein_helpers.rb +40 -0
- data/lib/mistral_translator/logger.rb +28 -4
- data/lib/mistral_translator/prompt_builder.rb +93 -41
- data/lib/mistral_translator/prompt_helpers.rb +83 -0
- data/lib/mistral_translator/prompt_metadata_helpers.rb +42 -0
- data/lib/mistral_translator/response_parser.rb +194 -23
- data/lib/mistral_translator/security.rb +72 -0
- data/lib/mistral_translator/summarizer.rb +41 -2
- data/lib/mistral_translator/translator.rb +174 -98
- data/lib/mistral_translator/translator_helpers.rb +268 -0
- data/lib/mistral_translator/version.rb +1 -1
- data/lib/mistral_translator.rb +51 -25
- metadata +39 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1736f848f5877e51c319bb4584e110e863a6e44464af7f05133d3ea2e2f96fd6
|
4
|
+
data.tar.gz: bd06e5e900f129e662fb2ca90b5c241e55ac86511710d7c4f0cc373fca6c4c2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f57b5f9652c5b8327de801d5e391eb41f3c6915a9d4551e4ee57a2a61369ead60cffe1c72a5a35ccf089f23401d41d5685480331fe97786d9af7a661c303349
|
7
|
+
data.tar.gz: 672f5aa7932401c2bae6c2ced94058d60ffb6dfab3fb5d6760896f61ed26e5f4eef7b2491697d6680e544b97612f774b4f6fed95b66d0127984d1863592e1509
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.0] - 2025-09-09
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
- Contexte et glossaire via `MistralTranslator::Translator#translate`
|
8
|
+
- Rate limiting client (50 req/min par défaut, thread-safe)
|
9
|
+
- Validation basique des entrées (max 50k chars, batch ≤ 20)
|
10
|
+
- Nouvelles erreurs: `SecurityError`, `RateLimitExceededError`
|
11
|
+
- Métriques intégrées et callbacks de monitoring
|
12
|
+
- Documentation mise à jour + guide de migration 0.1.0 → 0.2.0
|
13
|
+
|
14
|
+
### Changed
|
15
|
+
|
16
|
+
- Exemples et docs: utilisation d’une instance `Translator` pour `context`/`glossary`
|
17
|
+
- Amélioration de la structure interne (client/translator/summarizer)
|
18
|
+
|
19
|
+
### Fixed
|
20
|
+
|
21
|
+
- Masquage renforcé des secrets dans VCR
|
22
|
+
- Messages d’erreurs plus explicites pour les réponses invalides
|
23
|
+
|
3
24
|
## [0.1.0] - 2025-09-01
|
4
25
|
|
5
26
|
- Initial release
|
data/README.md
CHANGED
@@ -1,205 +1,273 @@
|
|
1
1
|
# MistralTranslator
|
2
2
|
|
3
|
-
|
3
|
+
Ruby gem for AI-powered translation and text summarization using Mistral AI API, with advanced Rails support.
|
4
4
|
|
5
|
-
|
5
|
+
[](https://badge.fury.io/rb/mistral_translator)
|
6
|
+
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
6
8
|
|
7
|
-
|
9
|
+
## Quick Start
|
8
10
|
|
9
11
|
```ruby
|
12
|
+
# Installation
|
10
13
|
gem 'mistral_translator'
|
14
|
+
|
15
|
+
# Configuration
|
16
|
+
MistralTranslator.configure do |config|
|
17
|
+
config.api_key = ENV['MISTRAL_API_KEY']
|
18
|
+
end
|
19
|
+
|
20
|
+
# Basic usage
|
21
|
+
MistralTranslator.translate("Bonjour le monde", from: "fr", to: "en")
|
22
|
+
# => "Hello world"
|
11
23
|
```
|
12
24
|
|
13
|
-
|
25
|
+
## Key Features
|
14
26
|
|
15
|
-
|
16
|
-
|
27
|
+
- **Smart translation** with context and custom glossaries
|
28
|
+
- **Native Rails integration** (Mobility, Globalize, I18n attributes)
|
29
|
+
- **Batch processing** for high-volume translations
|
30
|
+
- **Multi-level summarization** with translation
|
31
|
+
- **Robust error handling** with automatic retry and fallback
|
32
|
+
- **Complete monitoring** with metrics and callbacks
|
33
|
+
- **Asynchronous jobs** for background processing
|
34
|
+
- **Built-in rate limiting** and security
|
35
|
+
|
36
|
+
## Supported Languages
|
37
|
+
|
38
|
+
`fr` `en` `es` `pt` `de` `it` `nl` `ru` `mg` `ja` `ko` `zh` `ar`
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Add to your Gemfile:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
gem 'mistral_translator'
|
17
46
|
```
|
18
47
|
|
19
|
-
|
48
|
+
Then run:
|
20
49
|
|
21
50
|
```bash
|
22
|
-
|
51
|
+
bundle install
|
23
52
|
```
|
24
53
|
|
25
|
-
|
54
|
+
### API Key Setup
|
55
|
+
|
56
|
+
Get your API key from [Mistral AI Console](https://console.mistral.ai/) and configure it:
|
26
57
|
|
27
58
|
```ruby
|
28
|
-
|
59
|
+
# Environment variable (recommended)
|
60
|
+
export MISTRAL_API_KEY="your_api_key_here"
|
29
61
|
|
62
|
+
# Or in Rails config/initializers/mistral_translator.rb
|
30
63
|
MistralTranslator.configure do |config|
|
31
|
-
config.api_key = '
|
32
|
-
config.
|
33
|
-
config.model = 'mistral-small' # optionnel
|
64
|
+
config.api_key = ENV['MISTRAL_API_KEY']
|
65
|
+
config.enable_metrics = Rails.env.production?
|
34
66
|
end
|
35
67
|
```
|
36
68
|
|
37
|
-
|
69
|
+
## Basic Usage Examples
|
38
70
|
|
39
|
-
|
40
|
-
|
41
|
-
```bash
|
42
|
-
export MISTRAL_API_KEY=votre_clé_api
|
43
|
-
export MISTRAL_API_URL=https://api.mistral.ai
|
44
|
-
```
|
45
|
-
|
46
|
-
## Utilisation
|
47
|
-
|
48
|
-
### Traduction simple
|
71
|
+
### Simple Translation
|
49
72
|
|
50
73
|
```ruby
|
51
|
-
#
|
52
|
-
result = MistralTranslator.translate("Bonjour
|
53
|
-
# => "Hello
|
74
|
+
# Basic translation
|
75
|
+
result = MistralTranslator.translate("Bonjour", from: "fr", to: "en")
|
76
|
+
# => "Hello"
|
54
77
|
|
55
|
-
#
|
56
|
-
|
57
|
-
"
|
58
|
-
from:
|
59
|
-
to:
|
78
|
+
# With context for better accuracy
|
79
|
+
result = MistralTranslator.translate(
|
80
|
+
"Le produit est disponible",
|
81
|
+
from: "fr",
|
82
|
+
to: "en",
|
83
|
+
context: "E-commerce website"
|
60
84
|
)
|
61
|
-
# =>
|
85
|
+
# => "The product is available"
|
86
|
+
|
87
|
+
# With custom glossary
|
88
|
+
result = MistralTranslator.translate(
|
89
|
+
"Notre API est performante",
|
90
|
+
from: "fr",
|
91
|
+
to: "en",
|
92
|
+
glossary: { "API" => "API" } # Preserve technical terms
|
93
|
+
)
|
94
|
+
# => "Our API is high-performing"
|
62
95
|
```
|
63
96
|
|
64
|
-
###
|
97
|
+
### Auto-Detection
|
65
98
|
|
66
99
|
```ruby
|
67
|
-
|
68
|
-
|
69
|
-
# =>
|
100
|
+
# Automatic source language detection
|
101
|
+
MistralTranslator.translate_auto("¡Hola mundo!", to: "fr")
|
102
|
+
# => "Salut le monde !"
|
70
103
|
```
|
71
104
|
|
72
|
-
###
|
105
|
+
### Batch Processing
|
73
106
|
|
74
107
|
```ruby
|
75
|
-
|
76
|
-
|
108
|
+
texts = ["Bonjour", "Merci", "Au revoir"]
|
109
|
+
results = MistralTranslator.translate_batch(texts, from: "fr", to: "en")
|
110
|
+
# => {0 => "Hello", 1 => "Thank you", 2 => "Goodbye"}
|
77
111
|
```
|
78
112
|
|
79
|
-
###
|
113
|
+
### Text Summarization
|
80
114
|
|
81
115
|
```ruby
|
82
|
-
long_text = "
|
116
|
+
long_text = "Ruby on Rails is a web framework..."
|
83
117
|
|
84
|
-
#
|
85
|
-
summary = MistralTranslator.summarize(long_text, language:
|
118
|
+
# Simple summary
|
119
|
+
summary = MistralTranslator.summarize(long_text, language: "en", max_words: 50)
|
86
120
|
|
87
|
-
#
|
121
|
+
# Summary with translation
|
88
122
|
summary = MistralTranslator.summarize_and_translate(
|
89
|
-
long_text,
|
90
|
-
from: 'fr',
|
91
|
-
to: 'en',
|
92
|
-
max_words: 150
|
93
|
-
)
|
94
|
-
|
95
|
-
# Résumés par niveaux
|
96
|
-
summaries = MistralTranslator.summarize_tiered(
|
97
123
|
long_text,
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
long: 300
|
124
|
+
from: "en",
|
125
|
+
to: "fr",
|
126
|
+
max_words: 100
|
102
127
|
)
|
103
|
-
# => { short: "...", medium: "...", long: "..." }
|
104
128
|
```
|
105
129
|
|
106
|
-
###
|
130
|
+
### Rails Integration
|
107
131
|
|
108
132
|
```ruby
|
109
|
-
#
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
133
|
+
# With Mobility
|
134
|
+
class Article < ApplicationRecord
|
135
|
+
extend Mobility
|
136
|
+
translates :title, :content, backend: :table
|
137
|
+
|
138
|
+
def translate_to_all_languages!
|
139
|
+
MistralTranslator::RecordTranslation.translate_mobility_record(
|
140
|
+
self,
|
141
|
+
[:title, :content],
|
142
|
+
source_locale: I18n.locale
|
143
|
+
)
|
144
|
+
end
|
145
|
+
end
|
115
146
|
|
116
|
-
|
117
|
-
|
147
|
+
# Usage
|
148
|
+
article = Article.create!(title_fr: "Titre français", content_fr: "Contenu...")
|
149
|
+
article.translate_to_all_languages!
|
118
150
|
```
|
119
151
|
|
120
|
-
##
|
121
|
-
|
122
|
-
La gem supporte les langues suivantes :
|
123
|
-
- Français (fr)
|
124
|
-
- Anglais (en)
|
125
|
-
- Espagnol (es)
|
126
|
-
- Portugais (pt)
|
127
|
-
- Allemand (de)
|
128
|
-
- Italien (it)
|
129
|
-
- Néerlandais (nl)
|
130
|
-
- Russe (ru)
|
131
|
-
- Japonais (ja)
|
132
|
-
- Coréen (ko)
|
133
|
-
- Chinois (zh)
|
134
|
-
- Arabe (ar)
|
152
|
+
## Configuration Options
|
135
153
|
|
136
154
|
```ruby
|
137
|
-
|
138
|
-
|
139
|
-
|
155
|
+
MistralTranslator.configure do |config|
|
156
|
+
# Required
|
157
|
+
config.api_key = ENV['MISTRAL_API_KEY']
|
158
|
+
|
159
|
+
# Optional
|
160
|
+
config.model = "mistral-small" # AI model to use
|
161
|
+
config.retry_delays = [1, 2, 4, 8, 16] # Retry delays in seconds
|
162
|
+
config.enable_metrics = true # Enable performance metrics
|
163
|
+
|
164
|
+
# Callbacks for monitoring
|
165
|
+
config.on_translation_complete = ->(from, to, orig_len, trans_len, duration) {
|
166
|
+
Rails.logger.info "Translation #{from}→#{to} completed in #{duration.round(2)}s"
|
167
|
+
}
|
168
|
+
|
169
|
+
config.on_translation_error = ->(from, to, error, attempt, timestamp) {
|
170
|
+
Rails.logger.error "Translation failed: #{error.message} (attempt #{attempt})"
|
171
|
+
}
|
172
|
+
end
|
140
173
|
```
|
141
174
|
|
142
|
-
##
|
175
|
+
## Error Handling
|
143
176
|
|
144
177
|
```ruby
|
145
178
|
begin
|
146
|
-
result = MistralTranslator.translate("Hello", from:
|
179
|
+
result = MistralTranslator.translate("Hello", from: "en", to: "fr")
|
147
180
|
rescue MistralTranslator::RateLimitError
|
148
|
-
|
181
|
+
# Rate limit hit - automatic retry with backoff
|
182
|
+
retry
|
149
183
|
rescue MistralTranslator::AuthenticationError
|
150
|
-
|
184
|
+
# Invalid API key
|
185
|
+
Rails.logger.error "Check your Mistral API key"
|
151
186
|
rescue MistralTranslator::UnsupportedLanguageError => e
|
152
|
-
|
187
|
+
# Language not supported
|
188
|
+
Rails.logger.error "Language '#{e.language}' not supported"
|
189
|
+
rescue MistralTranslator::Error => e
|
190
|
+
# General error handling
|
191
|
+
Rails.logger.error "Translation failed: #{e.message}"
|
153
192
|
end
|
154
193
|
```
|
155
194
|
|
156
|
-
##
|
157
|
-
|
158
|
-
### Client personnalisé
|
195
|
+
## Performance Monitoring
|
159
196
|
|
160
197
|
```ruby
|
161
|
-
|
162
|
-
|
163
|
-
|
198
|
+
# Enable metrics
|
199
|
+
MistralTranslator.configure { |c| c.enable_metrics = true }
|
200
|
+
|
201
|
+
# View metrics
|
202
|
+
metrics = MistralTranslator.metrics
|
203
|
+
puts "Total translations: #{metrics[:total_translations]}"
|
204
|
+
puts "Average time: #{metrics[:average_translation_time]}s"
|
205
|
+
puts "Error rate: #{metrics[:error_rate]}%"
|
164
206
|
```
|
165
207
|
|
166
|
-
|
208
|
+
## 📖 Complete Documentation
|
167
209
|
|
168
|
-
|
169
|
-
status = MistralTranslator.health_check
|
170
|
-
# => { status: :ok, message: "API connection successful" }
|
171
|
-
```
|
210
|
+
**For comprehensive guides, advanced usage, and Rails integration examples:**
|
172
211
|
|
173
|
-
|
212
|
+
### 🌐 [**Full Documentation Website**](https://peyochanchan.github.io/mistral_translator/)
|
174
213
|
|
175
|
-
|
214
|
+
The complete documentation includes:
|
176
215
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
216
|
+
- **Getting Started Guide** - Step-by-step tutorials
|
217
|
+
- **Advanced Usage** - Context, glossaries, batch processing, monitoring
|
218
|
+
- **Rails Integration** - Mobility, Globalize, jobs, controllers
|
219
|
+
- **API Reference** - Complete method documentation
|
220
|
+
- **Examples** - Ready-to-use code samples
|
221
|
+
- **Error Handling** - Comprehensive error management strategies
|
222
|
+
|
223
|
+
### Quick Links
|
224
|
+
|
225
|
+
- [Installation & Setup](https://peyochanchan.github.io/mistral_translator/installation)
|
226
|
+
- [Rails Integration Guide](https://peyochanchan.github.io/mistral_translator/rails-integration/)
|
227
|
+
- [API Methods Reference](https://peyochanchan.github.io/mistral_translator/api-reference/methods)
|
228
|
+
- [Error Handling Guide](https://peyochanchan.github.io/mistral_translator/api-reference/errors)
|
229
|
+
- [Monitoring Setup](https://peyochanchan.github.io/mistral_translator/advanced-usage/monitoring)
|
181
230
|
|
182
|
-
|
231
|
+
## Requirements
|
232
|
+
|
233
|
+
- Ruby 3.2+
|
234
|
+
- Mistral AI API key
|
235
|
+
- Rails 6.0+ (optional, for Rails integration features)
|
236
|
+
|
237
|
+
## Testing
|
183
238
|
|
184
239
|
```bash
|
240
|
+
# Run tests
|
185
241
|
bundle exec rspec
|
186
|
-
```
|
187
242
|
|
188
|
-
|
243
|
+
# Run with coverage
|
244
|
+
COVERAGE=true bundle exec rspec
|
189
245
|
|
190
|
-
|
191
|
-
bundle exec
|
246
|
+
# Integration tests (requires API key)
|
247
|
+
MISTRAL_API_KEY=your_key bundle exec rspec --tag integration
|
192
248
|
```
|
193
249
|
|
194
|
-
##
|
250
|
+
## Contributing
|
251
|
+
|
252
|
+
1. Fork the repository
|
253
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
254
|
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
255
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
256
|
+
5. Open a Pull Request
|
257
|
+
|
258
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed guidelines.
|
259
|
+
|
260
|
+
## License
|
261
|
+
|
262
|
+
MIT License. See [LICENSE](LICENSE) for details.
|
263
|
+
|
264
|
+
## Support
|
195
265
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
4. Pousser vers la branche
|
201
|
-
5. Ouvrir une Pull Request
|
266
|
+
- 📖 [Documentation](https://peyochanchan.github.io/mistral_translator/)
|
267
|
+
- 🐛 [Issues](../../issues)
|
268
|
+
- 💬 [Discussions](../../discussions)
|
269
|
+
- 📧 Support: Create an issue for help
|
202
270
|
|
203
|
-
|
271
|
+
---
|
204
272
|
|
205
|
-
|
273
|
+
Built with Ruby ❤️ by [@peyochanchan](https://github.com/peyochanchan)
|
data/README_TESTING.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Tests de la gem MistralTranslator
|
2
|
+
|
3
|
+
## Types de tests
|
4
|
+
|
5
|
+
### 1. Tests unitaires (par défaut)
|
6
|
+
|
7
|
+
- Utilisent des mocks et stubs
|
8
|
+
- Pas besoin de clé API
|
9
|
+
- Rapides et fiables
|
10
|
+
|
11
|
+
### 2. Tests d'intégration
|
12
|
+
|
13
|
+
- Marqués avec `:integration`
|
14
|
+
- Nécessitent `MISTRAL_TEST_API_KEY`
|
15
|
+
- Utilisent la vraie API Mistral
|
16
|
+
|
17
|
+
### 3. Tests VCR
|
18
|
+
|
19
|
+
- Marqués avec `:vcr`
|
20
|
+
- Enregistrent les réponses API
|
21
|
+
- Rejouent les réponses sans nouvelle requête
|
22
|
+
|
23
|
+
## Configuration
|
24
|
+
|
25
|
+
### Variables d'environnement
|
26
|
+
|
27
|
+
```bash
|
28
|
+
# Pour les tests d'intégration (optionnel)
|
29
|
+
export MISTRAL_TEST_API_KEY="votre_cle_de_test"
|
30
|
+
|
31
|
+
# Alternative (utilise votre clé principale)
|
32
|
+
export MISTRAL_API_KEY="votre_cle_principale"
|
33
|
+
```
|
data/SECURITY.md
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
# Sécurité - MistralTranslator
|
2
|
+
|
3
|
+
## 🔒 Améliorations de sécurité implémentées
|
4
|
+
|
5
|
+
### 1. Validation des entrées utilisateur
|
6
|
+
|
7
|
+
#### Validation basique (par défaut)
|
8
|
+
|
9
|
+
- **Longueur des textes** : Limite à 50 000 caractères maximum
|
10
|
+
- **Textes vides** : Acceptés et retournés comme chaîne vide (cas d'usage légitime)
|
11
|
+
- **Validation des batches** : Maximum 20 textes par batch
|
12
|
+
- **Validation des paramètres** : Vérification des types et valeurs
|
13
|
+
|
14
|
+
#### Protection contre les attaques courantes
|
15
|
+
|
16
|
+
- **Injection de code** : Détection des patterns dangereux
|
17
|
+
- **Scripts malveillants** : Filtrage des balises HTML/JavaScript
|
18
|
+
- **Injection SQL** : Détection des patterns SQL malveillants
|
19
|
+
- **Traversal de chemins** : Protection contre les accès non autorisés
|
20
|
+
|
21
|
+
### 2. Rate Limiting côté client
|
22
|
+
|
23
|
+
#### Fonctionnalités
|
24
|
+
|
25
|
+
- **Limite par défaut** : 50 requêtes par minute
|
26
|
+
- **Configuration flexible** : Limites personnalisables
|
27
|
+
- **Thread-safe** : Protection contre les conditions de course
|
28
|
+
- **Attente intelligente** : Délai automatique quand la limite est atteinte
|
29
|
+
|
30
|
+
#### Configuration
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# Via variables d'environnement
|
34
|
+
MISTRAL_RATE_LIMIT_MAX_REQUESTS=100
|
35
|
+
MISTRAL_RATE_LIMIT_WINDOW=60
|
36
|
+
|
37
|
+
# Via code
|
38
|
+
rate_limiter = MistralTranslator::Security::BasicRateLimiter.new(
|
39
|
+
max_requests: 100,
|
40
|
+
window_seconds: 60
|
41
|
+
)
|
42
|
+
```
|
43
|
+
|
44
|
+
### 3. Gestion sécurisée des erreurs
|
45
|
+
|
46
|
+
#### Nouvelles exceptions
|
47
|
+
|
48
|
+
- `SecurityError` : Violations de sécurité détectées
|
49
|
+
- `RateLimitExceededError` : Limite de taux dépassée
|
50
|
+
|
51
|
+
#### Messages d'erreur sécurisés
|
52
|
+
|
53
|
+
- Aucune exposition d'informations sensibles
|
54
|
+
- Messages d'erreur génériques pour éviter les fuites
|
55
|
+
|
56
|
+
### 4. Optimisation de la taille
|
57
|
+
|
58
|
+
#### Réduction significative
|
59
|
+
|
60
|
+
- **Avant** : ~4000+ lignes avec validation complète
|
61
|
+
- **Après** : 3197 lignes avec sécurité essentielle
|
62
|
+
- **Gain** : ~20% de réduction de taille
|
63
|
+
|
64
|
+
#### Architecture modulaire
|
65
|
+
|
66
|
+
- Module de sécurité optionnel et léger
|
67
|
+
- Validation basique par défaut
|
68
|
+
- Possibilité d'extension future
|
69
|
+
|
70
|
+
## 🛡️ Utilisation sécurisée
|
71
|
+
|
72
|
+
### Configuration recommandée
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
MistralTranslator.configure do |config|
|
76
|
+
config.api_key = ENV['MISTRAL_API_KEY']
|
77
|
+
config.enable_metrics = true
|
78
|
+
|
79
|
+
# Callbacks de sécurité
|
80
|
+
config.on_translation_error = lambda do |from, to, error, attempt, timestamp|
|
81
|
+
Rails.logger.error "[Security] Translation failed: #{error.class.name}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
### Cas d'usage légitimes
|
87
|
+
|
88
|
+
- **Textes vides** : Acceptés automatiquement (retournent une chaîne vide)
|
89
|
+
- **Même langue source/cible** : Retourne le texte original sans appel API
|
90
|
+
- **Validation permissive** : Focus sur la sécurité sans bloquer l'usage normal
|
91
|
+
|
92
|
+
### Bonnes pratiques
|
93
|
+
|
94
|
+
1. **Validation côté client** : Toujours valider les entrées avant l'envoi
|
95
|
+
2. **Gestion des erreurs** : Capturer et logger les erreurs de sécurité
|
96
|
+
3. **Monitoring** : Surveiller les tentatives de rate limiting
|
97
|
+
4. **Mise à jour** : Maintenir la gem à jour pour les dernières corrections
|
98
|
+
|
99
|
+
## 🔍 Tests de sécurité
|
100
|
+
|
101
|
+
### Couverture des tests
|
102
|
+
|
103
|
+
- Validation des entrées malveillantes
|
104
|
+
- Tests de rate limiting
|
105
|
+
- Vérification de la thread-safety
|
106
|
+
- Tests d'encodage et de format
|
107
|
+
|
108
|
+
### Exécution des tests
|
109
|
+
|
110
|
+
```bash
|
111
|
+
# Tests de sécurité uniquement
|
112
|
+
bundle exec rspec spec/mistral_translator/security_spec.rb
|
113
|
+
|
114
|
+
# Tests de sécurité existants
|
115
|
+
bundle exec rspec spec/mistral_translator/security_spec.rb
|
116
|
+
```
|
117
|
+
|
118
|
+
## 📊 Métriques de sécurité
|
119
|
+
|
120
|
+
### Indicateurs surveillés
|
121
|
+
|
122
|
+
- Nombre de requêtes bloquées par validation
|
123
|
+
- Fréquence des rate limits
|
124
|
+
- Types d'erreurs de sécurité
|
125
|
+
- Temps de réponse moyen
|
126
|
+
|
127
|
+
### Accès aux métriques
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
# Métriques globales
|
131
|
+
MistralTranslator.metrics
|
132
|
+
|
133
|
+
# Réinitialisation
|
134
|
+
MistralTranslator.reset_metrics!
|
135
|
+
```
|
136
|
+
|
137
|
+
## 🚨 Signaler des vulnérabilités
|
138
|
+
|
139
|
+
Si vous découvrez une vulnérabilité de sécurité :
|
140
|
+
|
141
|
+
1. **Ne pas** ouvrir d'issue publique
|
142
|
+
2. Envoyer un email à : security@mistral-translator.dev
|
143
|
+
3. Inclure : description, étapes de reproduction, impact
|
144
|
+
|
145
|
+
## 📝 Changelog de sécurité
|
146
|
+
|
147
|
+
### Version 0.2.0
|
148
|
+
|
149
|
+
- ✅ Validation basique des entrées
|
150
|
+
- ✅ Rate limiting côté client
|
151
|
+
- ✅ Nouvelles exceptions de sécurité
|
152
|
+
- ✅ Optimisation de la taille de la gem
|
153
|
+
- ✅ Tests de sécurité complets
|
154
|
+
|
155
|
+
---
|
156
|
+
|
157
|
+
_Cette documentation est mise à jour à chaque amélioration de sécurité._
|
data/docs/.nojekyll
ADDED
data/docs/404.html
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="en">
|
3
|
+
<head>
|
4
|
+
<meta charset="UTF-8" />
|
5
|
+
<title>404 - MistralTranslator Docs</title>
|
6
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
|
7
|
+
<meta
|
8
|
+
name="description"
|
9
|
+
content="Page non trouvée - MistralTranslator documentation"
|
10
|
+
/>
|
11
|
+
<meta
|
12
|
+
name="viewport"
|
13
|
+
content="width=device-width, initial-scale=1.0, minimum-scale=1.0"
|
14
|
+
/>
|
15
|
+
<link
|
16
|
+
rel="stylesheet"
|
17
|
+
href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css"
|
18
|
+
/>
|
19
|
+
</head>
|
20
|
+
<body>
|
21
|
+
<div id="app"></div>
|
22
|
+
<script>
|
23
|
+
window.$docsify = {
|
24
|
+
name: "MistralTranslator",
|
25
|
+
repo: "peyochanchan/mistral_translator",
|
26
|
+
};
|
27
|
+
</script>
|
28
|
+
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script>
|
29
|
+
</body>
|
30
|
+
</html>
|