deepl-rb 3.10.0 → 4.0.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.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/CHANGELOG.md +313 -1
  4. data/README.md +225 -309
  5. data/Rakefile +17 -8
  6. data/VERSION +1 -1
  7. data/deepl-rb.gemspec +73 -86
  8. data/lib/deepl/admin_client.rb +39 -14
  9. data/lib/deepl/api.rb +3 -17
  10. data/lib/deepl/app_info.rb +0 -1
  11. data/lib/deepl/client.rb +399 -84
  12. data/lib/deepl/configuration.rb +19 -70
  13. data/lib/deepl/exceptions/authorization_failed.rb +1 -1
  14. data/lib/deepl/exceptions/connection_error.rb +10 -7
  15. data/lib/deepl/exceptions/limit_exceeded.rb +1 -1
  16. data/lib/deepl/exceptions/quota_exceeded.rb +1 -1
  17. data/lib/deepl/exceptions/request_entity_too_large.rb +1 -2
  18. data/lib/deepl/exceptions/request_error.rb +7 -0
  19. data/lib/deepl/exceptions/server_error.rb +2 -1
  20. data/lib/deepl/exceptions/translation_memory_job_error.rb +32 -0
  21. data/lib/deepl/exceptions/validation_error.rb +3 -2
  22. data/lib/deepl/requests/base.rb +129 -51
  23. data/lib/deepl/requests/correct.rb +2 -8
  24. data/lib/deepl/requests/document/upload.rb +29 -26
  25. data/lib/deepl/requests/glossary/base.rb +30 -0
  26. data/lib/deepl/requests/glossary/create.rb +18 -22
  27. data/lib/deepl/requests/glossary/destroy.rb +4 -16
  28. data/lib/deepl/requests/glossary/entries.rb +6 -14
  29. data/lib/deepl/requests/glossary/find.rb +3 -13
  30. data/lib/deepl/requests/glossary/language_pairs.rb +2 -2
  31. data/lib/deepl/requests/glossary/list.rb +2 -2
  32. data/lib/deepl/requests/languages.rb +6 -11
  33. data/lib/deepl/requests/languages_for_resource.rb +2 -0
  34. data/lib/deepl/requests/multilingual_glossary/create.rb +8 -0
  35. data/lib/deepl/requests/multilingual_glossary/destroy_dictionary.rb +4 -0
  36. data/lib/deepl/requests/multilingual_glossary/entries.rb +5 -3
  37. data/lib/deepl/requests/multilingual_glossary/helpers.rb +22 -14
  38. data/lib/deepl/requests/rephrase.rb +3 -1
  39. data/lib/deepl/requests/spoken_terms/create.rb +5 -0
  40. data/lib/deepl/requests/spoken_terms/destroy_term_list.rb +1 -0
  41. data/lib/deepl/requests/spoken_terms/entries.rb +1 -0
  42. data/lib/deepl/requests/spoken_terms/helpers.rb +9 -2
  43. data/lib/deepl/requests/style_profile/create.rb +5 -0
  44. data/lib/deepl/requests/style_profile/list.rb +2 -2
  45. data/lib/deepl/requests/style_rule/create.rb +25 -1
  46. data/lib/deepl/requests/style_rule/create_custom_instruction.rb +7 -0
  47. data/lib/deepl/requests/style_rule/helpers.rb +13 -4
  48. data/lib/deepl/requests/style_rule/list.rb +2 -0
  49. data/lib/deepl/requests/style_rule/update.rb +1 -0
  50. data/lib/deepl/requests/style_rule/update_custom_instruction.rb +2 -0
  51. data/lib/deepl/requests/translate.rb +20 -36
  52. data/lib/deepl/requests/translation_memory/create_export.rb +6 -1
  53. data/lib/deepl/requests/translation_memory/create_import.rb +10 -0
  54. data/lib/deepl/requests/translation_memory/download_export.rb +0 -4
  55. data/lib/deepl/requests/translation_memory/list.rb +2 -2
  56. data/lib/deepl/requests/translation_memory/segments.rb +2 -2
  57. data/lib/deepl/requests/translation_memory/storage_base.rb +5 -9
  58. data/lib/deepl/requests/write_improvements.rb +7 -0
  59. data/lib/deepl/resources/document_handle.rb +1 -26
  60. data/lib/deepl/resources/document_translation_status.rb +0 -12
  61. data/lib/deepl/resources/language.rb +4 -10
  62. data/lib/deepl/resources/multilingual_glossary.rb +4 -4
  63. data/lib/deepl/resources/style_rule.rb +1 -23
  64. data/lib/deepl/resources/translation_memory_job.rb +3 -4
  65. data/lib/deepl/resources/usage.rb +5 -38
  66. data/lib/deepl/utils/client_builder.rb +44 -9
  67. data/lib/deepl/utils/glossary_entries.rb +30 -0
  68. data/lib/deepl/utils/http_client_builder.rb +50 -13
  69. data/lib/deepl/utils/output_path.rb +25 -0
  70. data/lib/deepl/utils/term_validator.rb +3 -3
  71. data/lib/deepl/utils/translation_memory_operations.rb +230 -0
  72. data/lib/deepl.rb +6 -171
  73. data/lib/http_client_options.rb +2 -2
  74. data/lib/version.rb +1 -1
  75. data/upgrading_to_v4.md +374 -0
  76. metadata +9 -50
  77. data/.gitlab-ci.yml +0 -197
  78. data/lib/deepl/deprecation.rb +0 -42
  79. data/lib/deepl/document_api.rb +0 -150
  80. data/lib/deepl/exceptions/not_supported.rb +0 -20
  81. data/lib/deepl/glossary_api.rb +0 -38
  82. data/lib/deepl/style_rule_api.rb +0 -72
  83. data/lib/deepl/translation_memory_api.rb +0 -389
  84. data/license_checker.sh +0 -8
  85. data/spec/fixtures.rb +0 -33
  86. data/spec/offline/admin_client_spec.rb +0 -551
  87. data/spec/offline/client_spec.rb +0 -273
  88. data/spec/offline/configuration_spec.rb +0 -164
  89. data/spec/offline/constants_spec.rb +0 -180
  90. data/spec/offline/correct_text_spec.rb +0 -50
  91. data/spec/offline/deprecation_spec.rb +0 -32
  92. data/spec/offline/errors_spec.rb +0 -92
  93. data/spec/offline/glossaries_spec.rb +0 -98
  94. data/spec/offline/http_client_spec.rb +0 -319
  95. data/spec/offline/languages_spec.rb +0 -155
  96. data/spec/offline/module_methods_spec.rb +0 -13
  97. data/spec/offline/multilingual_glossaries_spec.rb +0 -358
  98. data/spec/offline/offline_helper.rb +0 -71
  99. data/spec/offline/rephrase_text_spec.rb +0 -73
  100. data/spec/offline/spoken_terms_spec.rb +0 -215
  101. data/spec/offline/style_profiles_spec.rb +0 -266
  102. data/spec/offline/style_rules_spec.rb +0 -318
  103. data/spec/offline/translate_document_spec.rb +0 -606
  104. data/spec/offline/translate_text_spec.rb +0 -286
  105. data/spec/offline/translation_memories_client_spec.rb +0 -256
  106. data/spec/offline/translation_memories_spec.rb +0 -463
  107. data/spec/offline/usage_spec.rb +0 -151
  108. data/spec/real_api/admin_client_spec.rb +0 -77
  109. data/spec/real_api/correct_text_spec.rb +0 -20
  110. data/spec/real_api/glossaries_spec.rb +0 -28
  111. data/spec/real_api/languages_spec.rb +0 -27
  112. data/spec/real_api/multilingual_glossaries_spec.rb +0 -53
  113. data/spec/real_api/proxy_spec.rb +0 -41
  114. data/spec/real_api/real_api_helper.rb +0 -65
  115. data/spec/real_api/rephrase_text_spec.rb +0 -16
  116. data/spec/real_api/spoken_terms_spec.rb +0 -56
  117. data/spec/real_api/style_profiles_spec.rb +0 -46
  118. data/spec/real_api/style_rules_spec.rb +0 -46
  119. data/spec/real_api/translate_document_spec.rb +0 -18
  120. data/spec/real_api/translate_text_spec.rb +0 -16
  121. data/spec/real_api/translation_memories_spec.rb +0 -56
  122. data/spec/real_api/usage_spec.rb +0 -20
  123. data/spec/spec_helper.rb +0 -13
@@ -1,318 +0,0 @@
1
- # Copyright 2026 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE file.
4
- # frozen_string_literal: true
5
-
6
- require_relative 'offline_helper'
7
-
8
- describe 'style rules' do
9
- describe DeepL::StyleRuleApi do
10
- let(:style_rules) { DeepL.style_rules }
11
- let(:style_json) do
12
- { style_id: 's-1', name: 'house style', language: 'en', version: 3,
13
- creation_time: '2026-01-02T03:04:05Z', updated_time: '2026-02-03T04:05:06Z',
14
- configured_rules: { numbers: { number_format: 'dot' } },
15
- custom_instructions: [{ id: 'i-1', label: 'Tone', prompt: 'Be brief',
16
- source_language: 'en' }] }
17
- end
18
- let(:instruction_json) { { id: 'i-1', label: 'Tone', prompt: 'Be brief' } }
19
-
20
- describe '#list' do
21
- it 'sends a GET without a query by default' do
22
- stub_request(:get, url('/v3/style_rules')).with(headers: json_headers)
23
- .to_return(reply(style_rules: [style_json]))
24
-
25
- style_rules.list
26
- end
27
-
28
- it 'sends page, page_size and detailed in the query' do
29
- stub_request(:get, url('/v3/style_rules?page=2&page_size=5&detailed=true'))
30
- .to_return(reply(style_rules: []))
31
-
32
- style_rules.list(page: 2, page_size: 5, detailed: true)
33
- end
34
-
35
- it 'parses the timestamps of the style rules' do
36
- stub_request(:get, url('/v3/style_rules')).to_return(reply(style_rules: [style_json]))
37
-
38
- rule = style_rules.list.first
39
-
40
- expect([rule.creation_time, rule.updated_time])
41
- .to eq([Time.utc(2026, 1, 2, 3, 4, 5), Time.utc(2026, 2, 3, 4, 5, 6)])
42
- end
43
-
44
- it 'raises a DeepL error for a malformed timestamp' do
45
- stub_request(:get, url('/v3/style_rules'))
46
- .to_return(reply(style_rules: [style_json.merge(creation_time: 'not a time')]))
47
-
48
- expect { style_rules.list }.to raise_error(DeepL::Exceptions::Error, /invalid time/)
49
- end
50
- end
51
-
52
- describe '#create' do
53
- it 'sends the name and language' do
54
- stub_request(:post, url('/v3/style_rules'))
55
- .with(body: { name: 'house style', language: 'en' }, headers: json_headers)
56
- .to_return(reply(style_json, 201))
57
-
58
- style_rules.create('house style', 'en')
59
- end
60
-
61
- it 'sends the configured rules and the custom instructions' do
62
- rules = { numbers: { number_format: 'dot' } }
63
- instructions = [{ label: 'Tone', prompt: 'Be brief' }]
64
- stub_request(:post, url('/v3/style_rules'))
65
- .with(body: { name: 'n', language: 'en', configured_rules: rules,
66
- custom_instructions: instructions })
67
- .to_return(reply(style_json, 201))
68
-
69
- style_rules.create('n', 'en', configured_rules: rules, custom_instructions: instructions)
70
- end
71
-
72
- it 'keeps the configured rules and custom instructions in a reused options hash' do
73
- options = { configured_rules: { numbers: { number_format: 'dot' } },
74
- custom_instructions: [{ label: 'Tone', prompt: 'Be brief' }] }
75
- stub_request(:post, url('/v3/style_rules'))
76
- .with(body: { name: 'n', language: 'en' }.merge(options))
77
- .to_return(reply(style_json, 201)).times(2)
78
-
79
- 2.times { style_rules.create('n', 'en', options) }
80
- end
81
-
82
- it 'parses the configured rules, defaulting missing rules to an empty hash' do
83
- stub_request(:post, url('/v3/style_rules')).to_return(reply(style_json, 201))
84
-
85
- rule = style_rules.create('n', 'en')
86
-
87
- expect([rule.configured_rules.vocabulary, rule.custom_instructions.map(&:class)])
88
- .to eq([{}, [DeepL::Resources::CustomInstruction]])
89
- end
90
- end
91
-
92
- describe '#find' do
93
- it 'sends a GET without a body' do
94
- stub_request(:get, url('/v3/style_rules/s-1')).with(headers: auth_headers) { bodyless?(_1) }
95
- .to_return(reply(style_json))
96
-
97
- style_rules.find('s-1')
98
- end
99
- end
100
-
101
- describe 'IDs in the path' do
102
- it 'URL-encodes the style rule ID and the custom instruction ID' do
103
- stub_request(:get, url('/v3/style_rules/a%2Fb%20c')).to_return(reply(style_json))
104
- stub_request(:delete, url('/v3/style_rules/a%2Fb%20c/custom_instructions/i%2F1%20x'))
105
- .to_return(status: 204)
106
-
107
- style_rules.find('a/b c')
108
- style_rules.destroy_custom_instruction('a/b c', 'i/1 x')
109
- end
110
- end
111
-
112
- describe 'resource objects' do
113
- let(:rule) { DeepL::Resources::StyleRule.new({ 'style_id' => 's-1', 'name' => 'n' }, nil, nil) }
114
- let(:instruction) { DeepL::Resources::CustomInstruction.new('id' => 'i-1') }
115
-
116
- it 'sends the ID of a StyleRule object' do
117
- stub_request(:get, url('/v3/style_rules/s-1')).to_return(reply(style_json))
118
- stub_request(:patch, url('/v3/style_rules/s-1')).to_return(reply(style_json))
119
- stub_request(:put, url('/v3/style_rules/s-1/configured_rules')).to_return(reply(style_json))
120
- stub_request(:delete, url('/v3/style_rules/s-1')).to_return(status: 204)
121
-
122
- style_rules.find(rule)
123
- style_rules.update_name(rule, 'new')
124
- style_rules.update_configured_rules(rule, {})
125
- style_rules.destroy(rule)
126
- end
127
-
128
- it 'sends the IDs of StyleRule and CustomInstruction objects' do
129
- path = url('/v3/style_rules/s-1/custom_instructions/i-1')
130
- stub_request(:post, url('/v3/style_rules/s-1/custom_instructions'))
131
- .to_return(reply(instruction_json, 201))
132
- stub_request(:get, path).to_return(reply(instruction_json))
133
- stub_request(:put, path).to_return(reply(instruction_json))
134
- stub_request(:delete, path).to_return(status: 204)
135
-
136
- style_rules.create_custom_instruction(rule, 'Tone', 'Be brief')
137
- style_rules.find_custom_instruction(rule, instruction)
138
- style_rules.update_custom_instruction(rule, instruction, 'New', 'Be long')
139
- style_rules.destroy_custom_instruction(rule, instruction)
140
- end
141
- end
142
-
143
- describe '#update_name' do
144
- it 'sends a PATCH with the name' do
145
- stub_request(:patch, url('/v3/style_rules/s-1'))
146
- .with(body: { name: 'new' }, headers: json_headers).to_return(reply(style_json))
147
-
148
- style_rules.update_name('s-1', 'new')
149
- end
150
- end
151
-
152
- describe '#update_configured_rules' do
153
- it 'sends the configured rules as the PUT body' do
154
- rules = { numbers: { number_format: 'comma' } }
155
- stub_request(:put, url('/v3/style_rules/s-1/configured_rules'))
156
- .with(body: rules, headers: json_headers).to_return(reply(style_json))
157
-
158
- style_rules.update_configured_rules('s-1', rules)
159
- end
160
- end
161
-
162
- describe '#destroy' do
163
- it 'sends a DELETE without a body and returns the ID' do
164
- stub_request(:delete, url('/v3/style_rules/s-1'))
165
- .with(headers: auth_headers) { bodyless?(_1) }
166
- .to_return(status: 204)
167
-
168
- expect(style_rules.destroy('s-1')).to eq('s-1')
169
- end
170
- end
171
-
172
- describe 'custom instructions' do
173
- it 'sends the label and prompt on create, with the source language when given' do
174
- stub_request(:post, url('/v3/style_rules/s-1/custom_instructions'))
175
- .with(body: { label: 'Tone', prompt: 'Be brief' }, headers: json_headers)
176
- .to_return(reply(instruction_json, 201))
177
- stub_request(:post, url('/v3/style_rules/s-1/custom_instructions'))
178
- .with(body: { label: 'Tone', prompt: 'Be brief', source_language: 'de' })
179
- .to_return(reply(instruction_json.merge(source_language: 'de'), 201))
180
-
181
- style_rules.create_custom_instruction('s-1', 'Tone', 'Be brief')
182
- style_rules.create_custom_instruction('s-1', 'Tone', 'Be brief', 'de')
183
- end
184
-
185
- it 'sends a GET on find' do
186
- stub_request(:get, url('/v3/style_rules/s-1/custom_instructions/i-1'))
187
- .with(headers: auth_headers) { bodyless?(_1) }.to_return(reply(instruction_json))
188
-
189
- style_rules.find_custom_instruction('s-1', 'i-1')
190
- end
191
-
192
- it 'sends the label and prompt on update, with the source language when given' do
193
- stub_request(:put, url('/v3/style_rules/s-1/custom_instructions/i-1'))
194
- .with(body: { label: 'New', prompt: 'Be long' }, headers: json_headers)
195
- .to_return(reply(instruction_json))
196
- stub_request(:put, url('/v3/style_rules/s-1/custom_instructions/i-1'))
197
- .with(body: { label: 'New', prompt: 'Be long', source_language: 'de' })
198
- .to_return(reply(instruction_json))
199
-
200
- style_rules.update_custom_instruction('s-1', 'i-1', 'New', 'Be long')
201
- style_rules.update_custom_instruction('s-1', 'i-1', 'New', 'Be long', 'de')
202
- end
203
-
204
- it 'sends a DELETE on destroy and returns the instruction ID' do
205
- stub_request(:delete, url('/v3/style_rules/s-1/custom_instructions/i-1'))
206
- .with(headers: auth_headers) { bodyless?(_1) }.to_return(status: 204)
207
-
208
- expect(style_rules.destroy_custom_instruction('s-1', 'i-1')).to eq('i-1')
209
- end
210
- end
211
- end
212
-
213
- # The client has its own key and server, different from the module configuration, so a request
214
- # that falls back to module state hits no stub.
215
- describe 'DeepL::Client' do
216
- let(:client) { DeepL::Client.new('client-key', server_url: 'https://client.example.test') }
217
- let(:style_json) { { style_id: 's-1', name: 'house style', language: 'en', version: 1 } }
218
- let(:instruction_json) { { id: 'i-1', label: 'Tone', prompt: 'Be brief' } }
219
- let(:rule) { DeepL::Resources::StyleRule.new({ 'style_id' => 's-1', 'name' => 'n' }, nil, nil) }
220
- let(:instruction) { DeepL::Resources::CustomInstruction.new('id' => 'i-1') }
221
-
222
- def url(path)
223
- "https://client.example.test#{path}"
224
- end
225
-
226
- def json_headers
227
- { 'Authorization' => 'DeepL-Auth-Key client-key', 'Content-Type' => 'application/json' }
228
- end
229
-
230
- def auth_headers
231
- { 'Authorization' => 'DeepL-Auth-Key client-key' }
232
- end
233
-
234
- describe '#get_all_style_rules' do
235
- it 'sends no query by default' do
236
- stub_request(:get, url('/v3/style_rules')).with(headers: json_headers)
237
- .to_return(reply(style_rules: [style_json]))
238
-
239
- expect(client.get_all_style_rules.map(&:style_id)).to eq(['s-1'])
240
- end
241
-
242
- it 'sends page, page_size and detailed in the query' do
243
- stub_request(:get, url('/v3/style_rules?page=1&page_size=5&detailed=false'))
244
- .to_return(reply(style_rules: []))
245
-
246
- client.get_all_style_rules(page: 1, page_size: 5, detailed: false)
247
- end
248
- end
249
-
250
- describe '#create_style_rule' do
251
- it 'sends the name and language only by default' do
252
- stub_request(:post, url('/v3/style_rules'))
253
- .with(body: { name: 'n', language: 'en' }, headers: json_headers)
254
- .to_return(reply(style_json, 201))
255
-
256
- expect(client.create_style_rule('n', 'en').style_id).to eq('s-1')
257
- end
258
-
259
- it 'sends the configured rules, custom instructions and extra body parameters' do
260
- rules = { numbers: { number_format: 'dot' } }
261
- instructions = [{ label: 'Tone', prompt: 'Be brief', source_language: 'en' }]
262
- stub_request(:post, url('/v3/style_rules'))
263
- .with(body: { name: 'n', language: 'en', configured_rules: rules,
264
- custom_instructions: instructions, extra: 'x' })
265
- .to_return(reply(style_json, 201))
266
-
267
- client.create_style_rule('n', 'en', configured_rules: rules,
268
- custom_instructions: instructions,
269
- extra_body_parameters: { extra: 'x' })
270
- end
271
- end
272
-
273
- describe 'per-list operations' do
274
- it 'sends get, rename, configured rules update and delete for an ID or a StyleRule' do
275
- stub_request(:get, url('/v3/style_rules/s-1')).to_return(reply(style_json))
276
- stub_request(:patch, url('/v3/style_rules/s-1')).with(body: { name: 'new' })
277
- .to_return(reply(style_json))
278
- stub_request(:put, url('/v3/style_rules/s-1/configured_rules'))
279
- .with(body: { numbers: { number_format: 'comma' } }).to_return(reply(style_json))
280
- stub_request(:delete, url('/v3/style_rules/s-1')).to_return(status: 204)
281
-
282
- client.get_style_rule(rule)
283
- client.update_style_rule_name('s-1', 'new')
284
- client.update_style_rule_configured_rules(rule, { numbers: { number_format: 'comma' } })
285
- expect(client.delete_style_rule(rule)).to be_nil
286
- end
287
- end
288
-
289
- describe 'custom instructions' do
290
- let(:path) { url('/v3/style_rules/s-1/custom_instructions/i-1') }
291
-
292
- it 'sends create with the source language only when given' do
293
- stub_request(:post, url('/v3/style_rules/s-1/custom_instructions'))
294
- .with(body: { label: 'Tone', prompt: 'Be brief' }).to_return(reply(instruction_json, 201))
295
- stub_request(:post, url('/v3/style_rules/s-1/custom_instructions'))
296
- .with(body: { label: 'Tone', prompt: 'Be brief', source_language: 'de' })
297
- .to_return(reply(instruction_json, 201))
298
-
299
- client.create_style_rule_custom_instruction(rule, 'Tone', 'Be brief')
300
- client.create_style_rule_custom_instruction('s-1', 'Tone', 'Be brief',
301
- source_language: 'de')
302
- end
303
-
304
- it 'sends get, update and delete for an ID or a CustomInstruction' do
305
- stub_request(:get, path).with(headers: auth_headers).to_return(reply(instruction_json))
306
- stub_request(:put, path)
307
- .with(body: { label: 'New', prompt: 'Be long', source_language: 'de' })
308
- .to_return(reply(instruction_json))
309
- stub_request(:delete, path).to_return(status: 204)
310
-
311
- expect(client.get_style_rule_custom_instruction(rule, instruction).id).to eq('i-1')
312
- client.update_style_rule_custom_instruction('s-1', 'i-1', 'New', 'Be long',
313
- source_language: 'de')
314
- expect(client.delete_style_rule_custom_instruction(rule, instruction)).to be_nil
315
- end
316
- end
317
- end
318
- end