deepl-rb 3.9.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 (215) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +27 -1
  3. data/CHANGELOG.md +741 -18
  4. data/Gemfile +4 -0
  5. data/README.md +1001 -327
  6. data/Rakefile +29 -7
  7. data/VERSION +1 -1
  8. data/deepl-rb.gemspec +75 -88
  9. data/lib/deepl/admin_client.rb +208 -0
  10. data/lib/deepl/api.rb +18 -8
  11. data/lib/deepl/app_info.rb +19 -0
  12. data/lib/deepl/client.rb +1284 -0
  13. data/lib/deepl/configuration.rb +27 -32
  14. data/lib/deepl/constants/admin_usage_group_by.rb +13 -0
  15. data/lib/deepl/constants/custom_tag_usage_aggregate_by.rb +13 -0
  16. data/lib/deepl/constants/document_status_code.rb +17 -0
  17. data/lib/deepl/constants/language_include.rb +17 -0
  18. data/lib/deepl/constants/language_resource_name.rb +21 -0
  19. data/lib/deepl/constants/tag_handling_version.rb +14 -0
  20. data/lib/deepl/exceptions/authorization_failed.rb +1 -1
  21. data/lib/deepl/exceptions/bad_request.rb +1 -3
  22. data/lib/deepl/exceptions/connection_error.rb +40 -0
  23. data/lib/deepl/exceptions/document_not_ready_error.rb +17 -0
  24. data/lib/deepl/exceptions/document_translation_error.rb +17 -1
  25. data/lib/deepl/exceptions/error.rb +13 -1
  26. data/lib/deepl/exceptions/limit_exceeded.rb +1 -1
  27. data/lib/deepl/exceptions/not_found.rb +8 -3
  28. data/lib/deepl/exceptions/quota_exceeded.rb +1 -1
  29. data/lib/deepl/exceptions/request_entity_too_large.rb +1 -2
  30. data/lib/deepl/exceptions/request_error.rb +26 -2
  31. data/lib/deepl/exceptions/server_error.rb +9 -2
  32. data/lib/deepl/exceptions/translation_memory_job_error.rb +32 -0
  33. data/lib/deepl/exceptions/validation_error.rb +15 -0
  34. data/lib/deepl/requests/admin/base.rb +94 -0
  35. data/lib/deepl/requests/admin/create_developer_key.rb +38 -0
  36. data/lib/deepl/requests/admin/custom_tag_usage_analytics.rb +38 -0
  37. data/lib/deepl/requests/admin/list_developer_keys.rb +28 -0
  38. data/lib/deepl/requests/admin/update_developer_key.rb +35 -0
  39. data/lib/deepl/requests/admin/usage_analytics.rb +36 -0
  40. data/lib/deepl/requests/base.rb +313 -65
  41. data/lib/deepl/requests/correct.rb +51 -0
  42. data/lib/deepl/requests/document/download.rb +11 -3
  43. data/lib/deepl/requests/document/get_status.rb +2 -2
  44. data/lib/deepl/requests/document/upload.rb +56 -20
  45. data/lib/deepl/requests/glossary/base.rb +30 -0
  46. data/lib/deepl/requests/glossary/create.rb +18 -18
  47. data/lib/deepl/requests/glossary/destroy.rb +4 -16
  48. data/lib/deepl/requests/glossary/entries.rb +6 -14
  49. data/lib/deepl/requests/glossary/find.rb +3 -13
  50. data/lib/deepl/requests/glossary/language_pairs.rb +22 -6
  51. data/lib/deepl/requests/glossary/list.rb +2 -2
  52. data/lib/deepl/requests/language_resources.rb +41 -0
  53. data/lib/deepl/requests/languages.rb +25 -6
  54. data/lib/deepl/requests/languages_for_resource.rb +56 -0
  55. data/lib/deepl/requests/multilingual_glossary/create.rb +45 -0
  56. data/lib/deepl/requests/multilingual_glossary/destroy.rb +36 -0
  57. data/lib/deepl/requests/multilingual_glossary/destroy_dictionary.rb +55 -0
  58. data/lib/deepl/requests/multilingual_glossary/entries.rb +68 -0
  59. data/lib/deepl/requests/multilingual_glossary/find.rb +35 -0
  60. data/lib/deepl/requests/multilingual_glossary/helpers.rb +71 -0
  61. data/lib/deepl/requests/multilingual_glossary/list.rb +39 -0
  62. data/lib/deepl/requests/multilingual_glossary/replace_dictionary.rb +41 -0
  63. data/lib/deepl/requests/multilingual_glossary/update.rb +40 -0
  64. data/lib/deepl/requests/rephrase.rb +8 -10
  65. data/lib/deepl/requests/spoken_terms/create.rb +42 -0
  66. data/lib/deepl/requests/spoken_terms/destroy.rb +36 -0
  67. data/lib/deepl/requests/spoken_terms/destroy_term_list.rb +42 -0
  68. data/lib/deepl/requests/spoken_terms/entries.rb +47 -0
  69. data/lib/deepl/requests/spoken_terms/find.rb +35 -0
  70. data/lib/deepl/requests/spoken_terms/helpers.rb +74 -0
  71. data/lib/deepl/requests/spoken_terms/list.rb +35 -0
  72. data/lib/deepl/requests/spoken_terms/replace_term_list.rb +41 -0
  73. data/lib/deepl/requests/spoken_terms/update.rb +40 -0
  74. data/lib/deepl/requests/style_profile/base.rb +83 -0
  75. data/lib/deepl/requests/style_profile/create.rb +40 -0
  76. data/lib/deepl/requests/style_profile/destroy.rb +34 -0
  77. data/lib/deepl/requests/style_profile/find.rb +33 -0
  78. data/lib/deepl/requests/style_profile/list.rb +43 -0
  79. data/lib/deepl/requests/style_profile/update.rb +35 -0
  80. data/lib/deepl/requests/style_rule/create.rb +24 -0
  81. data/lib/deepl/requests/style_rule/create_custom_instruction.rb +11 -2
  82. data/lib/deepl/requests/style_rule/destroy.rb +4 -2
  83. data/lib/deepl/requests/style_rule/destroy_custom_instruction.rb +5 -3
  84. data/lib/deepl/requests/style_rule/find.rb +4 -2
  85. data/lib/deepl/requests/style_rule/find_custom_instruction.rb +5 -3
  86. data/lib/deepl/requests/style_rule/helpers.rb +44 -0
  87. data/lib/deepl/requests/style_rule/list.rb +3 -1
  88. data/lib/deepl/requests/style_rule/update.rb +5 -2
  89. data/lib/deepl/requests/style_rule/update_configured_rules.rb +4 -2
  90. data/lib/deepl/requests/style_rule/update_custom_instruction.rb +7 -3
  91. data/lib/deepl/requests/translate.rb +43 -27
  92. data/lib/deepl/requests/translation_memory/base.rb +1 -4
  93. data/lib/deepl/requests/translation_memory/create_export.rb +7 -2
  94. data/lib/deepl/requests/translation_memory/create_import.rb +11 -1
  95. data/lib/deepl/requests/translation_memory/destroy.rb +1 -1
  96. data/lib/deepl/requests/translation_memory/find.rb +1 -1
  97. data/lib/deepl/requests/translation_memory/find_job.rb +1 -1
  98. data/lib/deepl/requests/translation_memory/list.rb +4 -3
  99. data/lib/deepl/requests/translation_memory/segments.rb +5 -4
  100. data/lib/deepl/requests/translation_memory/storage_base.rb +37 -9
  101. data/lib/deepl/requests/translation_memory/upload_file.rb +1 -1
  102. data/lib/deepl/requests/usage.rb +28 -1
  103. data/lib/deepl/requests/write_improvements.rb +42 -0
  104. data/lib/deepl/resources/admin_usage_report.rb +73 -0
  105. data/lib/deepl/resources/custom_tag_usage_report.rb +59 -0
  106. data/lib/deepl/resources/developer_key.rb +57 -0
  107. data/lib/deepl/resources/document_handle.rb +8 -34
  108. data/lib/deepl/resources/document_translation_status.rb +14 -7
  109. data/lib/deepl/resources/language.rb +5 -3
  110. data/lib/deepl/resources/language_resource.rb +57 -0
  111. data/lib/deepl/resources/language_support.rb +56 -0
  112. data/lib/deepl/resources/multilingual_glossary.rb +112 -0
  113. data/lib/deepl/resources/spoken_terms.rb +61 -0
  114. data/lib/deepl/resources/style_profile.rb +44 -0
  115. data/lib/deepl/resources/style_rule.rb +4 -23
  116. data/lib/deepl/resources/text.rb +18 -2
  117. data/lib/deepl/resources/translation_memory.rb +8 -2
  118. data/lib/deepl/resources/translation_memory_import.rb +1 -1
  119. data/lib/deepl/resources/translation_memory_job.rb +26 -7
  120. data/lib/deepl/resources/translation_memory_segments.rb +8 -6
  121. data/lib/deepl/resources/usage.rb +61 -5
  122. data/lib/deepl/utils/client_builder.rb +89 -0
  123. data/lib/deepl/utils/document_poller.rb +70 -0
  124. data/lib/deepl/utils/exception_builder.rb +3 -2
  125. data/lib/deepl/utils/glossary_entries.rb +30 -0
  126. data/lib/deepl/utils/http_client_builder.rb +116 -0
  127. data/lib/deepl/utils/number_parser.rb +34 -0
  128. data/lib/deepl/utils/output_path.rb +25 -0
  129. data/lib/deepl/utils/term_validator.rb +33 -0
  130. data/lib/deepl/utils/time_parser.rb +14 -0
  131. data/lib/deepl/utils/translation_memory_operations.rb +230 -0
  132. data/lib/deepl.rb +68 -108
  133. data/lib/http_client_options.rb +21 -1
  134. data/lib/version.rb +1 -1
  135. data/upgrading_to_v4.md +374 -0
  136. metadata +84 -85
  137. data/.circleci/config.yml +0 -27
  138. data/.github/workflows/add_issues_to_kanban.yml +0 -16
  139. data/.gitlab-ci.yml +0 -179
  140. data/lib/deepl/document_api.rb +0 -131
  141. data/lib/deepl/exceptions/not_supported.rb +0 -14
  142. data/lib/deepl/glossary_api.rb +0 -38
  143. data/lib/deepl/style_rule_api.rb +0 -61
  144. data/lib/deepl/translation_memory_api.rb +0 -309
  145. data/license_checker.sh +0 -8
  146. data/spec/api/api_spec.rb +0 -20
  147. data/spec/api/configuration_spec.rb +0 -122
  148. data/spec/api/deepl_spec.rb +0 -262
  149. data/spec/constants/constants_spec.rb +0 -158
  150. data/spec/integration_tests/document_api_spec.rb +0 -211
  151. data/spec/integration_tests/document_error_paths_spec.rb +0 -33
  152. data/spec/integration_tests/glossary_api_spec.rb +0 -114
  153. data/spec/integration_tests/glossary_error_paths_spec.rb +0 -107
  154. data/spec/integration_tests/integration_test_utils.rb +0 -178
  155. data/spec/integration_tests/languages_api_spec.rb +0 -54
  156. data/spec/integration_tests/languages_error_paths_spec.rb +0 -25
  157. data/spec/integration_tests/rephrase_api_spec.rb +0 -90
  158. data/spec/integration_tests/rephrase_error_paths_spec.rb +0 -53
  159. data/spec/integration_tests/smoke_test_spec.rb +0 -24
  160. data/spec/integration_tests/style_rule_api_spec.rb +0 -117
  161. data/spec/integration_tests/style_rule_error_paths_spec.rb +0 -45
  162. data/spec/integration_tests/translate_api_spec.rb +0 -117
  163. data/spec/integration_tests/translate_error_paths_spec.rb +0 -48
  164. data/spec/integration_tests/translation_memory_api_spec.rb +0 -244
  165. data/spec/integration_tests/translation_memory_error_paths_spec.rb +0 -87
  166. data/spec/integration_tests/usage_api_spec.rb +0 -29
  167. data/spec/integration_tests/usage_error_paths_spec.rb +0 -18
  168. data/spec/requests/document/upload_spec.rb +0 -130
  169. data/spec/requests/extra_body_parameters_types_spec.rb +0 -82
  170. data/spec/requests/glossary/create_spec.rb +0 -44
  171. data/spec/requests/glossary/destroy_spec.rb +0 -27
  172. data/spec/requests/glossary/entries_spec.rb +0 -27
  173. data/spec/requests/glossary/find_spec.rb +0 -28
  174. data/spec/requests/glossary/language_pairs_spec.rb +0 -27
  175. data/spec/requests/glossary/list_spec.rb +0 -27
  176. data/spec/requests/languages_spec.rb +0 -27
  177. data/spec/requests/rephrase_spec.rb +0 -46
  178. data/spec/requests/style_rule/create_custom_instruction_spec.rb +0 -30
  179. data/spec/requests/style_rule/create_spec.rb +0 -29
  180. data/spec/requests/style_rule/destroy_custom_instruction_spec.rb +0 -28
  181. data/spec/requests/style_rule/destroy_spec.rb +0 -27
  182. data/spec/requests/style_rule/find_custom_instruction_spec.rb +0 -29
  183. data/spec/requests/style_rule/find_spec.rb +0 -28
  184. data/spec/requests/style_rule/list_spec.rb +0 -27
  185. data/spec/requests/style_rule/update_configured_rules_spec.rb +0 -31
  186. data/spec/requests/style_rule/update_custom_instruction_spec.rb +0 -32
  187. data/spec/requests/style_rule/update_spec.rb +0 -29
  188. data/spec/requests/translate_spec.rb +0 -338
  189. data/spec/requests/translation_memory/create_export_spec.rb +0 -37
  190. data/spec/requests/translation_memory/create_import_spec.rb +0 -61
  191. data/spec/requests/translation_memory/destroy_spec.rb +0 -36
  192. data/spec/requests/translation_memory/download_export_spec.rb +0 -54
  193. data/spec/requests/translation_memory/find_job_spec.rb +0 -37
  194. data/spec/requests/translation_memory/find_spec.rb +0 -36
  195. data/spec/requests/translation_memory/list_spec.rb +0 -27
  196. data/spec/requests/translation_memory/segments_spec.rb +0 -58
  197. data/spec/requests/translation_memory/upload_file_spec.rb +0 -55
  198. data/spec/requests/usage_spec.rb +0 -27
  199. data/spec/resources/custom_instruction_spec.rb +0 -32
  200. data/spec/resources/glossary_spec.rb +0 -38
  201. data/spec/resources/language_pair_spec.rb +0 -23
  202. data/spec/resources/language_spec.rb +0 -45
  203. data/spec/resources/style_rule_spec.rb +0 -68
  204. data/spec/resources/text_spec.rb +0 -23
  205. data/spec/resources/translation_memory_export_spec.rb +0 -38
  206. data/spec/resources/translation_memory_import_spec.rb +0 -30
  207. data/spec/resources/translation_memory_job_spec.rb +0 -109
  208. data/spec/resources/translation_memory_segments_spec.rb +0 -78
  209. data/spec/resources/translation_memory_spec.rb +0 -52
  210. data/spec/resources/usage_spec.rb +0 -35
  211. data/spec/spec_helper.rb +0 -62
  212. data/spec/support/live_mock_server.rb +0 -12
  213. data/spec/support/managed_glossary.rb +0 -65
  214. data/spec/support/managed_style_rule.rb +0 -17
  215. data/spec/support/managed_translation_memory.rb +0 -55
@@ -1,309 +0,0 @@
1
- # Copyright 2026 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- class TranslationMemoryApi # rubocop:disable Metrics/ClassLength
8
- # Time to wait between two status queries of an import or export job.
9
- JOB_POLLING_INTERVAL_SECONDS = 5
10
-
11
- def initialize(api, options = {})
12
- @api = api
13
- @options = options
14
- end
15
-
16
- ##
17
- # Lists the translation memories of the account.
18
- #
19
- # @param [Hash] options Additional options for the request. Supports `page` (page number for
20
- # pagination, 0-indexed) and `page_size` (number of items per page).
21
- # @return [Array<DeepL::Resources::TranslationMemory>] The translation memories of the page.
22
-
23
- def list(options = {})
24
- DeepL::Requests::TranslationMemory::List.new(@api, options).request
25
- end
26
-
27
- ##
28
- # Retrieves a single translation memory.
29
- #
30
- # @param [String, DeepL::Resources::TranslationMemory] translation_memory Translation memory
31
- # ID or object.
32
- # @param [Hash] options Additional options for the request.
33
- # @return [DeepL::Resources::TranslationMemory] The requested translation memory.
34
-
35
- def find(translation_memory, options = {})
36
- DeepL::Requests::TranslationMemory::Find.new(
37
- @api, extract_translation_memory_id(translation_memory), options
38
- ).request
39
- end
40
-
41
- ##
42
- # Retrieves one page of the segments of a translation memory. Pagination is cursor-based:
43
- # omit `page_cursor` on the first call, then pass the `next_page_cursor` of the previous
44
- # response until it is nil.
45
- #
46
- # @param [String, DeepL::Resources::TranslationMemory] translation_memory Translation memory
47
- # ID or object.
48
- # @param [Hash] options Additional options for the request. Supports `page_size` (maximum
49
- # number of segments per page, 1-100, defaults to 50), `page_cursor`
50
- # (cursor of a previous response), `filter_text` (substring filter
51
- # across source and target text, at least 2 characters) and
52
- # `filter_case_sensitive` (whether the filter is case-sensitive,
53
- # defaults to false).
54
- # @return [DeepL::Resources::TranslationMemorySegments] The requested page of segments.
55
-
56
- def segments(translation_memory, options = {})
57
- DeepL::Requests::TranslationMemory::Segments.new(
58
- @api, extract_translation_memory_id(translation_memory), options
59
- ).request
60
- end
61
-
62
- ##
63
- # Deletes a translation memory.
64
- #
65
- # @param [String, DeepL::Resources::TranslationMemory] translation_memory Translation memory
66
- # ID or object.
67
- # @param [Hash] options Additional options for the request.
68
- # @return [String] The ID of the deleted translation memory.
69
-
70
- def destroy(translation_memory, options = {})
71
- DeepL::Requests::TranslationMemory::Destroy.new(
72
- @api, extract_translation_memory_id(translation_memory), options
73
- ).request
74
- end
75
-
76
- ##
77
- # Creates an import job for a new translation memory. The job only declares the file, upload
78
- # the TMX file itself to the returned upload URL with `upload_file`, then poll `find_job` for
79
- # the outcome. Use `import_from_filepath` to do all three steps at once.
80
- #
81
- # @param [String] file_name Name of the TMX file to import, for example "legal.tmx".
82
- # @param [Integer] content_length Size of the TMX file in bytes.
83
- # @param [String, nil] content_type MIME type of the file, defaults to "application/xml".
84
- # @param [String, nil] display_name Name of the resulting translation memory, defaults to the
85
- # file name.
86
- # @param [Hash] additional_headers Additional HTTP headers for the request.
87
- # @return [DeepL::Resources::TranslationMemoryImport] The job ID and the upload URL.
88
-
89
- def create_import(file_name, content_length, content_type: nil, display_name: nil,
90
- additional_headers: {})
91
- DeepL::Requests::TranslationMemory::CreateImport.new(
92
- @api, file_name, content_length,
93
- { content_type: content_type, display_name: display_name }.compact, additional_headers
94
- ).request
95
- end
96
-
97
- ##
98
- # Uploads a TMX file to the upload URL of an import job, which starts the processing. The
99
- # upload URL is a pre-signed storage URL outside of the DeepL API, so no authorization header
100
- # is sent with this request.
101
- #
102
- # @param [String, DeepL::Resources::TranslationMemoryImport] translation_memory_import Import
103
- # returned by `create_import`, or its upload URL.
104
- # @param [String] file_content Content of the TMX file.
105
- # @param [String] content_type MIME type of the file. Must match the `content_type` declared
106
- # when the import job was created.
107
- # @return [nil]
108
-
109
- def upload_file(translation_memory_import, file_content,
110
- content_type: Requests::TranslationMemory::UploadFile::DEFAULT_CONTENT_TYPE)
111
- DeepL::Requests::TranslationMemory::UploadFile.new(
112
- @api, extract_upload_url(translation_memory_import), file_content, content_type
113
- ).request
114
- end
115
-
116
- ##
117
- # Creates an export job for a translation memory. Poll `find_job` for the download URL of the
118
- # exported TMX file. Use `export_to_filepath` to do both steps and write the file at once.
119
- #
120
- # @param [String, DeepL::Resources::TranslationMemory] translation_memory Translation memory
121
- # ID or object.
122
- # @param [Hash] options Additional options for the request.
123
- # @return [DeepL::Resources::TranslationMemoryExport] The job ID, and whether the API reused a
124
- # previously completed export.
125
-
126
- def create_export(translation_memory, options = {})
127
- DeepL::Requests::TranslationMemory::CreateExport.new(
128
- @api, extract_translation_memory_id(translation_memory), options
129
- ).request
130
- end
131
-
132
- ##
133
- # Retrieves the status of a translation memory import or export job.
134
- #
135
- # @param [String, DeepL::Resources::TranslationMemoryJob] job Job ID or object.
136
- # @param [Hash] options Additional options for the request.
137
- # @return [DeepL::Resources::TranslationMemoryJob] The current status of the job.
138
-
139
- def find_job(job, options = {})
140
- DeepL::Requests::TranslationMemory::FindJob.new(@api, extract_job_id(job), options).request
141
- end
142
-
143
- ##
144
- # Polls a translation memory import or export job until it is finished, `sleep`ing between
145
- # the status queries, and returns the final status.
146
- #
147
- # Note that an import job keeps reporting `awaiting_input` for a while after its file has
148
- # been uploaded, because the API detects the upload asynchronously. That status is therefore
149
- # polled through like any other non-terminal one. A job whose file is never uploaded does not
150
- # finish on its own, so pass `timeout_s` when that is a possibility.
151
- #
152
- # @raise [DeepL::Exceptions::Error] If the job failed or expired, or if `timeout_s` elapsed
153
- # before the job finished.
154
- #
155
- # @param [String, DeepL::Resources::TranslationMemoryJob] job Job ID or object.
156
- # @param [Hash] options Additional options for the status queries.
157
- # @param [Numeric, nil] timeout_s Maximum time in seconds to wait for the job to finish. Note
158
- # that this is not accurate to the second, the status is only
159
- # queried every five seconds.
160
- # @return [DeepL::Resources::TranslationMemoryJob] The finished job.
161
-
162
- def wait_until_job_done(job, options = {}, timeout_s: nil)
163
- job_status = find_job(job, options)
164
- started_at = monotonic_time
165
- until job_status.finished?
166
- raise_timeout_error(timeout_s) if timeout_exceeded?(started_at, timeout_s)
167
-
168
- log_job_polling
169
- sleep(JOB_POLLING_INTERVAL_SECONDS)
170
- job_status = find_job(job, options)
171
- end
172
- raise_job_error(job_status) if job_status.error?
173
-
174
- job_status
175
- end
176
-
177
- ##
178
- # Downloads the TMX file of a completed export job. The download URL is a pre-signed storage
179
- # URL outside of the DeepL API, so no authorization header is sent with this request.
180
- #
181
- # @raise [DeepL::Exceptions::Error] If the job carries no download URL, for example because
182
- # it has not completed yet.
183
- #
184
- # @param [DeepL::Resources::TranslationMemoryJob, String] job Completed export job carrying
185
- # the download URL, or the download URL itself.
186
- # @param [String] output_path Path to the file to write to. Will be overwritten if the file
187
- # already exists.
188
-
189
- def download_export(job, output_path)
190
- DeepL::Requests::TranslationMemory::DownloadExport.new(@api, extract_download_url(job),
191
- output_path).request
192
- end
193
-
194
- ##
195
- # Imports a TMX file as a new translation memory: creates the import job, uploads the file
196
- # and waits for the processing to finish.
197
- #
198
- # @raise [DeepL::Exceptions::Error] If the import fails.
199
- #
200
- # @param [String] input_file_path Path to the TMX file to import.
201
- # @param [String, nil] display_name Name of the resulting translation memory, defaults to the
202
- # file name.
203
- # @param [Numeric, nil] timeout_s Maximum time in seconds to wait for the import to finish.
204
- # Note that the API keeps reporting `awaiting_input` for a
205
- # while after the upload, so allow for a generous timeout.
206
- # @return [DeepL::Resources::TranslationMemoryJob] The finished import job, its result carries
207
- # the ID of the new translation memory.
208
-
209
- def import_from_filepath(input_file_path, display_name: nil, timeout_s: nil)
210
- unless File.exist?(input_file_path)
211
- raise Exceptions::Error, "No file found at #{input_file_path}"
212
- end
213
-
214
- file_content = File.binread(input_file_path)
215
- created = create_import(File.basename(input_file_path), file_content.bytesize,
216
- display_name: display_name)
217
- upload_file(created, file_content)
218
- wait_until_job_done(created.job_id, timeout_s: timeout_s)
219
- end
220
-
221
- ##
222
- # Exports a translation memory to a TMX file: creates the export job, waits for it to finish
223
- # and writes the result to +output_path+.
224
- #
225
- # @raise [DeepL::Exceptions::Error] If the export fails.
226
- #
227
- # @param [String, DeepL::Resources::TranslationMemory] translation_memory Translation memory
228
- # ID or object.
229
- # @param [String] output_path Path to the file to write to. Will be overwritten if the file
230
- # already exists.
231
- # @param [Numeric, nil] timeout_s Maximum time in seconds to wait for the export to finish.
232
- # @return [DeepL::Resources::TranslationMemoryJob] The finished export job.
233
-
234
- def export_to_filepath(translation_memory, output_path, timeout_s: nil)
235
- created = create_export(translation_memory)
236
- job = wait_until_job_done(created.job_id, timeout_s: timeout_s)
237
- download_export(job, output_path)
238
- job
239
- end
240
-
241
- private
242
-
243
- def extract_translation_memory_id(translation_memory)
244
- id = if translation_memory.is_a?(Resources::TranslationMemory)
245
- translation_memory.translation_memory_id
246
- else
247
- translation_memory
248
- end
249
- raise Exceptions::Error, 'Translation memory ID must not be empty' if blank?(id)
250
-
251
- id
252
- end
253
-
254
- def extract_job_id(job)
255
- id = job.is_a?(Resources::TranslationMemoryJob) ? job.job_id : job
256
- raise Exceptions::Error, 'Job ID must not be empty' if blank?(id)
257
-
258
- id
259
- end
260
-
261
- def extract_upload_url(translation_memory_import)
262
- if translation_memory_import.is_a?(Resources::TranslationMemoryImport)
263
- translation_memory_import.upload_url
264
- else
265
- translation_memory_import
266
- end
267
- end
268
-
269
- def extract_download_url(job)
270
- return job unless job.is_a?(Resources::TranslationMemoryJob)
271
-
272
- download_url = job.result&.download_url
273
- if blank?(download_url)
274
- raise Exceptions::Error, 'Translation memory export job has no download URL, ' \
275
- 'it may not have completed yet'
276
- end
277
-
278
- download_url
279
- end
280
-
281
- def blank?(value)
282
- value.nil? || value.empty?
283
- end
284
-
285
- def log_job_polling
286
- @api.configuration.logger&.info('Rechecking translation memory job status after sleeping ' \
287
- "for #{JOB_POLLING_INTERVAL_SECONDS} seconds.")
288
- end
289
-
290
- def monotonic_time
291
- Process.clock_gettime(Process::CLOCK_MONOTONIC)
292
- end
293
-
294
- def timeout_exceeded?(started_at, timeout_s)
295
- !timeout_s.nil? && (monotonic_time - started_at) > timeout_s
296
- end
297
-
298
- def raise_timeout_error(timeout_s)
299
- raise Exceptions::Error,
300
- "Manual timeout of #{timeout_s}s exceeded for the translation memory job"
301
- end
302
-
303
- def raise_job_error(job_status)
304
- raise Exceptions::Error,
305
- "Error occurred during the translation memory #{job_status.operation}: " \
306
- "#{job_status.error_message || job_status.status}"
307
- end
308
- end
309
- end
data/license_checker.sh DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- # Usage: ./license_checker.sh source_code_pattern
4
- # Example: ./license_checker.sh '*.rb'
5
- # This will search all .rb files, ignoring anything not tracked in your git tree
6
-
7
- git ls-files -z $1 | xargs -0 -n 10 -I{} sh -c 'RES=$(head -n 3 "{}" | grep "Copyright 20[0-9][0-9] DeepL SE (https://www.deepl.com)\|Copyright 20[0-9][0-9] Daniel Herzog"); if [ ! "${RES}" ] ; then echo "Lacking copyright header in" "{}" ; fi'
8
-
data/spec/api/api_spec.rb DELETED
@@ -1,20 +0,0 @@
1
- # Copyright 2018 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- require 'spec_helper'
7
-
8
- describe DeepL::API do
9
- subject(:api) { described_class.new(configuration) }
10
-
11
- let(:configuration) { DeepL::Configuration.new }
12
-
13
- describe '#initialize' do
14
- context 'when building an API object' do
15
- it 'saves the configuration' do
16
- expect(api.configuration).to be(configuration)
17
- end
18
- end
19
- end
20
- end
@@ -1,122 +0,0 @@
1
- # Copyright 2018 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- require 'logger'
7
- require 'spec_helper'
8
-
9
- describe DeepL::Configuration do
10
- subject(:config) { described_class.new(attributes) }
11
-
12
- let(:attributes) { {} }
13
-
14
- around do |tests|
15
- tmp_env = ENV.to_hash
16
- ENV.clear
17
- tests.call
18
- ENV.replace(tmp_env)
19
- end
20
-
21
- describe '#initialize' do
22
- context 'when using default configuration attributes' do
23
- it 'uses default attributes' do
24
- expect(config.auth_key).to eq(ENV.fetch('DEEPL_AUTH_KEY', nil))
25
- expect(config.host).to eq('https://api.deepl.com')
26
- expect(config.max_doc_status_queries).to be_nil
27
- expect(config.version).to eq('v2')
28
- end
29
-
30
- it 'sends platform information' do
31
- expect(config.user_agent).to include('deepl-ruby/')
32
- expect(config.user_agent).to include('(')
33
- expect(config.user_agent).to include(')')
34
- expect(config.user_agent).to include(' ruby/')
35
- end
36
- end
37
-
38
- context 'when using custom configuration attributes' do
39
- let(:logger) { Logger.new($stdout) }
40
- let(:attributes) { { auth_key: 'SAMPLE', host: 'https://api-free.deepl.com', logger: logger, max_doc_status_queries: 42, max_network_retries: 43, version: 'v1' } }
41
-
42
- it 'uses custom attributes' do
43
- expect(config.auth_key).to eq(attributes[:auth_key])
44
- expect(config.host).to eq(attributes[:host])
45
- expect(config.max_doc_status_queries).to eq(attributes[:max_doc_status_queries])
46
- expect(config.max_network_retries).to eq(attributes[:max_network_retries])
47
- expect(config.version).to eq(attributes[:version])
48
- end
49
- end
50
-
51
- context 'when using a free key' do
52
- let(:attributes) { { auth_key: '123e4567-e89b-12d3-a456-426614174000:fx' } }
53
-
54
- it 'uses the free API URL' do
55
- expect(config.host).to eq('https://api-free.deepl.com')
56
- end
57
- end
58
-
59
- context 'when using a pro key' do
60
- let(:attributes) { { auth_key: '123e4567-e89b-12d3-a456-426614174000' } }
61
-
62
- it 'uses the pro API URL' do
63
- expect(config.host).to eq('https://api.deepl.com')
64
- end
65
- end
66
-
67
- context 'when opting out of sending platform info' do
68
- subject(:config) { described_class.new(attributes, nil, nil, false) }
69
-
70
- it 'does not send platform information' do
71
- expect(config.user_agent).to include('deepl-ruby/')
72
- expect(config.user_agent).not_to include('(')
73
- expect(config.user_agent).not_to include(')')
74
- expect(config.user_agent).not_to include(' ruby/')
75
- end
76
- end
77
-
78
- context 'when registering an app and sending platform info' do
79
- subject(:config) { described_class.new(attributes, 'MyTestApp', '0.1.3', true) }
80
-
81
- it 'sends platform and app info' do
82
- expect(config.user_agent).to include('deepl-ruby/')
83
- expect(config.user_agent).to include('(')
84
- expect(config.user_agent).to include(')')
85
- expect(config.user_agent).to include(' ruby/')
86
- expect(config.user_agent).to include('MyTestApp/0.1.3')
87
- end
88
- end
89
-
90
- context 'when registering an app and not sending platform info' do
91
- subject(:config) { described_class.new(attributes, 'MyTestApp', '0.1.3', false) }
92
-
93
- it 'sends app, but not platform info' do
94
- expect(config.user_agent).to include('deepl-ruby/')
95
- expect(config.user_agent).not_to include('(')
96
- expect(config.user_agent).not_to include(')')
97
- expect(config.user_agent).not_to include(' ruby/')
98
- expect(config.user_agent).to include('MyTestApp/0.1.3')
99
- end
100
- end
101
- end
102
-
103
- describe '#validate!' do
104
- let(:auth_message) { 'auth_key not provided' }
105
-
106
- context 'when providing a valid auth key' do
107
- let(:attributes) { { auth_key: '' } }
108
-
109
- it 'raises an error' do
110
- expect { config.validate! }.to raise_error(DeepL::Exceptions::Error, auth_message)
111
- end
112
- end
113
-
114
- context 'when providing an invalid auth key' do
115
- let(:attributes) { { auth_key: 'not-empty' } }
116
-
117
- it 'does not raise an error' do
118
- expect { config.validate! }.not_to raise_error
119
- end
120
- end
121
- end
122
- end