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.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +313 -1
- data/README.md +225 -309
- data/Rakefile +17 -8
- data/VERSION +1 -1
- data/deepl-rb.gemspec +73 -86
- data/lib/deepl/admin_client.rb +39 -14
- data/lib/deepl/api.rb +3 -17
- data/lib/deepl/app_info.rb +0 -1
- data/lib/deepl/client.rb +399 -84
- data/lib/deepl/configuration.rb +19 -70
- data/lib/deepl/exceptions/authorization_failed.rb +1 -1
- data/lib/deepl/exceptions/connection_error.rb +10 -7
- data/lib/deepl/exceptions/limit_exceeded.rb +1 -1
- data/lib/deepl/exceptions/quota_exceeded.rb +1 -1
- data/lib/deepl/exceptions/request_entity_too_large.rb +1 -2
- data/lib/deepl/exceptions/request_error.rb +7 -0
- data/lib/deepl/exceptions/server_error.rb +2 -1
- data/lib/deepl/exceptions/translation_memory_job_error.rb +32 -0
- data/lib/deepl/exceptions/validation_error.rb +3 -2
- data/lib/deepl/requests/base.rb +129 -51
- data/lib/deepl/requests/correct.rb +2 -8
- data/lib/deepl/requests/document/upload.rb +29 -26
- data/lib/deepl/requests/glossary/base.rb +30 -0
- data/lib/deepl/requests/glossary/create.rb +18 -22
- data/lib/deepl/requests/glossary/destroy.rb +4 -16
- data/lib/deepl/requests/glossary/entries.rb +6 -14
- data/lib/deepl/requests/glossary/find.rb +3 -13
- data/lib/deepl/requests/glossary/language_pairs.rb +2 -2
- data/lib/deepl/requests/glossary/list.rb +2 -2
- data/lib/deepl/requests/languages.rb +6 -11
- data/lib/deepl/requests/languages_for_resource.rb +2 -0
- data/lib/deepl/requests/multilingual_glossary/create.rb +8 -0
- data/lib/deepl/requests/multilingual_glossary/destroy_dictionary.rb +4 -0
- data/lib/deepl/requests/multilingual_glossary/entries.rb +5 -3
- data/lib/deepl/requests/multilingual_glossary/helpers.rb +22 -14
- data/lib/deepl/requests/rephrase.rb +3 -1
- data/lib/deepl/requests/spoken_terms/create.rb +5 -0
- data/lib/deepl/requests/spoken_terms/destroy_term_list.rb +1 -0
- data/lib/deepl/requests/spoken_terms/entries.rb +1 -0
- data/lib/deepl/requests/spoken_terms/helpers.rb +9 -2
- data/lib/deepl/requests/style_profile/create.rb +5 -0
- data/lib/deepl/requests/style_profile/list.rb +2 -2
- data/lib/deepl/requests/style_rule/create.rb +25 -1
- data/lib/deepl/requests/style_rule/create_custom_instruction.rb +7 -0
- data/lib/deepl/requests/style_rule/helpers.rb +13 -4
- data/lib/deepl/requests/style_rule/list.rb +2 -0
- data/lib/deepl/requests/style_rule/update.rb +1 -0
- data/lib/deepl/requests/style_rule/update_custom_instruction.rb +2 -0
- data/lib/deepl/requests/translate.rb +20 -36
- data/lib/deepl/requests/translation_memory/create_export.rb +6 -1
- data/lib/deepl/requests/translation_memory/create_import.rb +10 -0
- data/lib/deepl/requests/translation_memory/download_export.rb +0 -4
- data/lib/deepl/requests/translation_memory/list.rb +2 -2
- data/lib/deepl/requests/translation_memory/segments.rb +2 -2
- data/lib/deepl/requests/translation_memory/storage_base.rb +5 -9
- data/lib/deepl/requests/write_improvements.rb +7 -0
- data/lib/deepl/resources/document_handle.rb +1 -26
- data/lib/deepl/resources/document_translation_status.rb +0 -12
- data/lib/deepl/resources/language.rb +4 -10
- data/lib/deepl/resources/multilingual_glossary.rb +4 -4
- data/lib/deepl/resources/style_rule.rb +1 -23
- data/lib/deepl/resources/translation_memory_job.rb +3 -4
- data/lib/deepl/resources/usage.rb +5 -38
- data/lib/deepl/utils/client_builder.rb +44 -9
- data/lib/deepl/utils/glossary_entries.rb +30 -0
- data/lib/deepl/utils/http_client_builder.rb +50 -13
- data/lib/deepl/utils/output_path.rb +25 -0
- data/lib/deepl/utils/term_validator.rb +3 -3
- data/lib/deepl/utils/translation_memory_operations.rb +230 -0
- data/lib/deepl.rb +6 -171
- data/lib/http_client_options.rb +2 -2
- data/lib/version.rb +1 -1
- data/upgrading_to_v4.md +374 -0
- metadata +9 -50
- data/.gitlab-ci.yml +0 -197
- data/lib/deepl/deprecation.rb +0 -42
- data/lib/deepl/document_api.rb +0 -150
- data/lib/deepl/exceptions/not_supported.rb +0 -20
- data/lib/deepl/glossary_api.rb +0 -38
- data/lib/deepl/style_rule_api.rb +0 -72
- data/lib/deepl/translation_memory_api.rb +0 -389
- data/license_checker.sh +0 -8
- data/spec/fixtures.rb +0 -33
- data/spec/offline/admin_client_spec.rb +0 -551
- data/spec/offline/client_spec.rb +0 -273
- data/spec/offline/configuration_spec.rb +0 -164
- data/spec/offline/constants_spec.rb +0 -180
- data/spec/offline/correct_text_spec.rb +0 -50
- data/spec/offline/deprecation_spec.rb +0 -32
- data/spec/offline/errors_spec.rb +0 -92
- data/spec/offline/glossaries_spec.rb +0 -98
- data/spec/offline/http_client_spec.rb +0 -319
- data/spec/offline/languages_spec.rb +0 -155
- data/spec/offline/module_methods_spec.rb +0 -13
- data/spec/offline/multilingual_glossaries_spec.rb +0 -358
- data/spec/offline/offline_helper.rb +0 -71
- data/spec/offline/rephrase_text_spec.rb +0 -73
- data/spec/offline/spoken_terms_spec.rb +0 -215
- data/spec/offline/style_profiles_spec.rb +0 -266
- data/spec/offline/style_rules_spec.rb +0 -318
- data/spec/offline/translate_document_spec.rb +0 -606
- data/spec/offline/translate_text_spec.rb +0 -286
- data/spec/offline/translation_memories_client_spec.rb +0 -256
- data/spec/offline/translation_memories_spec.rb +0 -463
- data/spec/offline/usage_spec.rb +0 -151
- data/spec/real_api/admin_client_spec.rb +0 -77
- data/spec/real_api/correct_text_spec.rb +0 -20
- data/spec/real_api/glossaries_spec.rb +0 -28
- data/spec/real_api/languages_spec.rb +0 -27
- data/spec/real_api/multilingual_glossaries_spec.rb +0 -53
- data/spec/real_api/proxy_spec.rb +0 -41
- data/spec/real_api/real_api_helper.rb +0 -65
- data/spec/real_api/rephrase_text_spec.rb +0 -16
- data/spec/real_api/spoken_terms_spec.rb +0 -56
- data/spec/real_api/style_profiles_spec.rb +0 -46
- data/spec/real_api/style_rules_spec.rb +0 -46
- data/spec/real_api/translate_document_spec.rb +0 -18
- data/spec/real_api/translate_text_spec.rb +0 -16
- data/spec/real_api/translation_memories_spec.rb +0 -56
- data/spec/real_api/usage_spec.rb +0 -20
- data/spec/spec_helper.rb +0 -13
data/spec/offline/client_spec.rb
DELETED
|
@@ -1,273 +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 DeepL::Client do
|
|
9
|
-
# The operations are added to DeepL::Client separately; until then the client's connection is
|
|
10
|
-
# exercised with the usage request.
|
|
11
|
-
def usage(client)
|
|
12
|
-
DeepL::Requests::Usage.new(client.send(:api)).request
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def stub_usage(base = OfflineHelper::HOST, headers: {})
|
|
16
|
-
stub = stub_request(:get, "#{base}/v2/usage")
|
|
17
|
-
stub = stub.with(headers: headers) unless headers.empty?
|
|
18
|
-
stub.to_return(usage_reply)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe '#initialize' do
|
|
22
|
-
it 'sends the trimmed auth key to the Pro server' do
|
|
23
|
-
stub_usage(headers: { 'Authorization' => 'DeepL-Auth-Key abc' })
|
|
24
|
-
|
|
25
|
-
usage(described_class.new(" abc \n"))
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it 'sends a Free key to the Free server' do
|
|
29
|
-
stub_usage('https://api-free.deepl.com',
|
|
30
|
-
headers: { 'Authorization' => 'DeepL-Auth-Key a:fx' })
|
|
31
|
-
|
|
32
|
-
usage(described_class.new('a:fx'))
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
it 'keeps the path prefix of the server URL' do
|
|
36
|
-
stub_usage('http://localhost:3000/proxy')
|
|
37
|
-
|
|
38
|
-
usage(described_class.new('abc', server_url: 'http://localhost:3000/proxy'))
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
it 'reads no environment variables' do
|
|
42
|
-
env = ENV.to_h.slice('DEEPL_AUTH_KEY', 'DEEPL_SERVER_URL')
|
|
43
|
-
ENV['DEEPL_SERVER_URL'] = 'http://environment.example.test'
|
|
44
|
-
stub_usage(headers: { 'Authorization' => 'DeepL-Auth-Key abc' })
|
|
45
|
-
|
|
46
|
-
usage(described_class.new('abc'))
|
|
47
|
-
ensure
|
|
48
|
-
ENV.delete('DEEPL_SERVER_URL')
|
|
49
|
-
ENV.update(env)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
it 'rejects an empty or blank auth key' do
|
|
53
|
-
['', " \t", nil].each do |key|
|
|
54
|
-
expect { described_class.new(key) }.to raise_error(ArgumentError, /auth_key/)
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
it 'does not show the auth key in inspect' do
|
|
59
|
-
expect(described_class.new('secret-key').inspect).not_to include('secret-key')
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it 'does not expose its connection or configuration' do
|
|
63
|
-
client = described_class.new('abc')
|
|
64
|
-
|
|
65
|
-
expect(client).not_to respond_to(:api)
|
|
66
|
-
expect(client).not_to respond_to(:configuration)
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
describe 'headers' do
|
|
71
|
-
it 'lets the headers replace the Authorization header' do
|
|
72
|
-
stub_usage(headers: { 'Authorization' => 'DeepL-Auth-Key other', 'X-Custom' => 'v' })
|
|
73
|
-
|
|
74
|
-
usage(described_class.new('abc', headers: { 'X-Custom' => 'v',
|
|
75
|
-
'authorization' => 'DeepL-Auth-Key other' }))
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
it 'replaces the User-Agent with the one in headers' do
|
|
79
|
-
stub_usage(headers: { 'User-Agent' => 'my-agent/1' })
|
|
80
|
-
|
|
81
|
-
usage(described_class.new('abc', headers: { 'user-agent' => 'my-agent/1' }))
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
it 'adds the app info to the User-Agent' do
|
|
85
|
-
stub_usage(headers: { 'User-Agent' => %r{\Adeepl-ruby/[^ ]+ my-app/1\.2\z} })
|
|
86
|
-
|
|
87
|
-
usage(described_class.new('abc', send_platform_info: false,
|
|
88
|
-
app_info: { app_name: 'my-app', app_version: '1.2' }))
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
it 'never mixes the options of two clients' do
|
|
92
|
-
stub_usage(headers: { 'Authorization' => 'DeepL-Auth-Key one', 'X-Client' => '1' })
|
|
93
|
-
stub_usage('http://localhost:3000',
|
|
94
|
-
headers: { 'Authorization' => 'DeepL-Auth-Key two', 'X-Client' => '2' })
|
|
95
|
-
|
|
96
|
-
usage(described_class.new('one', headers: { 'X-Client' => '1' }))
|
|
97
|
-
usage(described_class.new('two', server_url: 'http://localhost:3000',
|
|
98
|
-
headers: { 'X-Client' => '2' }))
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
describe 'max_retries' do
|
|
103
|
-
it 'retries as often as configured' do
|
|
104
|
-
stub = stub_request(:get, url('/v2/usage')).to_return(reply({}, 429), usage_reply)
|
|
105
|
-
|
|
106
|
-
usage(described_class.new('abc', max_retries: 1))
|
|
107
|
-
expect(stub).to have_been_requested.twice
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
it 'does not retry with max_retries 0' do
|
|
111
|
-
stub = stub_request(:get, url('/v2/usage')).to_return(reply({}, 503))
|
|
112
|
-
|
|
113
|
-
client = described_class.new('abc', max_retries: 0)
|
|
114
|
-
expect { usage(client) }.to raise_error(DeepL::Exceptions::ServerError)
|
|
115
|
-
expect(stub).to have_been_requested.once
|
|
116
|
-
end
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
describe 'timeout' do
|
|
120
|
-
it 'applies the timeout to each attempt' do
|
|
121
|
-
stub_usage
|
|
122
|
-
client = described_class.new('abc', timeout: 3)
|
|
123
|
-
|
|
124
|
-
usage(client)
|
|
125
|
-
|
|
126
|
-
http = client.send(:api).http_client
|
|
127
|
-
expect([http.open_timeout, http.read_timeout, http.write_timeout]).to eq([3.0, 3.0, 3.0])
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
it 'defaults to 10 seconds and keeps the timeouts of the HTTP client options' do
|
|
131
|
-
stub_usage
|
|
132
|
-
options = DeepL::HTTPClientOptions.new({}, nil, read_timeout: 7)
|
|
133
|
-
client = described_class.new('abc', http_client_options: options)
|
|
134
|
-
|
|
135
|
-
usage(client)
|
|
136
|
-
|
|
137
|
-
http = client.send(:api).http_client
|
|
138
|
-
expect([http.open_timeout, http.read_timeout, http.write_timeout]).to eq([10.0, 7, 10.0])
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
describe 'proxy' do
|
|
143
|
-
def http_client(**options)
|
|
144
|
-
described_class.new('abc', **options).send(:api).http_client
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
it 'accepts a proxy URL with credentials' do
|
|
148
|
-
expect(http_client(proxy: 'http://us%40r:p%3Ass@proxy.example.test:3128'))
|
|
149
|
-
.to have_attributes(proxy_address: 'proxy.example.test', proxy_port: 3128,
|
|
150
|
-
proxy_user: 'us@r', proxy_pass: 'p:ss')
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
it 'combines the proxy URL with the other HTTP client options' do
|
|
154
|
-
options = DeepL::HTTPClientOptions.new({}, nil, enable_ssl_verification: false)
|
|
155
|
-
|
|
156
|
-
expect(http_client(proxy: 'http://proxy.example.test:3128', http_client_options: options))
|
|
157
|
-
.to have_attributes(proxy_address: 'proxy.example.test',
|
|
158
|
-
verify_mode: OpenSSL::SSL::VERIFY_NONE)
|
|
159
|
-
end
|
|
160
|
-
|
|
161
|
-
it 'rejects a proxy in both proxy: and http_client_options:' do
|
|
162
|
-
options = DeepL::HTTPClientOptions.new('http://other.example.test:1')
|
|
163
|
-
|
|
164
|
-
expect { http_client(proxy: 'http://proxy.example.test:3128', http_client_options: options) }
|
|
165
|
-
.to raise_error(ArgumentError, /proxy/)
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
it 'does not show the proxy credentials in inspect' do
|
|
169
|
-
client = described_class.new('abc', proxy: 'http://u:secret@proxy.test:3128')
|
|
170
|
-
|
|
171
|
-
expect(client.inspect).not_to include('secret')
|
|
172
|
-
expect(client.send(:api).inspect).not_to include('secret')
|
|
173
|
-
expect(client.send(:api).http_client_options.inspect).to include('proxy.test:3128')
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
it 'does not show the proxy credentials in the error for an invalid proxy URL' do
|
|
177
|
-
['u:secret@proxy.test:3128', 'http://u:secret@proxy test:3128'].each do |proxy|
|
|
178
|
-
expect { described_class.new('abc', proxy: proxy) }
|
|
179
|
-
.to raise_error(ArgumentError) { |e| expect(e.full_message).not_to include('secret') }
|
|
180
|
-
end
|
|
181
|
-
end
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
describe '.free_account_auth_key?' do
|
|
185
|
-
it 'detects Free keys' do
|
|
186
|
-
expect(%w[a:fx a].map { |key| described_class.free_account_auth_key?(key) })
|
|
187
|
-
.to eq([true, false])
|
|
188
|
-
expect(described_class.free_account_auth_key?(nil)).to be(false)
|
|
189
|
-
end
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
describe '#translate_text' do
|
|
193
|
-
let(:client) { described_class.new(OfflineHelper::AUTH_KEY) }
|
|
194
|
-
|
|
195
|
-
def stub_translate(body, translations)
|
|
196
|
-
stub_request(:post, url('/v2/translate'))
|
|
197
|
-
.with(body: body, headers: json_headers)
|
|
198
|
-
.to_return(reply(translations: translations))
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
it 'sends the text, the options and show_billed_characters and returns one result' do
|
|
202
|
-
stub_translate({ text: ['Hello'], target_lang: 'DE', show_billed_characters: true,
|
|
203
|
-
formality: 'less', glossary_id: 'g-1', split_sentences: '0' },
|
|
204
|
-
[{ text: 'Hallo', billed_characters: 5 }])
|
|
205
|
-
|
|
206
|
-
result = client.translate_text('Hello', nil, 'DE', formality: 'less', glossary: 'g-1',
|
|
207
|
-
split_sentences: false, context: nil)
|
|
208
|
-
expect([result.class, result.text, result.billed_characters])
|
|
209
|
-
.to eq([DeepL::Resources::Text, 'Hallo', 5])
|
|
210
|
-
end
|
|
211
|
-
|
|
212
|
-
it 'returns an array for a one-element array' do
|
|
213
|
-
stub_translate({ text: ['Hello'], source_lang: 'EN', target_lang: 'DE',
|
|
214
|
-
show_billed_characters: true }, [{ text: 'Hallo' }])
|
|
215
|
-
|
|
216
|
-
expect(client.translate_text(['Hello'], 'EN', 'DE').map(&:text)).to eq(['Hallo'])
|
|
217
|
-
end
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
describe '#rephrase_text' do
|
|
221
|
-
let(:client) { described_class.new(OfflineHelper::AUTH_KEY) }
|
|
222
|
-
|
|
223
|
-
def stub_rephrase(body, improvements)
|
|
224
|
-
stub_request(:post, url('/v2/write/rephrase'))
|
|
225
|
-
.with(body: body, headers: json_headers)
|
|
226
|
-
.to_return(reply(improvements: improvements))
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
it 'sends the writing style and tone keywords, no nil options, and returns one result' do
|
|
230
|
-
stub_rephrase({ text: ['Hello'], target_lang: 'en-GB', writing_style: 'business',
|
|
231
|
-
tone: 'friendly' },
|
|
232
|
-
[{ text: 'Hi', detected_source_language: 'en', target_language: 'en-GB' }])
|
|
233
|
-
|
|
234
|
-
result = client.rephrase_text('Hello', 'en-GB', writing_style: 'business',
|
|
235
|
-
tone: 'friendly', context: nil)
|
|
236
|
-
expect([result.class, result.text, result.target_language])
|
|
237
|
-
.to eq([DeepL::Resources::Text, 'Hi', 'en-GB'])
|
|
238
|
-
end
|
|
239
|
-
|
|
240
|
-
it 'applies extra_body_parameters last, replacing string or symbol keyed fields' do
|
|
241
|
-
stub_rephrase({ text: ['Hello'], tone: 'confident', beta: true }, [{ text: 'Hi' }])
|
|
242
|
-
|
|
243
|
-
client.rephrase_text('Hello', tone: 'friendly',
|
|
244
|
-
extra_body_parameters: { 'tone' => 'confident', beta: true })
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
it 'returns an array for a one-element array' do
|
|
248
|
-
stub_rephrase({ text: ['Hello'] }, [{ text: 'Hi' }])
|
|
249
|
-
|
|
250
|
-
expect(client.rephrase_text(['Hello']).map(&:text)).to eq(['Hi'])
|
|
251
|
-
end
|
|
252
|
-
end
|
|
253
|
-
|
|
254
|
-
describe '#correct_text' do
|
|
255
|
-
let(:client) { described_class.new(OfflineHelper::AUTH_KEY) }
|
|
256
|
-
|
|
257
|
-
it 'sends the text, the target language and the extra body parameters' do
|
|
258
|
-
stub_request(:post, url('/v2/write/correct'))
|
|
259
|
-
.with(body: { text: ['Helo'], target_lang: 'en-US', beta: 1 }, headers: json_headers)
|
|
260
|
-
.to_return(reply(improvements: [{ text: 'Hello', target_language: 'en-US' }]))
|
|
261
|
-
|
|
262
|
-
result = client.correct_text('Helo', 'en-US', extra_body_parameters: { beta: 1 })
|
|
263
|
-
expect([result.text, result.target_language]).to eq(%w[Hello en-US])
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
it 'returns an array for a one-element array' do
|
|
267
|
-
stub_request(:post, url('/v2/write/correct'))
|
|
268
|
-
.to_return(reply(improvements: [{ text: 'Hello' }]))
|
|
269
|
-
|
|
270
|
-
expect(client.correct_text(['Helo']).map(&:text)).to eq(['Hello'])
|
|
271
|
-
end
|
|
272
|
-
end
|
|
273
|
-
end
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
# Copyright 2018 Daniel Herzog
|
|
2
|
-
# Use of this source code is governed by an MIT
|
|
3
|
-
# license that can be found in the LICENSE.md file.
|
|
4
|
-
# frozen_string_literal: true
|
|
5
|
-
|
|
6
|
-
require 'logger'
|
|
7
|
-
require_relative 'offline_helper'
|
|
8
|
-
|
|
9
|
-
describe DeepL::Configuration do
|
|
10
|
-
subject(:config) { described_class.new(attributes) }
|
|
11
|
-
|
|
12
|
-
let(:attributes) { {} }
|
|
13
|
-
|
|
14
|
-
around do |tests|
|
|
15
|
-
tmp_env = ENV.to_hash
|
|
16
|
-
ENV.clear
|
|
17
|
-
tests.call
|
|
18
|
-
ENV.replace(tmp_env)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe '#initialize' do
|
|
22
|
-
context 'when using default configuration attributes' do
|
|
23
|
-
it 'uses default attributes' do
|
|
24
|
-
expect(config.auth_key).to eq(ENV.fetch('DEEPL_AUTH_KEY', nil))
|
|
25
|
-
expect(config.host).to eq('https://api.deepl.com')
|
|
26
|
-
expect(config.max_doc_status_queries).to be_nil
|
|
27
|
-
expect(config.version).to eq('v2')
|
|
28
|
-
expect(config.timeout).to eq(60.0)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it 'sends platform information' do
|
|
32
|
-
expect(config.user_agent).to include('deepl-ruby/')
|
|
33
|
-
expect(config.user_agent).to include('(')
|
|
34
|
-
expect(config.user_agent).to include(')')
|
|
35
|
-
expect(config.user_agent).to include(' ruby/')
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
context 'when using custom configuration attributes' do
|
|
40
|
-
let(:logger) { Logger.new($stdout) }
|
|
41
|
-
let(:attributes) { { auth_key: 'SAMPLE', host: 'https://api-free.deepl.com', logger: logger, max_doc_status_queries: 42, max_network_retries: 43, version: 'v1' } }
|
|
42
|
-
|
|
43
|
-
it 'uses custom attributes' do
|
|
44
|
-
expect(config.auth_key).to eq(attributes[:auth_key])
|
|
45
|
-
expect(config.host).to eq(attributes[:host])
|
|
46
|
-
expect(config.max_doc_status_queries).to eq(attributes[:max_doc_status_queries])
|
|
47
|
-
expect(config.max_network_retries).to eq(attributes[:max_network_retries])
|
|
48
|
-
expect(config.version).to eq(attributes[:version])
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
context 'when the environment provides the key and the server URL' do
|
|
53
|
-
before do
|
|
54
|
-
ENV['DEEPL_AUTH_KEY'] = 'env-key'
|
|
55
|
-
ENV['DEEPL_SERVER_URL'] = 'https://api.example.test'
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
it 'uses the environment values' do
|
|
59
|
-
expect(config.auth_key).to eq('env-key')
|
|
60
|
-
expect(config.host).to eq('https://api.example.test')
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
it 'prefers the given attributes' do
|
|
64
|
-
config = described_class.new(auth_key: 'given', host: 'https://given.example.test')
|
|
65
|
-
expect([config.auth_key, config.host]).to eq(%w[given https://given.example.test])
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
context 'when using a free key' do
|
|
70
|
-
let(:attributes) { { auth_key: '123e4567-e89b-12d3-a456-426614174000:fx' } }
|
|
71
|
-
|
|
72
|
-
it 'uses the free API URL' do
|
|
73
|
-
expect(config.host).to eq('https://api-free.deepl.com')
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
context 'when the free key is set after the configuration was created' do
|
|
78
|
-
it 'uses the free API URL' do
|
|
79
|
-
config.auth_key = 'abc:fx'
|
|
80
|
-
|
|
81
|
-
expect(config.host).to eq('https://api-free.deepl.com')
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
context 'when the free key has surrounding whitespace' do
|
|
86
|
-
let(:attributes) { { auth_key: 'abc:fx ' } }
|
|
87
|
-
|
|
88
|
-
it 'uses the free API URL' do
|
|
89
|
-
expect(config.host).to eq('https://api-free.deepl.com')
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
context 'when using a pro key' do
|
|
94
|
-
let(:attributes) { { auth_key: '123e4567-e89b-12d3-a456-426614174000' } }
|
|
95
|
-
|
|
96
|
-
it 'uses the pro API URL' do
|
|
97
|
-
expect(config.host).to eq('https://api.deepl.com')
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
context 'when opting out of sending platform info' do
|
|
102
|
-
subject(:config) { described_class.new(attributes, nil, nil, false) }
|
|
103
|
-
|
|
104
|
-
it 'does not send platform information' do
|
|
105
|
-
expect(config.user_agent).to include('deepl-ruby/')
|
|
106
|
-
expect(config.user_agent).not_to include('(')
|
|
107
|
-
expect(config.user_agent).not_to include(')')
|
|
108
|
-
expect(config.user_agent).not_to include(' ruby/')
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
context 'when registering an app and sending platform info' do
|
|
113
|
-
subject(:config) { described_class.new(attributes, 'MyTestApp', '0.1.3', true) }
|
|
114
|
-
|
|
115
|
-
it 'sends platform and app info' do
|
|
116
|
-
expect(config.user_agent).to include('deepl-ruby/')
|
|
117
|
-
expect(config.user_agent).to include('(')
|
|
118
|
-
expect(config.user_agent).to include(')')
|
|
119
|
-
expect(config.user_agent).to include(' ruby/')
|
|
120
|
-
expect(config.user_agent).to include('MyTestApp/0.1.3')
|
|
121
|
-
end
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
context 'when registering an app and not sending platform info' do
|
|
125
|
-
subject(:config) { described_class.new(attributes, 'MyTestApp', '0.1.3', false) }
|
|
126
|
-
|
|
127
|
-
it 'sends app, but not platform info' do
|
|
128
|
-
expect(config.user_agent).to include('deepl-ruby/')
|
|
129
|
-
expect(config.user_agent).not_to include('(')
|
|
130
|
-
expect(config.user_agent).not_to include(')')
|
|
131
|
-
expect(config.user_agent).not_to include(' ruby/')
|
|
132
|
-
expect(config.user_agent).to include('MyTestApp/0.1.3')
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
describe '#validate!' do
|
|
138
|
-
let(:auth_message) { 'auth_key not provided' }
|
|
139
|
-
|
|
140
|
-
context 'when the auth key is empty' do
|
|
141
|
-
let(:attributes) { { auth_key: '' } }
|
|
142
|
-
|
|
143
|
-
it 'raises an error' do
|
|
144
|
-
expect { config.validate! }.to raise_error(DeepL::Exceptions::Error, auth_message)
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
context 'when the auth key is blank' do
|
|
149
|
-
let(:attributes) { { auth_key: " \n" } }
|
|
150
|
-
|
|
151
|
-
it 'raises an error' do
|
|
152
|
-
expect { config.validate! }.to raise_error(DeepL::Exceptions::Error, auth_message)
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
context 'when the auth key is set' do
|
|
157
|
-
let(:attributes) { { auth_key: 'not-empty' } }
|
|
158
|
-
|
|
159
|
-
it 'does not raise an error' do
|
|
160
|
-
expect { config.validate! }.not_to raise_error
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
end
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
# Copyright 2025 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.md file.
|
|
4
|
-
# frozen_string_literal: true
|
|
5
|
-
|
|
6
|
-
require_relative 'offline_helper'
|
|
7
|
-
|
|
8
|
-
describe 'DeepL::Constants' do
|
|
9
|
-
describe DeepL::Constants::Tone do
|
|
10
|
-
subject(:tone) { described_class }
|
|
11
|
-
|
|
12
|
-
it 'includes all expected tone values' do # rubocop:disable RSpec/ExampleLength
|
|
13
|
-
expect(tone.options).to contain_exactly(
|
|
14
|
-
'confident',
|
|
15
|
-
'default',
|
|
16
|
-
'diplomatic',
|
|
17
|
-
'enthusiastic',
|
|
18
|
-
'friendly',
|
|
19
|
-
'prefer_confident',
|
|
20
|
-
'prefer_diplomatic',
|
|
21
|
-
'prefer_enthusiastic',
|
|
22
|
-
'prefer_friendly'
|
|
23
|
-
)
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it 'validates correct tone values' do
|
|
27
|
-
expect(tone.valid?('enthusiastic')).to be true
|
|
28
|
-
expect(tone.valid?('friendly')).to be true
|
|
29
|
-
expect(tone.valid?('confident')).to be true
|
|
30
|
-
expect(tone.valid?('diplomatic')).to be true
|
|
31
|
-
expect(tone.valid?('default')).to be true
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it 'invalidates incorrect tone values' do
|
|
35
|
-
expect(tone.valid?('angry')).to be false
|
|
36
|
-
expect(tone.valid?('')).to be false
|
|
37
|
-
expect(tone.valid?(nil)).to be false
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
describe DeepL::Constants::WritingStyle do
|
|
42
|
-
subject(:writing_style) { described_class }
|
|
43
|
-
|
|
44
|
-
it 'includes all expected writing style values' do # rubocop:disable RSpec/ExampleLength
|
|
45
|
-
expect(writing_style.options).to contain_exactly(
|
|
46
|
-
'default',
|
|
47
|
-
'simple',
|
|
48
|
-
'business',
|
|
49
|
-
'academic',
|
|
50
|
-
'casual',
|
|
51
|
-
'prefer_academic',
|
|
52
|
-
'prefer_business',
|
|
53
|
-
'prefer_casual',
|
|
54
|
-
'prefer_simple'
|
|
55
|
-
)
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
it 'validates correct writing style values' do
|
|
59
|
-
expect(writing_style.valid?('simple')).to be true
|
|
60
|
-
expect(writing_style.valid?('business')).to be true
|
|
61
|
-
expect(writing_style.valid?('academic')).to be true
|
|
62
|
-
expect(writing_style.valid?('casual')).to be true
|
|
63
|
-
expect(writing_style.valid?('default')).to be true
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
it 'invalidates incorrect writing style values' do
|
|
67
|
-
expect(writing_style.valid?('wordy')).to be false
|
|
68
|
-
expect(writing_style.valid?('')).to be false
|
|
69
|
-
expect(writing_style.valid?(nil)).to be false
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
describe DeepL::Constants::TagHandling do
|
|
74
|
-
subject(:tag_handling) { described_class }
|
|
75
|
-
|
|
76
|
-
it 'includes all expected tag handling values' do
|
|
77
|
-
expect(tag_handling.options).to contain_exactly('xml', 'html')
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
it 'validates correct tag handling values' do
|
|
81
|
-
expect(tag_handling.valid?('xml')).to be true
|
|
82
|
-
expect(tag_handling.valid?('html')).to be true
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
it 'invalidates incorrect tag handling values' do
|
|
86
|
-
expect(tag_handling.valid?('json')).to be false
|
|
87
|
-
expect(tag_handling.valid?('')).to be false
|
|
88
|
-
expect(tag_handling.valid?(nil)).to be false
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
describe DeepL::Constants::TagHandlingVersion do
|
|
93
|
-
it 'includes v1 and v2' do
|
|
94
|
-
expect([described_class::V1, described_class::V2]).to eq(%w[v1 v2])
|
|
95
|
-
expect(described_class.options).to contain_exactly('v1', 'v2')
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
describe DeepL::Constants::LanguageResourceName do
|
|
100
|
-
it 'includes the resources of the languages endpoint' do
|
|
101
|
-
expect(described_class.options).to contain_exactly(
|
|
102
|
-
'translate_text', 'translate_document', 'glossary', 'voice', 'write', 'style_rules',
|
|
103
|
-
'translation_memory'
|
|
104
|
-
)
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
describe DeepL::Constants::LanguageInclude do
|
|
109
|
-
it 'includes beta and external' do
|
|
110
|
-
expect([described_class::BETA, described_class::EXTERNAL]).to eq(%w[beta external])
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
describe DeepL::Constants::SplitSentences do
|
|
115
|
-
subject(:split_sentences) { described_class }
|
|
116
|
-
|
|
117
|
-
it 'includes all expected split sentences values' do
|
|
118
|
-
expect(split_sentences.options).to contain_exactly('0', '1', 'nonewlines')
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
it 'validates correct split sentences values' do
|
|
122
|
-
expect(split_sentences.valid?('0')).to be true
|
|
123
|
-
expect(split_sentences.valid?('1')).to be true
|
|
124
|
-
expect(split_sentences.valid?('nonewlines')).to be true
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
it 'invalidates incorrect split sentences values' do
|
|
128
|
-
expect(split_sentences.valid?('2')).to be false
|
|
129
|
-
expect(split_sentences.valid?('')).to be false
|
|
130
|
-
expect(split_sentences.valid?(nil)).to be false
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
describe DeepL::Constants::ModelType do
|
|
135
|
-
subject(:model_type) { described_class }
|
|
136
|
-
|
|
137
|
-
it 'includes all expected model type values' do
|
|
138
|
-
expect(model_type.options).to contain_exactly(
|
|
139
|
-
'quality_optimized',
|
|
140
|
-
'prefer_quality_optimized',
|
|
141
|
-
'latency_optimized'
|
|
142
|
-
)
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
it 'validates correct model type values' do
|
|
146
|
-
expect(model_type.valid?('quality_optimized')).to be true
|
|
147
|
-
expect(model_type.valid?('prefer_quality_optimized')).to be true
|
|
148
|
-
expect(model_type.valid?('latency_optimized')).to be true
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
it 'invalidates incorrect model type values' do
|
|
152
|
-
expect(model_type.valid?('speed_optimized')).to be false
|
|
153
|
-
expect(model_type.valid?('')).to be false
|
|
154
|
-
expect(model_type.valid?(nil)).to be false
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
describe DeepL::Constants::Formality do
|
|
159
|
-
subject(:formality) { described_class }
|
|
160
|
-
|
|
161
|
-
it 'includes all expected formality values' do
|
|
162
|
-
expect(formality.options).to contain_exactly('default', 'more', 'less', 'prefer_more',
|
|
163
|
-
'prefer_less')
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
it 'validates correct formality values' do
|
|
167
|
-
expect(formality.valid?('default')).to be true
|
|
168
|
-
expect(formality.valid?('more')).to be true
|
|
169
|
-
expect(formality.valid?('less')).to be true
|
|
170
|
-
expect(formality.valid?('prefer_more')).to be true
|
|
171
|
-
expect(formality.valid?('prefer_less')).to be true
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
it 'invalidates incorrect formality values' do
|
|
175
|
-
expect(formality.valid?('neutral')).to be false
|
|
176
|
-
expect(formality.valid?('')).to be false
|
|
177
|
-
expect(formality.valid?(nil)).to be false
|
|
178
|
-
end
|
|
179
|
-
end
|
|
180
|
-
end
|
|
@@ -1,50 +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 'DeepL::Client#correct_text' do
|
|
9
|
-
let(:client) { DeepL::Client.new(OfflineHelper::AUTH_KEY) }
|
|
10
|
-
|
|
11
|
-
def stub_correct(body = nil)
|
|
12
|
-
request = stub_request(:post, url('/v2/write/correct'))
|
|
13
|
-
request = request.with(body: body, headers: json_headers) unless body.nil?
|
|
14
|
-
request
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it 'sends only the text when no target language is given' do
|
|
18
|
-
stub_correct(text: ['Helo']).to_return(reply(improvements: [{ text: 'Hello' }]))
|
|
19
|
-
client.correct_text('Helo')
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'sends several texts' do
|
|
23
|
-
stub_correct(text: %w[a b], target_lang: 'en-US')
|
|
24
|
-
.to_return(reply(improvements: [{ text: 'a' }, { text: 'b' }]))
|
|
25
|
-
|
|
26
|
-
client.correct_text(%w[a b], 'en-US')
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
it 'returns a text for a string' do
|
|
30
|
-
stub_correct.to_return(reply(improvements: [{ text: 'Hello' }]))
|
|
31
|
-
|
|
32
|
-
expect(client.correct_text('Helo')).to be_a(DeepL::Resources::Text)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
[['Helo', []], [%w[a b], [{ text: 'x' }]]].each do |text, improvements|
|
|
36
|
-
it "raises DeepL::Exceptions::Error for #{improvements.size} improvements of #{text.inspect}" do
|
|
37
|
-
stub_correct.to_return(reply(improvements: improvements))
|
|
38
|
-
|
|
39
|
-
expect { client.correct_text(text) }
|
|
40
|
-
.to raise_error(an_instance_of(DeepL::Exceptions::Error), /expected \d improvements/)
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
[[''], [[]], [['a', '']]].each do |args|
|
|
45
|
-
it "raises ArgumentError for #{args.first.inspect} without sending a request" do
|
|
46
|
-
expect { client.correct_text(*args) }.to raise_error(ArgumentError, /non-empty/)
|
|
47
|
-
expect(a_request(:any, /.*/)).not_to have_been_made
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|