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
data/lib/deepl/client.rb CHANGED
@@ -6,8 +6,7 @@
6
6
  module DeepL
7
7
  ##
8
8
  # Client for the DeepL API. Every client owns its configuration and HTTP connection: two
9
- # clients never share options, and nothing is read from environment variables or from the
10
- # deprecated module-level configuration (+DeepL.configure+).
9
+ # clients never share options, and nothing is read from environment variables.
11
10
  #
12
11
  # client = DeepL::Client.new(ENV.fetch('DEEPL_AUTH_KEY'), app_info: { app_name: 'my-app',
13
12
  # app_version: '1.2.3' })
@@ -28,27 +27,41 @@ module DeepL
28
27
  end
29
28
 
30
29
  ##
30
+ # A keyword argument given as +nil+ takes its default, like an omitted one.
31
31
  # @param [String] auth_key Authentication key, surrounding whitespace is removed.
32
- # @param [String, nil] server_url Base URL of the DeepL API, a path prefix is kept. Defaults
33
- # to +https://api-free.deepl.com+ for Free keys (ending in +:fx+), else
32
+ # @param [String, nil] server_url Base URL of the DeepL API, an absolute +http+ or +https+
33
+ # URL with a host; a port and a path prefix are kept, trailing slashes are removed.
34
+ # Defaults to +https://api-free.deepl.com+ for Free keys (ending in +:fx+), else
34
35
  # +https://api.deepl.com+.
35
36
  # @param [Hash] headers HTTP headers sent with every DeepL API request (pre-signed storage
36
37
  # URLs get only a +User-Agent+). A header of the same name in any casing replaces the
37
38
  # library's, +Authorization+ and +User-Agent+ included, except the technical headers
38
39
  # +Content-Type+, +Content-Length+, +Transfer-Encoding+ and +Host+.
39
- # @param [Integer] max_retries Number of retries after the first attempt, for HTTP 429, any
40
- # 5xx and transient network failures (not TLS failures or malformed responses).
41
- # @param [Numeric] timeout Timeout of each attempt in seconds, applied to opening the
40
+ # @param [Integer, nil] max_retries Number of retries after the first attempt, for HTTP 429, any
41
+ # 5xx and transient network failures (not TLS failures or malformed responses). Requests that
42
+ # create a resource (glossaries, spoken terms, style rule lists and custom instructions, style
43
+ # profiles, document uploads, translation memory import and export jobs) are retried only
44
+ # after HTTP 429 and transient network failures while connecting, such as DNS resolution
45
+ # errors, refused connections, unreachable hosts or networks, connection timeouts and resets
46
+ # during the TLS or proxy handshake, when the request was not processed.
47
+ # @param [Numeric, nil] timeout Timeout of each attempt in seconds, applied to opening the
42
48
  # connection, reading and writing. A longer backoff interval takes precedence.
43
- # @param [String, nil] proxy Proxy URL, e.g. +http://user:password@proxy.example:8080+.
44
- # @param [Boolean] send_platform_info Whether to add the OS and Ruby versions to the
49
+ # @param [String, URI, Hash, nil] proxy Proxy URL (+http+ or +https+ scheme) as a String or
50
+ # +URI+, e.g. +http://user:password@proxy.example:8080+, or a hash with +proxy_addr+ and
51
+ # +proxy_port+ (string or symbol keys, both required), which is a plain HTTP proxy. An
52
+ # +https+ proxy is connected to over TLS and needs net-http 0.5.0 or later (bundled from
53
+ # Ruby 3.4) and an +https+ server URL.
54
+ # @param [Boolean, nil] send_platform_info Whether to add the OS and Ruby versions to the
45
55
  # +User-Agent+.
46
56
  # @param [DeepL::AppInfo, Hash, nil] app_info Your application's +app_name+ and
47
57
  # +app_version+, added to the +User-Agent+.
48
58
  # @param [DeepL::HTTPClientOptions, nil] http_client_options Proxy, certificate, SSL
49
59
  # verification and per-phase timeouts. Its timeouts take precedence over +timeout+.
50
60
  # @param [Logger, nil] logger Logger for requests and retries.
51
- # @raise [ArgumentError] If +auth_key+ is empty.
61
+ # @raise [ArgumentError] If +auth_key+ is empty, +server_url+ is not an absolute +http+ or
62
+ # +https+ URL with a host, or +proxy+ is not a valid proxy URL, +URI+ or proxy hash (an empty
63
+ # host or blank +proxy_addr+ is not valid), has a proxy URL scheme other than +http+ or
64
+ # +https+, or is an +https+ proxy that cannot be used (see +proxy+).
52
65
  def initialize(auth_key, server_url: nil, headers: {}, max_retries: 5, timeout: 10, # rubocop:disable Metrics/ParameterLists
53
66
  proxy: nil, send_platform_info: true, app_info: nil, http_client_options: nil,
54
67
  logger: nil)
@@ -70,30 +83,43 @@ module DeepL
70
83
  # @param [String, Array<String>] text Text or texts to translate.
71
84
  # @param [String, nil] source_lang Source language, +nil+ to detect it.
72
85
  # @param [String] target_lang Target language.
73
- # @param [Hash] options Translation options, sent as given unless listed here:
86
+ # @param [Hash] options Translation options. Any other option raises ArgumentError before a
87
+ # request is sent:
74
88
  # * +:glossary+ - A glossary ID, `DeepL::Resources::Glossary` or
75
89
  # `DeepL::Resources::MultilingualGlossary`, sent as +glossary_id+.
76
90
  # * +:glossary_ids+ - Up to 5 glossaries (IDs or objects). Cannot be combined with
77
91
  # +:glossary+. Without +source_lang+, the API detects it.
78
92
  # * +:style_rule+, +:style_profile+, +:translation_memory+ - An ID or the resource object.
93
+ # +nil+ leaves it unset.
79
94
  # * +:split_sentences+ - +true+/+false+ are sent as +'1'+/+'0'+.
80
95
  # * +:custom_instructions+ - An array of instructions; a string is one instruction.
81
96
  # * +:extra_body_parameters+ - A hash added to the request body last; replaces fields of
82
97
  # the same name.
83
- # Other options (+:formality+, +:tag_handling+, +:tag_handling_version+, +:context+,
84
- # +:model_type+, ...) are sent unchanged.
98
+ # * +:formality+, +:context+, +:tag_handling+, +:tag_handling_version+, +:model_type+,
99
+ # +:preserve_formatting+, +:outline_detection+, +:splitting_tags+, +:non_splitting_tags+,
100
+ # +:ignore_tags+ - Sent unchanged.
101
+ # * +:translation_memory_threshold+ - Integer 0-100. Requires +:translation_memory+.
85
102
  # @return [DeepL::Resources::Text, Array<DeepL::Resources::Text>] One result for a string,
86
103
  # an array for an array (also for a one-element array).
87
- # @raise [ArgumentError] If the glossary options are combined or more than 5 are given.
104
+ # @raise [ArgumentError] If +text+ is empty or contains an empty string, +target_lang+ or a
105
+ # given +source_lang+ is empty, an option is unknown, the glossary options are combined or
106
+ # more than 5 are given, +:glossary+, +:style_rule+, +:style_profile+, +:translation_memory+
107
+ # or an entry of +:glossary_ids+ is an empty ID or a resource object without its ID, or
108
+ # +:translation_memory_threshold+ is set without +:translation_memory+ or is not from 0 to
109
+ # 100; nothing is sent.
88
110
  def translate_text(text, source_lang, target_lang, **options)
89
- result = Requests::Translate.new(api, text, source_lang, target_lang, options).request
90
- text.is_a?(Array) && !result.is_a?(Array) ? [result] : result
111
+ Requests::Translate.new(api, text, source_lang, target_lang, options).request
91
112
  end
92
113
 
93
114
  ##
94
115
  # Translates the document at +input_path+ and writes the result to +output_path+: uploads
95
116
  # it, waits until the translation is done and downloads it.
96
117
  #
118
+ # The upload is retried only after failures that show the request was not processed (HTTP 429
119
+ # and transient network failures while connecting, such as DNS resolution errors, refused
120
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
121
+ # proxy handshake), so that a retry cannot create a second document.
122
+ #
97
123
  # @param [String] input_path Path of the document to translate.
98
124
  # @param [String] output_path Path to write the translated document to. Must not exist.
99
125
  # @param [String, nil] source_lang Source language, +nil+ to detect it.
@@ -103,18 +129,18 @@ module DeepL
103
129
  # @param [Hash] options The options of #translate_document_upload, including +:filename+.
104
130
  # @return [DeepL::Resources::DocumentTranslationStatus] The final status, +done+.
105
131
  # @raise [ArgumentError] If +input_path+ does not exist; nothing is uploaded.
106
- # @raise [DeepL::Exceptions::DocumentTranslationError] For any other failure, also an
107
- # existing +output_path+ (nothing is uploaded and the file is kept), a failed translation
108
- # or an expired +timeout_s+. Its +document_handle+ is set once the upload succeeded, so the
132
+ # @raise [DeepL::Exceptions::DocumentTranslationError] For any other failure: an existing
133
+ # +output_path+ (the file is kept) or one whose directory is missing or not writable, both
134
+ # checked before the upload; an argument error of #translate_document_upload, such as an
135
+ # unknown option, raised before anything is sent; a failed translation or an expired
136
+ # +timeout_s+. Its +document_handle+ is set once the upload succeeded, so the
109
137
  # document can still be queried and downloaded. +http_status_code+ and +should_retry?+ are
110
138
  # those of the DeepL error that caused it, if any.
111
139
  def translate_document(input_path, output_path, source_lang, target_lang, timeout_s: nil, # rubocop:disable Metrics/MethodLength
112
140
  **options)
113
141
  raise ArgumentError, "Input file does not exist: #{input_path}" unless File.exist?(input_path)
114
- if File.exist?(output_path)
115
- raise Exceptions::DocumentTranslationError.new('File already exists at output path', nil)
116
- end
117
142
 
143
+ check_document_output_path(output_path)
118
144
  handle = nil
119
145
  begin
120
146
  handle = translate_document_upload(input_path, source_lang, target_lang, **options)
@@ -132,27 +158,41 @@ module DeepL
132
158
  ##
133
159
  # Uploads the document at +input_path+ for translation. Options set to +nil+ are not sent.
134
160
  #
161
+ # The upload is retried only after failures that show the request was not processed (HTTP 429
162
+ # and transient network failures while connecting, such as DNS resolution errors, refused
163
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
164
+ # proxy handshake), so that a retry cannot create a second document.
165
+ #
135
166
  # @param [String] input_path Path of the document to translate.
136
167
  # @param [String, nil] source_lang Source language, +nil+ to detect it.
137
168
  # @param [String] target_lang Target language.
138
- # @param [Hash] options Document translation options:
169
+ # @param [Hash] options Document translation options. Any other option raises ArgumentError
170
+ # before a request is sent:
139
171
  # * +:filename+ - File name sent with the upload, including its extension. Defaults to the
140
172
  # base name of +input_path+.
141
173
  # * +:formality+, +:output_format+ - Sent unchanged.
142
174
  # * +:enable_watermark+ - +true+ or +false+.
143
175
  # * +:glossary+ - A glossary ID, `DeepL::Resources::Glossary` or
144
- # `DeepL::Resources::MultilingualGlossary`, sent as +glossary_id+.
176
+ # `DeepL::Resources::MultilingualGlossary`, sent as +glossary_id+. Requires +source_lang+.
145
177
  # * +:glossary_ids+ - Up to 5 glossaries (IDs or objects). Requires +source_lang+ and
146
178
  # cannot be combined with +:glossary+.
147
179
  # * +:style_rule+, +:style_profile+, +:translation_memory+ - An ID or the resource object.
148
- # * +:translation_memory_threshold+ - Integer 0-100.
180
+ # +nil+ leaves it unset.
181
+ # * +:translation_memory_threshold+ - Integer 0-100. Requires +:translation_memory+.
149
182
  # * +:extra_body_parameters+ - A hash added to the form last; replaces fields of the same
150
- # name.
183
+ # name. A +nil+ value removes the field, the library's own field of that name included.
151
184
  # @return [DeepL::Resources::DocumentHandle] Handle of the uploaded document.
152
- # @raise [ArgumentError] If +input_path+ does not exist (nothing is uploaded), or the glossary
153
- # options are combined or more than 5 are given.
185
+ # @raise [ArgumentError] If +input_path+ does not exist, +target_lang+ or a given +source_lang+
186
+ # is empty, an option is unknown, +:glossary+ or +:glossary_ids+ is set without +source_lang+,
187
+ # the glossary options are combined or more than 5 are given, +:glossary+, +:style_rule+,
188
+ # +:style_profile+, +:translation_memory+ or an entry of +:glossary_ids+ is an empty ID or a
189
+ # resource object without its ID, or +:translation_memory_threshold+ is set without
190
+ # +:translation_memory+ or is not from 0 to 100; nothing is uploaded.
191
+ # @raise [SystemCallError] If +input_path+ cannot be read, e.g. +Errno::EISDIR+ for a directory
192
+ # or +Errno::EACCES+; nothing is uploaded.
154
193
  def translate_document_upload(input_path, source_lang, target_lang, **options)
155
194
  raise ArgumentError, "Input file does not exist: #{input_path}" unless File.exist?(input_path)
195
+ raise Errno::EISDIR, input_path if File.directory?(input_path)
156
196
 
157
197
  filename = options.delete(:filename)
158
198
  Requests::Document::Upload.new(api, input_path, source_lang, target_lang, filename,
@@ -182,13 +222,17 @@ module DeepL
182
222
  end
183
223
 
184
224
  ##
185
- # Downloads the translated document to +output_path+, overwriting an existing file.
225
+ # Downloads the translated document to +output_path+.
186
226
  #
187
227
  # @param [DeepL::Resources::DocumentHandle] handle Handle returned by the upload.
188
- # @param [String] output_path Path to write the translated document to.
228
+ # @param [String] output_path Path to write the translated document to. Must not exist.
189
229
  # @return [nil]
230
+ # @raise [SystemCallError] Before the request: +Errno::EEXIST+ if +output_path+ exists,
231
+ # +Errno::ENOENT+ if its directory does not exist, +Errno::EACCES+ if the directory is not
232
+ # writable.
190
233
  # @raise [DeepL::Exceptions::DocumentNotReadyError] If the translation is not done yet.
191
234
  def translate_document_download(handle, output_path)
235
+ Utils::OutputPath.check!(output_path)
192
236
  Requests::Document::Download.new(api, handle.document_id, handle.document_key,
193
237
  output_path).request
194
238
  nil
@@ -204,14 +248,14 @@ module DeepL
204
248
  # Cannot be combined with +tone+.
205
249
  # @param [String, nil] tone Tone, see +DeepL::Constants::Tone+.
206
250
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last;
207
- # replaces fields of the same name. Other options are sent unchanged.
251
+ # replaces fields of the same name. Any other option raises ArgumentError.
208
252
  # @return [DeepL::Resources::Text, Array<DeepL::Resources::Text>] One result for a string,
209
253
  # an array for an array (also for a one-element array).
254
+ # @raise [ArgumentError] If +text+ is empty or contains an empty string, +target_lang+ is an
255
+ # empty string, or an option is unknown; nothing is sent.
210
256
  # @raise [DeepL::Exceptions::Error] If the response has fewer improvements than texts.
211
257
  def rephrase_text(text, target_lang = nil, writing_style: nil, tone: nil, **options)
212
- result = Requests::Rephrase.new(api, text, target_lang, writing_style, tone,
213
- options.compact).request
214
- text.is_a?(Array) && !result.is_a?(Array) ? [result] : result
258
+ Requests::Rephrase.new(api, text, target_lang, writing_style, tone, options).request
215
259
  end
216
260
 
217
261
  ##
@@ -221,13 +265,14 @@ module DeepL
221
265
  # @param [String, Array<String>] text Text or texts to correct.
222
266
  # @param [String, nil] target_lang Target language, +nil+ to keep the language of the text.
223
267
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last;
224
- # replaces fields of the same name. Other options are sent unchanged.
268
+ # replaces fields of the same name. Any other option raises ArgumentError.
225
269
  # @return [DeepL::Resources::Text, Array<DeepL::Resources::Text>] One result for a string,
226
270
  # an array for an array (also for a one-element array).
227
- # @raise [ArgumentError] If +text+ is empty or contains an empty string.
271
+ # @raise [ArgumentError] If +text+ is empty or contains an empty string, or +target_lang+ is
272
+ # an empty string; nothing is sent.
228
273
  # @raise [DeepL::Exceptions::Error] If the response has fewer improvements than texts.
229
274
  def correct_text(text, target_lang = nil, **options)
230
- Requests::Correct.new(api, text, target_lang, options.compact).request
275
+ Requests::Correct.new(api, text, target_lang, options).request
231
276
  end
232
277
 
233
278
  ##
@@ -236,7 +281,7 @@ module DeepL
236
281
  # @return [Array<DeepL::Resources::Language>] The source languages;
237
282
  # +supports_formality?+ is +nil+ for each.
238
283
  def get_source_languages # rubocop:disable Naming/AccessorMethodName
239
- Requests::Languages.new(api, type: :source).request
284
+ Requests::Languages.new(api, :source).request
240
285
  end
241
286
 
242
287
  ##
@@ -245,7 +290,7 @@ module DeepL
245
290
  # @return [Array<DeepL::Resources::Language>] The target languages, each with
246
291
  # +supports_formality?+ set to +true+ or +false+.
247
292
  def get_target_languages # rubocop:disable Naming/AccessorMethodName
248
- Requests::Languages.new(api, type: :target).request
293
+ Requests::Languages.new(api, :target).request
249
294
  end
250
295
 
251
296
  ##
@@ -281,31 +326,147 @@ module DeepL
281
326
  Requests::Usage.new(api).request
282
327
  end
283
328
 
329
+ ##
330
+ # Creates a v2 glossary for one language pair. Glossaries are not regional, so a regional
331
+ # variant is sent as its base language (+EN-GB+ as +EN+).
332
+ #
333
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
334
+ # transient network failures while connecting, such as DNS resolution errors, refused
335
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
336
+ # proxy handshake), so that a retry cannot create a second glossary.
337
+ #
338
+ # @param [String] name Name of the glossary.
339
+ # @param [String] source_lang Source language.
340
+ # @param [String] target_lang Target language.
341
+ # @param [Hash, Array<Array(String, String)>, String] entries Source term to target term, as a
342
+ # hash, source/target pairs or a TSV string. Sent as TSV.
343
+ # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
344
+ # @return [DeepL::Resources::Glossary] The created glossary.
345
+ # @raise [ArgumentError] If +name+ or +entries+ is empty.
346
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If a term is empty after
347
+ # stripping surrounding whitespace or contains a control character, a TSV line is not exactly
348
+ # two terms, or a source term appears twice.
349
+ def create_glossary(name, source_lang, target_lang, entries, **options)
350
+ raise ArgumentError, 'Glossary entries must not be empty' if entries.nil? || entries.empty?
351
+
352
+ tsv = Utils::GlossaryEntries.to_tsv(entries)
353
+ Requests::Glossary::Create.new(api, name, source_lang, target_lang, tsv, 'tsv',
354
+ options).request
355
+ end
356
+
357
+ ##
358
+ # Creates a v2 glossary from CSV content, for example a file downloaded from the DeepL
359
+ # website.
360
+ #
361
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
362
+ # transient network failures while connecting, such as DNS resolution errors, refused
363
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
364
+ # proxy handshake), so that a retry cannot create a second glossary.
365
+ #
366
+ # @param [String] name Name of the glossary.
367
+ # @param [String] source_lang Source language.
368
+ # @param [String] target_lang Target language.
369
+ # @param [String, IO] csv CSV content, or an IO to read it from. Sent as given.
370
+ # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
371
+ # @return [DeepL::Resources::Glossary] The created glossary.
372
+ # @raise [ArgumentError] If +name+ is empty.
373
+ def create_glossary_from_csv(name, source_lang, target_lang, csv, **options)
374
+ csv = csv.read if csv.respond_to?(:read)
375
+
376
+ Requests::Glossary::Create.new(api, name, source_lang, target_lang, csv, 'csv',
377
+ options).request
378
+ end
379
+
380
+ ##
381
+ # Lists all v2 glossaries.
382
+ #
383
+ # @return [Array<DeepL::Resources::Glossary>]
384
+ def list_glossaries
385
+ Requests::Glossary::List.new(api).request
386
+ end
387
+
388
+ ##
389
+ # Retrieves a v2 glossary, without its entries.
390
+ #
391
+ # @param [String, DeepL::Resources::Glossary] glossary ID or glossary.
392
+ # @return [DeepL::Resources::Glossary]
393
+ # @raise [DeepL::Exceptions::NotFound] If the glossary does not exist.
394
+ def get_glossary(glossary)
395
+ Requests::Glossary::Find.new(api, glossary).request
396
+ end
397
+
398
+ ##
399
+ # Retrieves the entries of a v2 glossary.
400
+ #
401
+ # @param [String, DeepL::Resources::Glossary] glossary ID or glossary.
402
+ # @return [Hash{String => String}] Source term to target term.
403
+ # @raise [DeepL::Exceptions::NotFound] If the glossary does not exist.
404
+ def get_glossary_entries(glossary)
405
+ Requests::Glossary::Entries.new(api, glossary).request
406
+ end
407
+
408
+ ##
409
+ # Deletes a v2 glossary.
410
+ #
411
+ # @param [String, DeepL::Resources::Glossary] glossary ID or glossary.
412
+ # @return [nil]
413
+ # @raise [DeepL::Exceptions::NotFound] If the glossary does not exist.
414
+ def delete_glossary(glossary)
415
+ Requests::Glossary::Destroy.new(api, glossary).request
416
+ end
417
+
418
+ ##
419
+ # Retrieves the language pairs supported by glossaries: every source language combined with
420
+ # every other target language of +/v3/languages?resource=glossary+.
421
+ #
422
+ # @return [Array<DeepL::Resources::LanguagePair>]
423
+ def get_glossary_languages # rubocop:disable Naming/AccessorMethodName
424
+ Requests::Glossary::LanguagePairs.new(api).request
425
+ end
426
+
284
427
  ##
285
428
  # Creates a multilingual glossary with one dictionary per language pair.
286
429
  #
430
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
431
+ # transient network failures while connecting, such as DNS resolution errors, refused
432
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
433
+ # proxy handshake), so that a retry cannot create a second glossary.
434
+ #
287
435
  # @param [String] name Name of the glossary.
288
436
  # @param [Array<DeepL::Resources::MultilingualGlossaryDictionaryEntries, Hash>] dictionaries
289
437
  # One entry per language pair. A hash needs the keys +:source_lang+, +:target_lang+ and
290
438
  # +:entries+ (a hash, source/target pairs or a TSV string; +:entries_format+ +'csv'+ for a
291
- # CSV string, which is sent as given). Entries other than a string are always sent as TSV.
439
+ # CSV string, which must not be empty or whitespace only and is otherwise sent as given).
440
+ # Entries other than a string are always sent as TSV.
292
441
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
293
442
  # @return [DeepL::Resources::MultilingualGlossary] The created glossary.
294
- # @raise [DeepL::Exceptions::ValidationError] If a term is empty after stripping surrounding
295
- # whitespace or contains a control character, or a TSV line is not exactly two terms.
443
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If a term is empty after
444
+ # stripping surrounding whitespace or contains a control character, or a TSV line is not
445
+ # exactly two terms.
446
+ # @raise [ArgumentError] If +name+ or +dictionaries+ is empty, a dictionary has no entries
447
+ # (including empty or whitespace-only CSV entries), or a +source_lang+ or +target_lang+ of a
448
+ # dictionary is nil or empty, before any request.
296
449
  def create_multilingual_glossary(name, dictionaries, **options)
450
+ check_csv_entries!(dictionaries)
297
451
  Requests::MultilingualGlossary::Create.new(api, name, dictionaries, options).request
298
452
  end
299
453
 
300
454
  ##
301
455
  # Creates a multilingual glossary with one dictionary from CSV content.
302
456
  #
457
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
458
+ # transient network failures while connecting, such as DNS resolution errors, refused
459
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
460
+ # proxy handshake), so that a retry cannot create a second glossary.
461
+ #
303
462
  # @param [String] name Name of the glossary.
304
463
  # @param [String] source_lang Source language of the dictionary.
305
464
  # @param [String] target_lang Target language of the dictionary.
306
465
  # @param [String, IO] csv CSV content, or an IO to read it from. Sent as given.
307
466
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
308
467
  # @return [DeepL::Resources::MultilingualGlossary] The created glossary.
468
+ # @raise [ArgumentError] If +name+ is empty, or +source_lang+ or +target_lang+ is nil or empty,
469
+ # before any request.
309
470
  def create_multilingual_glossary_from_csv(name, source_lang, target_lang, csv, **options)
310
471
  dictionary = csv_dictionary(source_lang, target_lang, csv)
311
472
  Requests::MultilingualGlossary::Create.new(api, name, [dictionary], options).request
@@ -325,6 +486,8 @@ module DeepL
325
486
  # @param [String, DeepL::Resources::MultilingualGlossary] glossary ID or glossary.
326
487
  # @return [DeepL::Resources::MultilingualGlossary]
327
488
  # @raise [DeepL::Exceptions::NotFound] If the glossary does not exist.
489
+ # @raise [ArgumentError] If the glossary ID is empty or the glossary has no ID, before any
490
+ # request.
328
491
  def get_multilingual_glossary(glossary)
329
492
  Requests::MultilingualGlossary::Find.new(api, glossary).request
330
493
  end
@@ -340,9 +503,13 @@ module DeepL
340
503
  # pair adds a dictionary.
341
504
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
342
505
  # @return [DeepL::Resources::MultilingualGlossary] The updated glossary.
343
- # @raise [DeepL::Exceptions::ValidationError] If a term is empty after stripping or contains a
344
- # control character.
506
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If a term is empty after
507
+ # stripping or contains a control character.
508
+ # @raise [ArgumentError] If the glossary ID or the entries of a dictionary are empty (CSV
509
+ # entries also if whitespace only), a +source_lang+ or +target_lang+ of a dictionary is nil or
510
+ # empty, or the glossary has no ID, before any request.
345
511
  def update_multilingual_glossary(glossary, name: nil, dictionaries: nil, **options)
512
+ check_csv_entries!(dictionaries) unless dictionaries.nil?
346
513
  Requests::MultilingualGlossary::Update.new(api, glossary, name, dictionaries,
347
514
  options).request
348
515
  end
@@ -354,7 +521,11 @@ module DeepL
354
521
  # @param [String] name New name.
355
522
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
356
523
  # @return [DeepL::Resources::MultilingualGlossary] The updated glossary.
524
+ # @raise [ArgumentError] If +name+ or the glossary ID is empty, or the glossary has no ID,
525
+ # before any request.
357
526
  def update_multilingual_glossary_name(glossary, name, **options)
527
+ raise ArgumentError, 'Glossary name must not be empty' if name.to_s.empty?
528
+
358
529
  Requests::MultilingualGlossary::Update.new(api, glossary, name, nil, options).request
359
530
  end
360
531
 
@@ -367,9 +538,13 @@ module DeepL
367
538
  # language pair and its entries, see #create_multilingual_glossary.
368
539
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
369
540
  # @return [DeepL::Resources::MultilingualGlossary] The updated glossary.
370
- # @raise [DeepL::Exceptions::ValidationError] If a term is empty after stripping or contains a
371
- # control character.
541
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If a term is empty after
542
+ # stripping or contains a control character.
543
+ # @raise [ArgumentError] If the glossary ID or the entries are empty (CSV entries also if
544
+ # whitespace only), a +source_lang+ or +target_lang+ of a dictionary is nil or empty, or the
545
+ # glossary has no ID, before any request.
372
546
  def update_multilingual_glossary_dictionary(glossary, dictionary, **options)
547
+ check_csv_entries!([dictionary])
373
548
  Requests::MultilingualGlossary::Update.new(api, glossary, nil, [dictionary],
374
549
  options).request
375
550
  end
@@ -381,12 +556,16 @@ module DeepL
381
556
  # @param [String, DeepL::Resources::MultilingualGlossary] glossary ID or glossary.
382
557
  # @param [String] source_lang Source language of the dictionary.
383
558
  # @param [String] target_lang Target language of the dictionary.
384
- # @param [String, IO] csv CSV content, or an IO to read it from. Sent as given.
559
+ # @param [String, IO] csv CSV content, or an IO to read it from. Must not be empty or
560
+ # whitespace only; otherwise sent as given.
385
561
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
386
562
  # @return [DeepL::Resources::MultilingualGlossary] The updated glossary.
563
+ # @raise [ArgumentError] If the CSV content is empty or whitespace only, +source_lang+ or
564
+ # +target_lang+ is nil or empty, the glossary ID is empty or the glossary has no ID, before
565
+ # any request.
387
566
  def update_multilingual_glossary_dictionary_from_csv(glossary, source_lang, target_lang, csv,
388
567
  **options)
389
- dictionary = csv_dictionary(source_lang, target_lang, csv)
568
+ dictionary = non_empty_csv_dictionary(source_lang, target_lang, csv)
390
569
  Requests::MultilingualGlossary::Update.new(api, glossary, nil, [dictionary],
391
570
  options).request
392
571
  end
@@ -400,9 +579,13 @@ module DeepL
400
579
  # language pair and its entries, see #create_multilingual_glossary.
401
580
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
402
581
  # @return [DeepL::Resources::MultilingualGlossaryDictionary] The replaced dictionary.
403
- # @raise [DeepL::Exceptions::ValidationError] If a term is empty after stripping or contains a
404
- # control character.
582
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If a term is empty after
583
+ # stripping or contains a control character.
584
+ # @raise [ArgumentError] If the glossary ID or the entries are empty (CSV entries also if
585
+ # whitespace only), a +source_lang+ or +target_lang+ of a dictionary is nil or empty, or the
586
+ # glossary has no ID, before any request.
405
587
  def replace_multilingual_glossary_dictionary(glossary, dictionary, **options)
588
+ check_csv_entries!([dictionary])
406
589
  Requests::MultilingualGlossary::ReplaceDictionary.new(api, glossary, dictionary,
407
590
  options).request
408
591
  end
@@ -413,12 +596,16 @@ module DeepL
413
596
  # @param [String, DeepL::Resources::MultilingualGlossary] glossary ID or glossary.
414
597
  # @param [String] source_lang Source language of the dictionary.
415
598
  # @param [String] target_lang Target language of the dictionary.
416
- # @param [String, IO] csv CSV content, or an IO to read it from. Sent as given.
599
+ # @param [String, IO] csv CSV content, or an IO to read it from. Must not be empty or
600
+ # whitespace only; otherwise sent as given.
417
601
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
418
602
  # @return [DeepL::Resources::MultilingualGlossaryDictionary] The replaced dictionary.
603
+ # @raise [ArgumentError] If the CSV content is empty or whitespace only, +source_lang+ or
604
+ # +target_lang+ is nil or empty, the glossary ID is empty or the glossary has no ID, before
605
+ # any request.
419
606
  def replace_multilingual_glossary_dictionary_from_csv(glossary, source_lang, target_lang, csv,
420
607
  **options)
421
- dictionary = csv_dictionary(source_lang, target_lang, csv)
608
+ dictionary = non_empty_csv_dictionary(source_lang, target_lang, csv)
422
609
  Requests::MultilingualGlossary::ReplaceDictionary.new(api, glossary, dictionary,
423
610
  options).request
424
611
  end
@@ -432,7 +619,10 @@ module DeepL
432
619
  # @return [DeepL::Resources::MultilingualGlossaryDictionaryEntries] The entries as a hash
433
620
  # of source term to target term.
434
621
  # @raise [DeepL::Exceptions::NotFound] If the glossary does not exist or has no dictionary
435
- # for the language pair.
622
+ # for the language pair (HTTP 404), or if the API answers 200 with no entries for the pair
623
+ # (then without +response+ and with +http_status_code+ +nil+).
624
+ # @raise [ArgumentError] If the glossary ID is empty, the glossary has no ID, or +source_lang+
625
+ # or +target_lang+ is nil or empty, before any request.
436
626
  def get_multilingual_glossary_dictionary_entries(glossary, source_lang, target_lang)
437
627
  Requests::MultilingualGlossary::Entries.new(api, glossary, source_lang, target_lang).request
438
628
  end
@@ -444,6 +634,8 @@ module DeepL
444
634
  # @param [String] source_lang Source language of the dictionary.
445
635
  # @param [String] target_lang Target language of the dictionary.
446
636
  # @return [nil]
637
+ # @raise [ArgumentError] If the glossary ID, +source_lang+ or +target_lang+ is empty, or the
638
+ # glossary has no ID, before any request.
447
639
  def delete_multilingual_glossary_dictionary(glossary, source_lang, target_lang)
448
640
  Requests::MultilingualGlossary::DestroyDictionary.new(api, glossary, source_lang,
449
641
  target_lang).request
@@ -454,6 +646,8 @@ module DeepL
454
646
  #
455
647
  # @param [String, DeepL::Resources::MultilingualGlossary] glossary ID or glossary.
456
648
  # @return [nil]
649
+ # @raise [ArgumentError] If the glossary ID is empty or the glossary has no ID, before any
650
+ # request.
457
651
  def delete_multilingual_glossary(glossary)
458
652
  Requests::MultilingualGlossary::Destroy.new(api, glossary).request
459
653
  end
@@ -461,13 +655,19 @@ module DeepL
461
655
  ##
462
656
  # Creates a spoken terms collection, which improves the transcription of the given terms.
463
657
  #
658
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
659
+ # transient network failures while connecting, such as DNS resolution errors, refused
660
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
661
+ # proxy handshake), so that a retry cannot create a second collection.
662
+ #
464
663
  # @param [String] name Name of the collection.
465
664
  # @param [Array<DeepL::Resources::SpokenTermsEntries, Hash>] term_lists One entry per
466
665
  # language. A hash needs the keys +:lang+ and +:entries+ (an array of terms).
467
666
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
468
667
  # @return [DeepL::Resources::SpokenTerms] The created collection.
469
- # @raise [DeepL::Exceptions::ValidationError] If a term is empty after stripping surrounding
470
- # whitespace or contains a control character.
668
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If a term is empty after
669
+ # stripping surrounding whitespace or contains a control character.
670
+ # @raise [ArgumentError] If the +lang+ of a term list is nil or empty, before any request.
471
671
  def create_spoken_terms(name, term_lists, **options)
472
672
  Requests::SpokenTerms::Create.new(api, name, term_lists, options).request
473
673
  end
@@ -486,6 +686,7 @@ module DeepL
486
686
  # @param [String, DeepL::Resources::SpokenTerms] spoken_terms ID or collection.
487
687
  # @return [DeepL::Resources::SpokenTerms]
488
688
  # @raise [DeepL::Exceptions::NotFound] If the collection does not exist.
689
+ # @raise [ArgumentError] If the ID is empty or the collection has no ID, before any request.
489
690
  def get_spoken_terms(spoken_terms)
490
691
  Requests::SpokenTerms::Find.new(api, spoken_terms).request
491
692
  end
@@ -500,8 +701,10 @@ module DeepL
500
701
  # into the list of each language; a new language adds a list.
501
702
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
502
703
  # @return [DeepL::Resources::SpokenTerms] The updated collection.
503
- # @raise [DeepL::Exceptions::ValidationError] If a term is empty after stripping or contains a
504
- # control character.
704
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If a term is empty after
705
+ # stripping or contains a control character.
706
+ # @raise [ArgumentError] If the ID is empty, the collection has no ID, or the +lang+ of a term
707
+ # list is nil or empty, before any request.
505
708
  def update_spoken_terms(spoken_terms, name: nil, term_lists: nil, **options)
506
709
  Requests::SpokenTerms::Update.new(api, spoken_terms, name, term_lists, options).request
507
710
  end
@@ -511,6 +714,7 @@ module DeepL
511
714
  #
512
715
  # @param [String, DeepL::Resources::SpokenTerms] spoken_terms ID or collection.
513
716
  # @return [nil]
717
+ # @raise [ArgumentError] If the ID is empty or the collection has no ID, before any request.
514
718
  def delete_spoken_terms(spoken_terms)
515
719
  Requests::SpokenTerms::Destroy.new(api, spoken_terms).request
516
720
  end
@@ -521,6 +725,8 @@ module DeepL
521
725
  # @param [String, DeepL::Resources::SpokenTerms] spoken_terms ID or collection.
522
726
  # @param [String] lang Language code, for example +'en'+.
523
727
  # @return [DeepL::Resources::SpokenTermsEntries]
728
+ # @raise [ArgumentError] If the ID is empty, the collection has no ID, or +lang+ is nil or
729
+ # empty, before any request.
524
730
  def get_spoken_terms_entries(spoken_terms, lang)
525
731
  Requests::SpokenTerms::Entries.new(api, spoken_terms, lang).request
526
732
  end
@@ -533,8 +739,10 @@ module DeepL
533
739
  # hash needs the keys +:lang+ and +:entries+.
534
740
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
535
741
  # @return [DeepL::Resources::SpokenTermsList]
536
- # @raise [DeepL::Exceptions::ValidationError] If a term is empty after stripping or contains a
537
- # control character.
742
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If a term is empty after
743
+ # stripping or contains a control character.
744
+ # @raise [ArgumentError] If the ID is empty, the collection has no ID, or the +lang+ of the
745
+ # term list is nil or empty, before any request.
538
746
  def replace_spoken_terms_list(spoken_terms, entries, **options)
539
747
  Requests::SpokenTerms::ReplaceTermList.new(api, spoken_terms, entries, options).request
540
748
  end
@@ -545,6 +753,8 @@ module DeepL
545
753
  # @param [String, DeepL::Resources::SpokenTerms] spoken_terms ID or collection.
546
754
  # @param [String] lang Language code, for example +'en'+.
547
755
  # @return [nil]
756
+ # @raise [ArgumentError] If the ID is empty, the collection has no ID, or +lang+ is nil or
757
+ # empty, before any request.
548
758
  def delete_spoken_terms_list(spoken_terms, lang)
549
759
  Requests::SpokenTerms::DestroyTermList.new(api, spoken_terms, lang).request
550
760
  end
@@ -565,6 +775,11 @@ module DeepL
565
775
  ##
566
776
  # Creates a style rule list. Keywords left +nil+ are not sent.
567
777
  #
778
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
779
+ # transient network failures while connecting, such as DNS resolution errors, refused
780
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
781
+ # proxy handshake), so that a retry cannot create a second style rule list.
782
+ #
568
783
  # @param [String] name Name of the style rule list.
569
784
  # @param [String] language Language the rules apply to, for example +'en'+.
570
785
  # @param [Hash, nil] configured_rules Category -> rule -> value, sent as given.
@@ -572,6 +787,8 @@ module DeepL
572
787
  # (+label+, +prompt+ and optionally +source_language+), sent as given.
573
788
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
574
789
  # @return [DeepL::Resources::StyleRule] The created style rule list.
790
+ # @raise [ArgumentError] If +name+ or +language+ is empty, or a custom instruction's
791
+ # +source_language+ is empty, before any request.
575
792
  def create_style_rule(name, language, configured_rules: nil, custom_instructions: nil,
576
793
  **options)
577
794
  options = options.merge(configured_rules: configured_rules,
@@ -585,6 +802,8 @@ module DeepL
585
802
  # @param [String, DeepL::Resources::StyleRule] style_rule ID or style rule list.
586
803
  # @return [DeepL::Resources::StyleRule]
587
804
  # @raise [DeepL::Exceptions::NotFound] If the style rule list does not exist.
805
+ # @raise [ArgumentError] If the style rule ID is empty or the list has no ID, before any
806
+ # request.
588
807
  def get_style_rule(style_rule)
589
808
  Requests::StyleRule::Find.new(api, style_rule).request
590
809
  end
@@ -596,6 +815,8 @@ module DeepL
596
815
  # @param [String] name New name.
597
816
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
598
817
  # @return [DeepL::Resources::StyleRule] The updated style rule list.
818
+ # @raise [ArgumentError] If the style rule ID or +name+ is empty, or the list has no ID, before
819
+ # any request.
599
820
  def update_style_rule_name(style_rule, name, **options)
600
821
  Requests::StyleRule::Update.new(api, style_rule, name, options).request
601
822
  end
@@ -607,6 +828,8 @@ module DeepL
607
828
  # @param [Hash] configured_rules Category -> rule -> value, sent as the request body.
608
829
  # @param [Hash] options +:extra_body_parameters+, added to the body as further keys.
609
830
  # @return [DeepL::Resources::StyleRule] The updated style rule list.
831
+ # @raise [ArgumentError] If the style rule ID is empty or the list has no ID, before any
832
+ # request.
610
833
  def update_style_rule_configured_rules(style_rule, configured_rules, **options)
611
834
  Requests::StyleRule::UpdateConfiguredRules.new(api, style_rule, configured_rules,
612
835
  options).request
@@ -617,6 +840,8 @@ module DeepL
617
840
  #
618
841
  # @param [String, DeepL::Resources::StyleRule] style_rule ID or style rule list.
619
842
  # @return [nil]
843
+ # @raise [ArgumentError] If the style rule ID is empty or the list has no ID, before any
844
+ # request.
620
845
  def delete_style_rule(style_rule)
621
846
  Requests::StyleRule::Destroy.new(api, style_rule).request
622
847
  nil
@@ -625,12 +850,19 @@ module DeepL
625
850
  ##
626
851
  # Adds a custom instruction to a style rule list. +source_language+ is not sent if +nil+.
627
852
  #
853
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
854
+ # transient network failures while connecting, such as DNS resolution errors, refused
855
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
856
+ # proxy handshake), so that a retry cannot create a second custom instruction.
857
+ #
628
858
  # @param [String, DeepL::Resources::StyleRule] style_rule ID or style rule list.
629
859
  # @param [String] label Label of the instruction.
630
860
  # @param [String] prompt The instruction.
631
861
  # @param [String, nil] source_language Source language the instruction applies to.
632
862
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
633
863
  # @return [DeepL::Resources::CustomInstruction] The created instruction.
864
+ # @raise [ArgumentError] If the style rule ID, +label+ or +prompt+ is empty, +source_language+
865
+ # is empty (+nil+ leaves it unset), or the list has no ID, before any request.
634
866
  def create_style_rule_custom_instruction(style_rule, label, prompt, source_language: nil,
635
867
  **options)
636
868
  Requests::StyleRule::CreateCustomInstruction.new(api, style_rule, label, prompt,
@@ -644,6 +876,8 @@ module DeepL
644
876
  # @param [String, DeepL::Resources::CustomInstruction] instruction_id ID or instruction.
645
877
  # @return [DeepL::Resources::CustomInstruction]
646
878
  # @raise [DeepL::Exceptions::NotFound] If the instruction does not exist.
879
+ # @raise [ArgumentError] If the style rule or instruction ID is empty or the object has no ID,
880
+ # before any request.
647
881
  def get_style_rule_custom_instruction(style_rule, instruction_id)
648
882
  Requests::StyleRule::FindCustomInstruction.new(api, style_rule, instruction_id).request
649
883
  end
@@ -659,6 +893,8 @@ module DeepL
659
893
  # @param [String, nil] source_language Source language the instruction applies to.
660
894
  # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
661
895
  # @return [DeepL::Resources::CustomInstruction] The updated instruction.
896
+ # @raise [ArgumentError] If an ID, +label+ or +prompt+ is empty, +source_language+ is empty
897
+ # (+nil+ leaves it unset), or an object has no ID, before any request.
662
898
  def update_style_rule_custom_instruction(style_rule, instruction_id, label, prompt,
663
899
  source_language: nil, **options)
664
900
  Requests::StyleRule::UpdateCustomInstruction.new(api, style_rule, instruction_id, label,
@@ -672,6 +908,8 @@ module DeepL
672
908
  # @param [String, DeepL::Resources::StyleRule] style_rule ID or style rule list.
673
909
  # @param [String, DeepL::Resources::CustomInstruction] instruction_id ID or instruction.
674
910
  # @return [nil]
911
+ # @raise [ArgumentError] If the style rule or instruction ID is empty or the object has no ID,
912
+ # before any request.
675
913
  def delete_style_rule_custom_instruction(style_rule, instruction_id)
676
914
  Requests::StyleRule::DestroyCustomInstruction.new(api, style_rule, instruction_id).request
677
915
  nil
@@ -692,6 +930,11 @@ module DeepL
692
930
  # memories. Keywords left +nil+ are not sent. If a component cannot be used, the request
693
931
  # fails and no style profile is created.
694
932
  #
933
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
934
+ # transient network failures while connecting, such as DNS resolution errors, refused
935
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
936
+ # proxy handshake), so that a retry cannot create a second style profile.
937
+ #
695
938
  # @param [String] name Name of the style profile.
696
939
  # @param [String, nil] description Free-text description.
697
940
  # @param [String, nil] industry The industry the style profile is written for.
@@ -774,6 +1017,7 @@ module DeepL
774
1017
  # memory.
775
1018
  # @return [DeepL::Resources::TranslationMemory]
776
1019
  # @raise [DeepL::Exceptions::NotFound] If the translation memory does not exist.
1020
+ # @raise [ArgumentError] If the ID is empty or the object has no ID, before any request.
777
1021
  def get_translation_memory(translation_memory)
778
1022
  translation_memories.find(translation_memory)
779
1023
  end
@@ -784,6 +1028,7 @@ module DeepL
784
1028
  # @param [String, DeepL::Resources::TranslationMemory] translation_memory ID or translation
785
1029
  # memory.
786
1030
  # @return [nil]
1031
+ # @raise [ArgumentError] If the ID is empty or the object has no ID, before any request.
787
1032
  def delete_translation_memory(translation_memory)
788
1033
  translation_memories.destroy(translation_memory)
789
1034
  nil
@@ -803,6 +1048,7 @@ module DeepL
803
1048
  # @param [Boolean, nil] filter_case_sensitive Whether +filter_text+ is case-sensitive (API
804
1049
  # default: false).
805
1050
  # @return [DeepL::Resources::TranslationMemorySegments]
1051
+ # @raise [ArgumentError] If the ID is empty or the object has no ID, before any request.
806
1052
  def list_translation_memory_segments(translation_memory, page_size: nil, page_cursor: nil,
807
1053
  filter_text: nil, filter_case_sensitive: nil)
808
1054
  options = { page_size: page_size, page_cursor: page_cursor, filter_text: filter_text,
@@ -815,18 +1061,25 @@ module DeepL
815
1061
  # upload URL with #upload_translation_memory_file, then wait for the job with
816
1062
  # #wait_until_translation_memory_job_done. Keywords left +nil+ are not sent.
817
1063
  #
1064
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
1065
+ # transient network failures while connecting, such as DNS resolution errors, refused
1066
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
1067
+ # proxy handshake), so that a retry cannot create a second import job.
1068
+ #
818
1069
  # @param [String] file_name Name of the TMX file, for example +'legal.tmx'+.
819
1070
  # @param [Integer] content_length Size of the file in bytes.
820
1071
  # @param [String, nil] content_type MIME type of the file (API default: +application/xml+).
821
1072
  # @param [String, nil] display_name Name of the translation memory (API default: the file
822
1073
  # name).
823
- # @param [Hash] options +:extra_body_parameters+, a hash added to the request body last.
1074
+ # @param [Hash, nil] extra_body_parameters A hash added to the request body last.
824
1075
  # @return [DeepL::Resources::TranslationMemoryImport] The job ID and the upload URL.
1076
+ # @raise [ArgumentError] If +file_name+ is empty or +content_length+ is not positive, before
1077
+ # any request.
825
1078
  def create_translation_memory_import(file_name, content_length, content_type: nil,
826
- display_name: nil, **options)
1079
+ display_name: nil, extra_body_parameters: nil)
827
1080
  translation_memories.create_import(file_name, content_length,
828
1081
  content_type: content_type, display_name: display_name,
829
- extra_body_parameters: options[:extra_body_parameters])
1082
+ extra_body_parameters: extra_body_parameters)
830
1083
  end
831
1084
 
832
1085
  ##
@@ -842,6 +1095,7 @@ module DeepL
842
1095
  # @return [nil]
843
1096
  # @raise [DeepL::Exceptions::Error] If the storage URL replies with a non-2xx status; the error
844
1097
  # carries +http_status_code+, and +should_retry?+ is true for 429 and 5xx.
1098
+ # @raise [ArgumentError] If the upload URL is empty, before any request.
845
1099
  def upload_translation_memory_file(translation_memory_import, content, content_type: nil)
846
1100
  translation_memories.upload_file(translation_memory_import, content,
847
1101
  content_type: content_type)
@@ -851,6 +1105,11 @@ module DeepL
851
1105
  # Imports a TMX file as a new translation memory: creates the import job, uploads the file
852
1106
  # and waits until the job is completed.
853
1107
  #
1108
+ # Creating the import job is retried only after failures that show the request was not processed
1109
+ # (HTTP 429 and transient network failures while connecting, such as DNS resolution errors,
1110
+ # refused connections, unreachable hosts or networks, connection timeouts and resets during the
1111
+ # TLS or proxy handshake), so that a retry cannot create a second import job.
1112
+ #
854
1113
  # @param [String] input_path Path of the TMX file.
855
1114
  # @param [String, nil] display_name Name of the translation memory (API default: the file
856
1115
  # name).
@@ -858,18 +1117,22 @@ module DeepL
858
1117
  # sent with the upload. +nil+ uses +application/xml+.
859
1118
  # @param [Numeric, nil] timeout_s Maximum time in seconds to wait for the job, +nil+ to wait
860
1119
  # until it is completed. The API reports +awaiting_input+ for a while after the upload.
861
- # @param [Hash] options +:extra_body_parameters+, a hash added to the body of the import job
862
- # request last.
1120
+ # @param [Hash, nil] extra_body_parameters A hash added to the body of the import job request
1121
+ # last.
863
1122
  # @return [DeepL::Resources::TranslationMemoryJob] The completed job; its result carries the
864
1123
  # ID of the new translation memory.
865
1124
  # @raise [ArgumentError] If the file does not exist, before any request is sent.
866
- # @raise [DeepL::Exceptions::Error] If the file cannot be read, the upload fails, the job fails
867
- # or expires (with the API's error message), or +timeout_s+ expires.
1125
+ # @raise [DeepL::Exceptions::TranslationMemoryJobError] For any other failure, the original
1126
+ # error as +cause+: an empty or unreadable file (before any request), an invalid argument or
1127
+ # failure of the job creation, a failed upload, a job that fails or expires (with the API's
1128
+ # error message), or an expired +timeout_s+. +job_id+ is set once the job was created, +job+
1129
+ # is the last status fetched (+nil+ before the first status check); +http_status_code+ and
1130
+ # +should_retry?+ are those of a DeepL error cause.
868
1131
  def import_translation_memory_from_filepath(input_path, display_name: nil, content_type: nil,
869
- timeout_s: nil, **options)
1132
+ timeout_s: nil, extra_body_parameters: nil)
870
1133
  translation_memories.import_from_filepath(
871
1134
  input_path, display_name: display_name, content_type: content_type, timeout_s: timeout_s,
872
- extra_body_parameters: options[:extra_body_parameters]
1135
+ extra_body_parameters: extra_body_parameters
873
1136
  )
874
1137
  end
875
1138
 
@@ -878,25 +1141,35 @@ module DeepL
878
1141
  # #wait_until_translation_memory_job_done, then download the TMX file with
879
1142
  # #download_translation_memory_export.
880
1143
  #
1144
+ # Creation is retried only after failures that show the request was not processed (HTTP 429 and
1145
+ # transient network failures while connecting, such as DNS resolution errors, refused
1146
+ # connections, unreachable hosts or networks, connection timeouts and resets during the TLS or
1147
+ # proxy handshake), so that a retry cannot create a second export job.
1148
+ #
881
1149
  # @param [String, DeepL::Resources::TranslationMemory] translation_memory ID or translation
882
1150
  # memory.
883
1151
  # @return [DeepL::Resources::TranslationMemoryExport] The job ID, and whether the API reused
884
1152
  # a previously completed export.
1153
+ # @raise [ArgumentError] If the ID is empty or the object has no ID, before any request.
885
1154
  def create_translation_memory_export(translation_memory)
886
1155
  translation_memories.create_export(translation_memory)
887
1156
  end
888
1157
 
889
1158
  ##
890
- # Downloads the TMX file of a completed export job to +output_path+, overwriting an existing
891
- # file. The download URL is a pre-signed storage URL: no +Authorization+ header and no
892
- # configured +headers+ are sent to it.
1159
+ # Downloads the TMX file of a completed export job to +output_path+. The download URL is a
1160
+ # pre-signed storage URL: no +Authorization+ header and no configured +headers+ are sent to
1161
+ # it.
893
1162
  #
894
- # @param [DeepL::Resources::TranslationMemoryJob, String] job The completed export job, or
895
- # its download URL.
896
- # @param [String] output_path Path to write the file to.
1163
+ # @param [DeepL::Resources::TranslationMemoryJob] job The completed export job, as returned
1164
+ # by #wait_until_translation_memory_job_done.
1165
+ # @param [String] output_path Path to write the file to. Must not exist.
897
1166
  # @return [nil]
898
- # @raise [DeepL::Exceptions::Error] If the job has no download URL, for example because it
899
- # is not completed yet, the download fails or the file cannot be written.
1167
+ # @raise [DeepL::Exceptions::Error] If the job has no download URL, for example because it is
1168
+ # not completed yet, before any request.
1169
+ # @raise [SystemCallError] +Errno::EEXIST+ if +output_path+ exists, +Errno::ENOENT+ or
1170
+ # +Errno::EACCES+ if its directory is missing or not writable (checked before the request),
1171
+ # or the error of writing the file.
1172
+ # @raise [DeepL::Exceptions::Error] If the download fails.
900
1173
  def download_translation_memory_export(job, output_path)
901
1174
  translation_memories.download_export(job, output_path)
902
1175
  nil
@@ -904,17 +1177,26 @@ module DeepL
904
1177
 
905
1178
  ##
906
1179
  # Exports a translation memory to a TMX file: creates the export job, waits until it is
907
- # completed and writes the file to +output_path+, overwriting an existing file.
1180
+ # completed and writes the file to +output_path+.
1181
+ #
1182
+ # Starting the export is retried only after failures that show the request was not processed
1183
+ # (HTTP 429 and transient network failures while connecting, such as DNS resolution errors,
1184
+ # refused connections, unreachable hosts or networks, connection timeouts and resets during the
1185
+ # TLS or proxy handshake), so that a retry cannot create a second export job.
908
1186
  #
909
1187
  # @param [String, DeepL::Resources::TranslationMemory] translation_memory ID or translation
910
1188
  # memory.
911
- # @param [String] output_path Path to write the file to.
1189
+ # @param [String] output_path Path to write the file to. Must not exist.
912
1190
  # @param [Numeric, nil] timeout_s Maximum time in seconds to wait for the job, +nil+ to wait
913
1191
  # until it is completed.
914
1192
  # @return [DeepL::Resources::TranslationMemoryJob] The completed job.
915
- # @raise [DeepL::Exceptions::Error] If the directory of +output_path+ does not exist (checked
916
- # before the job is created), the job fails or expires (with the API's error message),
917
- # +timeout_s+ expires, the download fails or the file cannot be written.
1193
+ # @raise [DeepL::Exceptions::TranslationMemoryJobError] For every failure, the original error
1194
+ # as +cause+: +output_path+ exists or its directory does not exist or is not writable
1195
+ # (checked before the job is created), an empty ID or an object without one, a failed job
1196
+ # creation, a job that fails or expires (with the API's error message), an expired
1197
+ # +timeout_s+, a failed download or a failure writing the file. +job_id+ is set once the job
1198
+ # was created, +job+ is the last status fetched (+nil+ before the first status check);
1199
+ # +http_status_code+ and +should_retry?+ are those of a DeepL error cause.
918
1200
  def export_translation_memory_to_filepath(translation_memory, output_path, timeout_s: nil)
919
1201
  translation_memories.export_to_filepath(translation_memory, output_path,
920
1202
  timeout_s: timeout_s)
@@ -925,6 +1207,7 @@ module DeepL
925
1207
  #
926
1208
  # @param [String, DeepL::Resources::TranslationMemoryJob] job ID or job.
927
1209
  # @return [DeepL::Resources::TranslationMemoryJob]
1210
+ # @raise [ArgumentError] If the ID is empty or the object has no ID, before any request.
928
1211
  def get_translation_memory_job(job)
929
1212
  translation_memories.find_job(job)
930
1213
  end
@@ -939,6 +1222,7 @@ module DeepL
939
1222
  # @return [DeepL::Resources::TranslationMemoryJob] The completed job.
940
1223
  # @raise [DeepL::Exceptions::Error] If the job fails or expires (with the API's error
941
1224
  # message), or +timeout_s+ expires.
1225
+ # @raise [ArgumentError] If the ID is empty or the object has no ID, before any request.
942
1226
  def wait_until_translation_memory_job_done(job, timeout_s: nil)
943
1227
  translation_memories.wait_until_job_done(job, timeout_s: timeout_s)
944
1228
  end
@@ -951,19 +1235,50 @@ module DeepL
951
1235
 
952
1236
  attr_reader :api
953
1237
 
954
- # The translation memory methods share the request, polling and ID handling of
955
- # TranslationMemoryApi.
956
1238
  def translation_memories
957
- @translation_memories ||= TranslationMemoryApi.new(api)
1239
+ @translation_memories ||= Utils::TranslationMemoryOperations.new(api)
958
1240
  end
959
1241
 
960
1242
  def configuration
961
1243
  api.configuration
962
1244
  end
963
1245
 
1246
+ def check_document_output_path(output_path)
1247
+ Utils::OutputPath.check!(output_path)
1248
+ rescue Errno::EEXIST
1249
+ raise Exceptions::DocumentTranslationError.new('File already exists at output path', nil)
1250
+ rescue SystemCallError => e
1251
+ raise Exceptions::DocumentTranslationError.new("Cannot write to output path: #{e.message}",
1252
+ nil, e)
1253
+ end
1254
+
964
1255
  def csv_dictionary(source_lang, target_lang, csv)
965
1256
  csv = csv.read if csv.respond_to?(:read)
966
1257
  { source_lang: source_lang, target_lang: target_lang, entries: csv, entries_format: 'csv' }
967
1258
  end
1259
+
1260
+ # Like #csv_dictionary, but raises ArgumentError for empty or whitespace-only CSV content.
1261
+ def non_empty_csv_dictionary(source_lang, target_lang, csv)
1262
+ dictionary = csv_dictionary(source_lang, target_lang, csv)
1263
+ check_csv_entries!([dictionary])
1264
+ dictionary
1265
+ end
1266
+
1267
+ # Raises ArgumentError if a dictionary hash gives its entries as a CSV string that is empty or
1268
+ # whitespace only. Other dictionaries are checked when the request body is built.
1269
+ def check_csv_entries!(dictionaries)
1270
+ return unless (dictionaries || []).any? { |dictionary| empty_csv_entries?(dictionary) }
1271
+
1272
+ raise ArgumentError, 'CSV content must not be empty'
1273
+ end
1274
+
1275
+ # Reads +:entries+ and +:entries_format+ like the request body does (symbol or string key).
1276
+ def empty_csv_entries?(dictionary)
1277
+ return false unless dictionary.is_a?(Hash)
1278
+
1279
+ value = ->(key) { dictionary[key] || dictionary[key.to_s] }
1280
+ entries = value.call(:entries)
1281
+ value.call(:entries_format) == 'csv' && entries.is_a?(String) && entries.strip.empty?
1282
+ end
968
1283
  end
969
1284
  end