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
@@ -0,0 +1,30 @@
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.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Utils
8
+ # TSV encoding of glossary entries, shared by the v2 and v3 glossary requests.
9
+ # @api private
10
+ module GlossaryEntries
11
+ extend self
12
+
13
+ # A hash, source/target pairs or a TSV string of them, checked and joined as TSV.
14
+ def to_tsv(entries)
15
+ entries_class = Resources::MultilingualGlossaryDictionaryEntries
16
+ entries = entries.is_a?(String) ? entries_class.tsv_to_pairs(entries) : entries.to_a
17
+ entries_class.check_pairs!(entries).map do |entry|
18
+ source = TermValidator.validate!(entry.first)
19
+ target = TermValidator.validate!(entry.last)
20
+ "#{source}\t#{target}"
21
+ end.join("\n")
22
+ end
23
+
24
+ # Source term to target term, one pair per non-empty TSV line.
25
+ def from_tsv(tsv)
26
+ tsv.to_s.split(/\r?\n/).reject(&:empty?).to_h { |line| line.split("\t", 2) }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -8,6 +8,8 @@ module DeepL
8
8
  module HTTPClientBuilder
9
9
  extend self
10
10
 
11
+ PROXY_SCHEMES = %w[http https].freeze
12
+
11
13
  ##
12
14
  # Builds an unstarted Net::HTTP client for +host+ configured with +options+. Net::HTTP does
13
15
  # not retry on its own (+max_retries+ 0): only the library's retries apply.
@@ -17,7 +19,7 @@ module DeepL
17
19
  # @return [Net::HTTP]
18
20
  def build(host, options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
19
21
  uri = URI(host)
20
- http = Net::HTTP.new(uri.host, uri.port, *proxy_arguments(options.proxy))
22
+ http = Net::HTTP.new(uri.host, uri.port, *proxy_arguments(options.proxy, uri))
21
23
  http.use_ssl = uri.scheme == 'https'
22
24
  disable_lenient_defaults(http)
23
25
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless options.enable_ssl_verification
@@ -40,21 +42,56 @@ module DeepL
40
42
 
41
43
  private
42
44
 
43
- # The proxy is either a URL string, e.g. +http://user:pass@proxy.example:8080+, or a hash
44
- # with +proxy_addr+ and +proxy_port+. Returns the proxy arguments of +Net::HTTP.new+.
45
- def proxy_arguments(proxy)
46
- if proxy.is_a?(String) && !proxy.empty?
47
- proxy_uri = parse_proxy_url(proxy)
45
+ # The proxy is a URL string or +URI+ with the +http+ or +https+ scheme, e.g.
46
+ # +http://user:pass@proxy.example:8080+, or a hash with +proxy_addr+ and +proxy_port+ (string
47
+ # or symbol keys), which is a plain HTTP proxy. +nil+, +''+ and an empty hash mean no proxy;
48
+ # any other value raises ArgumentError. Returns the proxy arguments of +Net::HTTP.new+ for the
49
+ # server +uri+.
50
+ def proxy_arguments(proxy, uri)
51
+ return [nil, nil] if proxy.nil? || proxy == '' || proxy == {}
52
+
53
+ proxy = proxy.to_s if proxy.is_a?(URI::Generic)
54
+ return proxy_url_arguments(proxy, uri) if proxy.is_a?(String)
55
+
56
+ hash_proxy_arguments(proxy)
57
+ end
58
+
59
+ def proxy_url_arguments(proxy, uri)
60
+ proxy_uri = parse_proxy_url(proxy)
61
+ # The messages name the problem only: the proxy URL may hold credentials.
62
+ unless PROXY_SCHEMES.include?(proxy_uri.scheme)
63
+ raise ArgumentError, 'Invalid proxy URL scheme: expected http or https', cause: nil
64
+ end
65
+
66
+ arguments = [proxy_uri.host, proxy_uri.port, decode(proxy_uri.user),
67
+ decode(proxy_uri.password)]
68
+ proxy_uri.scheme == 'https' ? https_proxy_arguments(arguments, uri) : arguments
69
+ end
48
70
 
49
- return [proxy_uri.host, proxy_uri.port, decode(proxy_uri.user),
50
- decode(proxy_uri.password)]
71
+ # Net::HTTP connects to the proxy over TLS (+p_use_ssl+) from net-http 0.5.0, and only for an
72
+ # https server; it does not verify the proxy's certificate.
73
+ def https_proxy_arguments(arguments, uri)
74
+ unless Net::HTTP.method_defined?(:proxy_use_ssl=)
75
+ raise ArgumentError, 'An https proxy needs net-http 0.5.0 or later (installed: ' \
76
+ "#{Net::HTTP::VERSION})", cause: nil
77
+ end
78
+ unless uri.scheme == 'https'
79
+ raise ArgumentError, 'An https proxy can only be used for an https server URL', cause: nil
51
80
  end
52
- unless proxy.is_a?(Hash) && proxy.key?('proxy_addr') && proxy.key?('proxy_port')
53
- return [nil,
54
- nil]
81
+
82
+ arguments + [nil, true]
83
+ end
84
+
85
+ def hash_proxy_arguments(proxy)
86
+ if proxy.is_a?(Hash)
87
+ address = proxy.fetch('proxy_addr') { proxy[:proxy_addr] }
88
+ port = proxy.fetch('proxy_port') { proxy[:proxy_port] }
89
+ return [address, port] unless address.nil? || port.nil? || address.to_s.strip.empty?
55
90
  end
56
91
 
57
- [proxy['proxy_addr'], proxy['proxy_port']]
92
+ raise ArgumentError,
93
+ 'Invalid proxy: expected a URL string, a URI or a hash with proxy_addr and ' \
94
+ "proxy_port, got #{HTTPClientOptions.redact_proxy(proxy).inspect}"
58
95
  end
59
96
 
60
97
  # Error messages show the URL without its user info; the URI error, which repeats the URL,
@@ -65,7 +102,7 @@ module DeepL
65
102
  rescue URI::InvalidURIError
66
103
  nil
67
104
  end
68
- return proxy_uri unless proxy_uri&.host.nil?
105
+ return proxy_uri unless proxy_uri.nil? || proxy_uri.host.to_s.empty?
69
106
 
70
107
  raise ArgumentError, "Invalid proxy URL: #{HTTPClientOptions.redact_proxy(proxy)}",
71
108
  cause: nil
@@ -0,0 +1,25 @@
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.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Utils
8
+ # Checks of an output file path before any request is sent.
9
+ # @api private
10
+ module OutputPath
11
+ extend self
12
+
13
+ # @raise [Errno::EEXIST] If +path+ exists.
14
+ # @raise [Errno::ENOENT] If the directory of +path+ does not exist.
15
+ # @raise [Errno::EACCES] If the directory of +path+ is not writable.
16
+ def check!(path)
17
+ raise Errno::EEXIST, path.to_s if File.exist?(path)
18
+
19
+ directory = File.dirname(path)
20
+ raise Errno::ENOENT, directory unless File.directory?(directory)
21
+ raise Errno::EACCES, directory unless File.writable?(directory)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -9,7 +9,7 @@ module DeepL
9
9
  extend self
10
10
 
11
11
  # C0 and C1 control characters plus the Unicode line and paragraph separators.
12
- INVALID_CHARACTERS = /[\u0000-\u001F\u0080-\u009F

]/.freeze
12
+ INVALID_CHARACTERS = /[\u0000-\u001F\u0080-\u009F

]/
13
13
 
14
14
  ##
15
15
  # Strips surrounding whitespace from a single glossary or spoken term, then checks it
@@ -17,8 +17,8 @@ module DeepL
17
17
  #
18
18
  # @param [String] term The term to check.
19
19
  # @return [String] The stripped term, which is what the request sends.
20
- # @raise [DeepL::Exceptions::ValidationError] If the stripped term is empty or contains a
21
- # control character.
20
+ # @raise [DeepL::Exceptions::ValidationError] (an ArgumentError) If the stripped term is empty
21
+ # or contains a control character.
22
22
  def validate!(term)
23
23
  term = term.to_s.strip
24
24
  raise Exceptions::ValidationError, 'Term must not be empty' if term.empty?
@@ -0,0 +1,230 @@
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.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Utils
8
+ # The requests, polling and ID handling behind the translation memory methods of
9
+ # DeepL::Client.
10
+ # @api private
11
+ class TranslationMemoryOperations # rubocop:disable Metrics/ClassLength
12
+ # Time to wait between two status queries of an import or export job.
13
+ JOB_POLLING_INTERVAL_SECONDS = 5
14
+
15
+ def initialize(api)
16
+ @api = api
17
+ end
18
+
19
+ def list(options = {})
20
+ DeepL::Requests::TranslationMemory::List.new(@api, options).request
21
+ end
22
+
23
+ def find(translation_memory, options = {})
24
+ DeepL::Requests::TranslationMemory::Find.new(
25
+ @api, extract_translation_memory_id(translation_memory), options
26
+ ).request
27
+ end
28
+
29
+ def segments(translation_memory, options = {})
30
+ DeepL::Requests::TranslationMemory::Segments.new(
31
+ @api, extract_translation_memory_id(translation_memory), options
32
+ ).request
33
+ end
34
+
35
+ def destroy(translation_memory, options = {})
36
+ DeepL::Requests::TranslationMemory::Destroy.new(
37
+ @api, extract_translation_memory_id(translation_memory), options
38
+ ).request
39
+ end
40
+
41
+ def create_import(file_name, content_length, content_type: nil, display_name: nil,
42
+ extra_body_parameters: nil)
43
+ options = { content_type: content_type, display_name: display_name,
44
+ extra_body_parameters: extra_body_parameters }.compact
45
+ DeepL::Requests::TranslationMemory::CreateImport.new(
46
+ @api, file_name, content_length, options
47
+ ).request
48
+ end
49
+
50
+ def upload_file(translation_memory_import, file_content,
51
+ content_type: Requests::TranslationMemory::UploadFile::DEFAULT_CONTENT_TYPE)
52
+ DeepL::Requests::TranslationMemory::UploadFile.new(
53
+ @api, extract_upload_url(translation_memory_import), file_content, content_type
54
+ ).request
55
+ end
56
+
57
+ def create_export(translation_memory, options = {})
58
+ DeepL::Requests::TranslationMemory::CreateExport.new(
59
+ @api, extract_translation_memory_id(translation_memory), options
60
+ ).request
61
+ end
62
+
63
+ def find_job(job, options = {})
64
+ DeepL::Requests::TranslationMemory::FindJob.new(@api, extract_job_id(job), options).request
65
+ end
66
+
67
+ # Yields each status it receives, if a block is given.
68
+ def wait_until_job_done(job, timeout_s: nil, &on_status)
69
+ deadline = timeout_s.nil? ? nil : monotonic_time + timeout_s
70
+ job_status = find_job(job).tap { |status| on_status&.call(status) }
71
+ until job_status.finished?
72
+ interval = polling_interval_until(deadline, timeout_s)
73
+ log_job_polling(interval)
74
+ sleep(interval)
75
+ job_status = find_job(job).tap { |status| on_status&.call(status) }
76
+ end
77
+ raise_job_error(job_status) if job_status.error?
78
+
79
+ job_status
80
+ end
81
+
82
+ def download_export(job, output_path)
83
+ download_url = extract_download_url(job)
84
+ Utils::OutputPath.check!(output_path)
85
+ DeepL::Requests::TranslationMemory::DownloadExport.new(@api, download_url,
86
+ output_path).request
87
+ end
88
+
89
+ def import_from_filepath(input_file_path, display_name: nil, timeout_s: nil,
90
+ content_type: nil, extra_body_parameters: nil)
91
+ check_input_file(input_file_path)
92
+ with_job_error('import') do |progress|
93
+ file_content = read_input_file(input_file_path)
94
+ raise ArgumentError, "File #{input_file_path} is empty" if file_content.empty?
95
+
96
+ created = create_import(File.basename(input_file_path), file_content.bytesize,
97
+ content_type: content_type, display_name: display_name,
98
+ extra_body_parameters: extra_body_parameters)
99
+ upload_and_wait(created, file_content, content_type, timeout_s, progress)
100
+ end
101
+ end
102
+
103
+ def export_to_filepath(translation_memory, output_path, timeout_s: nil)
104
+ with_job_error('export') do |progress|
105
+ check_output_path(output_path)
106
+ created = create_export(translation_memory)
107
+ progress[:job_id] = created.job_id
108
+ job = wait_until_job_done(created.job_id, timeout_s: timeout_s) { progress[:job] = _1 }
109
+ download_export(job, output_path)
110
+ job
111
+ end
112
+ end
113
+
114
+ private
115
+
116
+ def extract_translation_memory_id(translation_memory)
117
+ id = if translation_memory.is_a?(Resources::TranslationMemory)
118
+ translation_memory.translation_memory_id
119
+ else
120
+ translation_memory
121
+ end
122
+ raise ArgumentError, 'Translation memory ID must not be empty' if blank?(id)
123
+
124
+ id
125
+ end
126
+
127
+ def extract_job_id(job)
128
+ id = job.is_a?(Resources::TranslationMemoryJob) ? job.job_id : job
129
+ raise ArgumentError, 'Job ID must not be empty' if blank?(id)
130
+
131
+ id
132
+ end
133
+
134
+ def extract_upload_url(translation_memory_import)
135
+ if translation_memory_import.is_a?(Resources::TranslationMemoryImport)
136
+ translation_memory_import.upload_url
137
+ else
138
+ translation_memory_import
139
+ end
140
+ end
141
+
142
+ def extract_download_url(job)
143
+ download_url = job.result&.download_url
144
+ if blank?(download_url)
145
+ raise Exceptions::Error, 'Translation memory export job has no download URL, ' \
146
+ 'it may not have completed yet'
147
+ end
148
+
149
+ download_url
150
+ end
151
+
152
+ def blank?(value)
153
+ value.nil? || value.empty?
154
+ end
155
+
156
+ def upload_and_wait(created, file_content, content_type, timeout_s, progress)
157
+ progress[:job_id] = created.job_id
158
+ upload_file(created, file_content, content_type: content_type)
159
+ wait_until_job_done(created.job_id, timeout_s: timeout_s) { progress[:job] = _1 }
160
+ end
161
+
162
+ # Wraps every failure of the block in a TranslationMemoryJobError carrying
163
+ # +progress[:job_id]+ (set once the job was created) and +progress[:job]+ (its last status).
164
+ def with_job_error(operation)
165
+ progress = {}
166
+ yield progress
167
+ rescue StandardError => e
168
+ prefix = "Error occurred during the translation memory #{operation}"
169
+ # A failed job's error from the wait already starts with the prefix.
170
+ message = e.message.start_with?(prefix) ? e.message : "#{prefix}: #{e.message}"
171
+ raise Exceptions::TranslationMemoryJobError.new(message, progress[:job_id],
172
+ progress[:job], e)
173
+ end
174
+
175
+ def check_input_file(input_file_path)
176
+ return if File.exist?(input_file_path)
177
+
178
+ raise ArgumentError, "No file found at #{input_file_path}"
179
+ end
180
+
181
+ def read_input_file(input_file_path)
182
+ File.binread(input_file_path)
183
+ rescue SystemCallError, IOError => e
184
+ raise Exceptions::Error, "Could not read #{input_file_path}: #{e.message}"
185
+ end
186
+
187
+ def check_output_path(output_path)
188
+ directory = File.dirname(output_path)
189
+ unless File.directory?(directory)
190
+ raise Exceptions::Error, "Output directory #{directory} does not exist"
191
+ end
192
+ unless File.writable?(directory)
193
+ raise Exceptions::Error, "Output directory #{directory} is not writable"
194
+ end
195
+ raise Exceptions::Error, "File already exists at #{output_path}" if File.exist?(output_path)
196
+ end
197
+
198
+ def log_job_polling(interval)
199
+ @api.configuration.logger&.info('Rechecking translation memory job status after sleeping ' \
200
+ "for #{format('%.2f', interval)} seconds.")
201
+ end
202
+
203
+ def monotonic_time
204
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
205
+ end
206
+
207
+ # The polling interval, cut to the time left until +deadline+ so that a wait never runs
208
+ # past +timeout_s+.
209
+ def polling_interval_until(deadline, timeout_s)
210
+ return JOB_POLLING_INTERVAL_SECONDS if deadline.nil?
211
+
212
+ remaining = deadline - monotonic_time
213
+ raise_timeout_error(timeout_s) if remaining <= 0
214
+
215
+ [JOB_POLLING_INTERVAL_SECONDS, remaining].min
216
+ end
217
+
218
+ def raise_timeout_error(timeout_s)
219
+ raise Exceptions::Error,
220
+ "Manual timeout of #{timeout_s}s exceeded for the translation memory job"
221
+ end
222
+
223
+ def raise_job_error(job_status)
224
+ raise Exceptions::Error,
225
+ "Error occurred during the translation memory #{job_status.operation}: " \
226
+ "#{job_status.error_message || job_status.status}"
227
+ end
228
+ end
229
+ end
230
+ end
data/lib/deepl.rb CHANGED
@@ -8,9 +8,6 @@ require 'fileutils'
8
8
  require 'json'
9
9
  require 'net/http'
10
10
 
11
- # -- Deprecation
12
- require_relative 'deepl/deprecation'
13
-
14
11
  # -- Exceptions
15
12
  require_relative 'deepl/exceptions/error'
16
13
  require_relative 'deepl/exceptions/validation_error'
@@ -20,9 +17,9 @@ require_relative 'deepl/exceptions/bad_request'
20
17
  require_relative 'deepl/exceptions/limit_exceeded'
21
18
  require_relative 'deepl/exceptions/quota_exceeded'
22
19
  require_relative 'deepl/exceptions/not_found'
23
- require_relative 'deepl/exceptions/not_supported'
24
20
  require_relative 'deepl/exceptions/request_entity_too_large'
25
21
  require_relative 'deepl/exceptions/document_translation_error'
22
+ require_relative 'deepl/exceptions/translation_memory_job_error'
26
23
  require_relative 'deepl/exceptions/server_error'
27
24
  require_relative 'deepl/exceptions/document_not_ready_error'
28
25
  require_relative 'deepl/exceptions/connection_error'
@@ -32,6 +29,7 @@ require_relative 'deepl/requests/base'
32
29
  require_relative 'deepl/requests/document/download'
33
30
  require_relative 'deepl/requests/document/get_status'
34
31
  require_relative 'deepl/requests/document/upload'
32
+ require_relative 'deepl/requests/glossary/base'
35
33
  require_relative 'deepl/requests/glossary/create'
36
34
  require_relative 'deepl/requests/glossary/destroy'
37
35
  require_relative 'deepl/requests/glossary/entries'
@@ -131,7 +129,10 @@ require_relative 'deepl/utils/backoff_timer'
131
129
  require_relative 'deepl/utils/time_parser'
132
130
  require_relative 'deepl/utils/number_parser'
133
131
  require_relative 'deepl/utils/term_validator'
132
+ require_relative 'deepl/utils/output_path'
133
+ require_relative 'deepl/utils/glossary_entries'
134
134
  require_relative 'deepl/utils/document_poller'
135
+ require_relative 'deepl/utils/translation_memory_operations'
135
136
 
136
137
  # -- Constants
137
138
  require_relative 'deepl/constants/base_constant'
@@ -157,178 +158,12 @@ require_relative 'version'
157
158
  # -- Other wrappers
158
159
  require_relative 'deepl/api'
159
160
  require_relative 'deepl/configuration'
160
- require_relative 'deepl/document_api'
161
- require_relative 'deepl/glossary_api'
162
- require_relative 'deepl/style_rule_api'
163
- require_relative 'deepl/translation_memory_api'
164
161
  require_relative 'deepl/app_info'
165
162
  require_relative 'deepl/client'
166
163
  require_relative 'deepl/admin_client'
167
164
 
168
165
  # -- Gem interface
169
166
  #
170
- # The module-level API (+DeepL.configure+, +DeepL.translate+, +DeepL.glossaries+, ...) keeps its
171
- # configuration in module state. It is deprecated in favour of DeepL::Client and will be removed
172
- # in the next major version.
167
+ # Create a DeepL::Client (or a DeepL::AdminClient for the Admin API) to send requests.
173
168
  module DeepL
174
- extend self
175
-
176
- ## -- API shortcuts
177
-
178
- # @deprecated Use DeepL::Client instead.
179
- def api
180
- deprecated(:api, 'DeepL::Client')
181
- module_api
182
- end
183
-
184
- # @deprecated Use DeepL::Client#get_source_languages or DeepL::Client#get_target_languages
185
- # instead.
186
- def languages(options = {})
187
- deprecated(:languages,
188
- 'DeepL::Client#get_source_languages or DeepL::Client#get_target_languages')
189
- Requests::Languages.new(module_api, options).request
190
- end
191
-
192
- ##
193
- # Translates +text+ from +source_lang+ into +target_lang+.
194
- #
195
- # @param [String, Array<String>] text Text(s) to translate.
196
- # @param [String, nil] source_lang Source language. `nil` enables automatic detection.
197
- # @param [String] target_lang Target language.
198
- # @param [Hash] options Additional (body) options for the translation. Notable options:
199
- # * +:glossary+ - A glossary ID, `DeepL::Resources::Glossary` or
200
- # `DeepL::Resources::MultilingualGlossary` to use for the translation, sent as
201
- # +glossary_id+. Cannot be combined with +:glossary_id+ or +:glossary_ids+.
202
- # * +:glossary_id+ - Deprecated, use +:glossary+. A single glossary ID (string).
203
- # * +:glossary_ids+ - An array of up to 5 glossary IDs (strings,
204
- # `DeepL::Resources::Glossary` or `DeepL::Resources::MultilingualGlossary` objects) to
205
- # use for the translation. Glossaries are applied in order (first match wins). Without
206
- # +source_lang+, the API detects it. Cannot be combined with +:glossary+ or
207
- # +:glossary_id+. Raises `ArgumentError` if these rules are violated or more than 5 IDs
208
- # are provided.
209
- # * +:style_rule+ - A style rule ID string or a `DeepL::Resources::StyleRule` object.
210
- # * +:style_profile+ - A style profile ID string or a `DeepL::Resources::StyleProfile`
211
- # object. Applies the profile's glossaries, style rule lists and translation memories.
212
- # @param [Hash] additional_headers Additional HTTP headers for the translation.
213
- # @return [DeepL::Resources::Text, Array<DeepL::Resources::Text>] Translated text resource(s).
214
- # @deprecated Use DeepL::Client#translate_text instead.
215
- def translate(text, source_lang, target_lang, options = {}, additional_headers = {})
216
- deprecated(:translate, 'DeepL::Client#translate_text')
217
- ensure_configured
218
- Requests::Translate.new(module_api, text, source_lang, target_lang, options,
219
- additional_headers).request
220
- end
221
-
222
- # @deprecated Use DeepL::Client instead.
223
- def document(options = {})
224
- deprecated(:document, 'DeepL::Client')
225
- ensure_configured
226
- DocumentApi.new(module_api, options)
227
- end
228
-
229
- # @deprecated Use DeepL::Client instead.
230
- def glossaries(options = {})
231
- deprecated(:glossaries, 'DeepL::Client')
232
- ensure_configured
233
- GlossaryApi.new(module_api, options)
234
- end
235
-
236
- # @deprecated Use DeepL::Client instead.
237
- def style_rules(options = {})
238
- deprecated(:style_rules, 'DeepL::Client')
239
- ensure_configured
240
- StyleRuleApi.new(module_api, options)
241
- end
242
-
243
- # @deprecated Use DeepL::Client instead.
244
- def translation_memories(options = {})
245
- deprecated(:translation_memories, 'DeepL::Client')
246
- ensure_configured
247
- TranslationMemoryApi.new(module_api, options)
248
- end
249
-
250
- # @deprecated Use DeepL::Client#rephrase_text instead.
251
- def rephrase(text, target_lang = nil, writing_style = nil, tone = nil, options = {}, # rubocop:disable Metrics/ParameterLists
252
- additional_headers = {})
253
- deprecated(:rephrase, 'DeepL::Client#rephrase_text')
254
- ensure_configured
255
- Requests::Rephrase.new(module_api, text, target_lang, writing_style, tone, options,
256
- additional_headers).request
257
- end
258
-
259
- # @deprecated Use DeepL::Client#get_usage instead.
260
- def usage(options = {})
261
- deprecated(:usage, 'DeepL::Client#get_usage')
262
- ensure_configured
263
- Requests::Usage.new(module_api, options).request
264
- end
265
-
266
- # @deprecated Use DeepL::Client with +http_client_options:+ instead.
267
- def http_client
268
- deprecated(:http_client, 'DeepL::Client')
269
- @http_client
270
- end
271
-
272
- # @deprecated Use DeepL::Client with +http_client_options:+ instead.
273
- def with_session(client_options = HTTPClientOptions.new) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
274
- raise ArgumentError, 'This method requires a block to be passed in which contains the actual API calls, see README for example usage.' unless block_given? # rubocop:disable Layout/LineLength
275
-
276
- deprecated(:with_session, 'DeepL::Client')
277
- previous_api_client = module_api.http_client
278
- previous_api_options = module_api.http_client_options
279
- previous_http_client = @http_client
280
- begin
281
- http = Utils::HTTPClientBuilder.build(module_configuration.host, client_options)
282
- start_session(http, client_options)
283
- @http_client = http
284
- module_api.update_http_client(http, client_options)
285
- yield
286
- ensure
287
- module_api.update_http_client(previous_api_client, previous_api_options)
288
- @http_client = previous_http_client
289
- http.finish if http&.started?
290
- end
291
- end
292
-
293
- # -- Configuration
294
-
295
- # @deprecated Use DeepL::Client.new instead.
296
- def configuration
297
- deprecated(:configuration, 'DeepL::Client.new')
298
- module_configuration
299
- end
300
-
301
- # @deprecated Use DeepL::Client.new instead.
302
- def configure
303
- deprecated(:configure, 'DeepL::Client.new')
304
- yield module_configuration if block_given?
305
- module_configuration.validate!
306
- end
307
-
308
- private
309
-
310
- def module_configuration
311
- @configuration ||= Configuration.new # rubocop:disable Naming/MemoizedInstanceVariableName
312
- end
313
-
314
- def module_api
315
- @api ||= API.new(module_configuration) # rubocop:disable Naming/MemoizedInstanceVariableName
316
- end
317
-
318
- # Opens the session connection within the configured timeout, unless the options set one.
319
- def start_session(http, client_options)
320
- timeout = module_configuration.timeout
321
- http.open_timeout = timeout if client_options.open_timeout.nil? && timeout
322
- http.start
323
- end
324
-
325
- def ensure_configured
326
- module_configuration.validate! if @configuration.nil?
327
- end
328
-
329
- def deprecated(name, replacement)
330
- Deprecation.warn_once("DeepL.#{name}",
331
- "[DEPRECATION] DeepL.#{name} is deprecated, use #{replacement} instead.",
332
- uplevel: 3)
333
- end
334
169
  end
@@ -20,10 +20,10 @@ module DeepL
20
20
  end
21
21
 
22
22
  # Hides the user info of a proxy URL: +http://user:pass@host:port+ becomes
23
- # +http://[REDACTED]@host:port+.
23
+ # +http://[REDACTED]@host:port+, and +//user:pass@host:port+ +//[REDACTED]@host:port+.
24
24
  def self.redact_proxy(proxy)
25
25
  if proxy.is_a?(String)
26
- return proxy.sub(%r{\A([a-z][a-z0-9+.-]*://)?[^/@]*@}i,
26
+ return proxy.sub(%r{\A((?:[a-z][a-z0-9+.-]*:)?//)?[^/@]*@}i,
27
27
  '\1[REDACTED]@')
28
28
  end
29
29
  return proxy unless proxy.is_a?(Hash)
data/lib/version.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module DeepL
7
- VERSION = '3.10.0'
7
+ VERSION = '4.0.0'
8
8
  end