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,117 +0,0 @@
1
- # Copyright 2025 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE file.
4
- # frozen_string_literal: true
5
-
6
- require 'spec_helper'
7
-
8
- describe DeepL::StyleRuleApi, :mock_server_only do
9
- describe '#translate_with_style_rules' do
10
- it 'when performing a request with style_id' do
11
- source_lang = 'DE'
12
- target_lang = 'EN-US'
13
- text = 'Hallo, Welt!'
14
- style_id = 'dca2e053-8ae5-45e6-a0d2-881156e7f4e4'
15
-
16
- result = DeepL.translate(text, source_lang, target_lang, { style_rule: style_id })
17
- expect(result).to be_a(DeepL::Resources::Text)
18
- end
19
-
20
- it 'when performing a request with style_rule object' do
21
- source_lang = 'DE'
22
- target_lang = 'EN-US'
23
- text = 'Hallo, Welt!'
24
- style_rule = build_test_style_rule
25
-
26
- result = DeepL.translate(text, source_lang, target_lang, { style_rule: style_rule })
27
- expect(result).to be_a(DeepL::Resources::Text)
28
- end
29
- end
30
-
31
- describe '#list_style_rules' do
32
- it 'when requesting a list of all style rules' do
33
- style_rules = DeepL.style_rules.list(page: 0, page_size: 10, detailed: true)
34
- expect(style_rules).to be_an(Array)
35
- expect(style_rules.length).to eq(1)
36
- expect(style_rules[0].style_id).to eq('dca2e053-8ae5-45e6-a0d2-881156e7f4e4')
37
- expect(style_rules[0].name).to eq('Default Style Rule')
38
- expect(style_rules[0].configured_rules).to be_a(DeepL::Resources::ConfiguredRules)
39
- expect(style_rules[0].custom_instructions).to be_an(Array)
40
- end
41
-
42
- it 'when requesting a list of all style rules without detailed' do
43
- style_rules = DeepL.style_rules.list
44
- expect(style_rules).to be_an(Array)
45
- expect(style_rules.length).to eq(1)
46
- expect(style_rules[0].style_id).to eq('dca2e053-8ae5-45e6-a0d2-881156e7f4e4')
47
- expect(style_rules[0].configured_rules).to be_nil
48
- expect(style_rules[0].custom_instructions).to be_nil
49
- end
50
- end
51
-
52
- describe 'style rule management operations' do
53
- it 'when performing all management operations on style rules' do # rubocop:disable RSpec/ExampleLength,RSpec/MultipleExpectations
54
- # Create a style rule
55
- style_rule = DeepL.style_rules.create('Test Style Rule', 'en')
56
- expect(style_rule).to be_a(DeepL::Resources::StyleRule)
57
- expect(style_rule.style_id).not_to be_nil
58
- style_id = style_rule.style_id
59
-
60
- # Find the style rule
61
- found_rule = DeepL.style_rules.find(style_id)
62
- expect(found_rule).to be_a(DeepL::Resources::StyleRule)
63
- expect(found_rule.style_id).to eq(style_id)
64
-
65
- # Update the style rule name
66
- updated_rule = DeepL.style_rules.update_name(style_id, 'Updated Style Rule')
67
- expect(updated_rule).to be_a(DeepL::Resources::StyleRule)
68
-
69
- # Update configured rules
70
- configured_rules = {
71
- 'dates_and_times' => { 'calendar_era' => 'use_bc_and_ad' }
72
- }
73
- updated_rule = DeepL.style_rules.update_configured_rules(style_id, configured_rules)
74
- expect(updated_rule).to be_a(DeepL::Resources::StyleRule)
75
-
76
- # Create a custom instruction
77
- custom_instruction = DeepL.style_rules.create_custom_instruction(
78
- style_id, 'Test Instruction', 'Always use formal language'
79
- )
80
- expect(custom_instruction).to be_a(DeepL::Resources::CustomInstruction)
81
- expect(custom_instruction.id).not_to be_nil
82
- instruction_id = custom_instruction.id
83
-
84
- # Find a custom instruction
85
- fetched_instruction = DeepL.style_rules.find_custom_instruction(style_id, instruction_id)
86
- expect(fetched_instruction).to be_a(DeepL::Resources::CustomInstruction)
87
- expect(fetched_instruction.id).to eq(instruction_id)
88
-
89
- # Update a custom instruction
90
- updated_instruction = DeepL.style_rules.update_custom_instruction(
91
- style_id, instruction_id, 'Updated Instruction', 'Use casual language'
92
- )
93
- expect(updated_instruction).to be_a(DeepL::Resources::CustomInstruction)
94
-
95
- # Destroy the custom instruction
96
- deleted_instruction_id = DeepL.style_rules.destroy_custom_instruction(style_id,
97
- instruction_id)
98
- expect(deleted_instruction_id).to eq(instruction_id)
99
-
100
- # Destroy the style rule
101
- deleted_style_id = DeepL.style_rules.destroy(style_id)
102
- expect(deleted_style_id).to eq(style_id)
103
- end
104
- end
105
-
106
- def build_test_style_rule
107
- style_rule_data = {
108
- 'style_id' => 'dca2e053-8ae5-45e6-a0d2-881156e7f4e4',
109
- 'name' => 'Default Style Rule',
110
- 'creation_time' => '2025-01-01T00:00:00Z',
111
- 'updated_time' => '2025-01-01T00:00:00Z',
112
- 'language' => 'en',
113
- 'version' => 1
114
- }
115
- DeepL::Resources::StyleRule.new(style_rule_data, nil, nil)
116
- end
117
- end
@@ -1,45 +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
- require 'spec_helper'
7
-
8
- describe DeepL::StyleRuleApi, :mock_server_only do # rubocop:disable RSpec/SpecFilePathFormat
9
- include_context 'with a live mock server'
10
-
11
- let(:missing_uuid) { '00000000-0000-0000-0000-000000000000' }
12
-
13
- describe 'authorization failures' do
14
- let(:unauthorized_style_rules) { described_class.new(unauthorized_api) }
15
-
16
- it 'raises AuthorizationFailed on a read operation (list)' do
17
- expect { unauthorized_style_rules.list }
18
- .to raise_error(DeepL::Exceptions::AuthorizationFailed)
19
- end
20
-
21
- it 'raises AuthorizationFailed on a write operation (create)' do
22
- expect { unauthorized_style_rules.create('Auth Failure Test', 'en') }
23
- .to raise_error(DeepL::Exceptions::AuthorizationFailed)
24
- end
25
- end
26
-
27
- describe 'not-found failures' do
28
- it 'raises NotFound when #find is called with a missing UUID' do
29
- expect { DeepL.style_rules.find(missing_uuid) }
30
- .to raise_error(DeepL::Exceptions::NotFound)
31
- end
32
- end
33
-
34
- describe 'bad-request failures' do
35
- it 'raises BadRequest when create is called with a nil language' do
36
- expect { DeepL.style_rules.create('Nil Lang', nil) }
37
- .to raise_error(DeepL::Exceptions::BadRequest)
38
- end
39
-
40
- it 'raises an error when #destroy is called with a malformed UUID' do
41
- expect { DeepL.style_rules.destroy('invalid-uuid') }
42
- .to raise_error(DeepL::Exceptions::Error)
43
- end
44
- end
45
- end
@@ -1,117 +0,0 @@
1
- # Copyright 2026 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE file.
4
- # frozen_string_literal: true
5
-
6
- require 'spec_helper'
7
-
8
- describe 'DeepL.translate' do # rubocop:disable RSpec/DescribeClass
9
- include_context 'with a live mock server'
10
-
11
- let(:text) { 'proton beam' }
12
-
13
- describe 'happy path' do
14
- it 'translates a single string into a Text resource' do
15
- result = DeepL.translate(text, 'EN', 'DE')
16
-
17
- expect(result).to be_a(DeepL::Resources::Text)
18
- expect(result.text).to be_a(String)
19
- expect(result.text).not_to be_empty
20
- end
21
-
22
- it 'translates an array of strings into an array of Text resources' do
23
- results = DeepL.translate(%w[Sample Word], 'EN', 'ES')
24
-
25
- expect(results).to be_an(Array)
26
- expect(results.size).to eq(2)
27
- expect(results).to all(be_a(DeepL::Resources::Text))
28
- end
29
- end
30
-
31
- describe 'options' do
32
- it 'accepts a formality setting' do
33
- result = DeepL.translate('How are you?', 'EN', 'DE', { formality: 'more' })
34
-
35
- expect(result).to be_a(DeepL::Resources::Text)
36
- end
37
-
38
- it 'translates with XML tag handling and ignored tags' do
39
- result = DeepL.translate('<p>Sample <code>x</code></p>', 'EN', 'ES',
40
- { tag_handling: 'xml', ignore_tags: %w[code] })
41
-
42
- expect(result.text).to be_a(String)
43
- expect(result.text).not_to be_empty
44
- end
45
-
46
- it 'accepts a context option' do
47
- result = DeepL.translate('That is hot!', 'EN', 'ES', { context: 'about the weather' })
48
-
49
- expect(result).to be_a(DeepL::Resources::Text)
50
- end
51
-
52
- it 'accepts an empty context option' do
53
- result = DeepL.translate('That is hot!', 'EN', 'ES', { context: '' })
54
-
55
- expect(result).to be_a(DeepL::Resources::Text)
56
- end
57
-
58
- it 'translates an HTML document with split_sentences, outline_detection and splitting_tags' do
59
- document = '<document><meta><title>Title</title></meta>' \
60
- '<content><par>First sentence. Second one.</par></content></document>'
61
- result = DeepL.translate(document, 'EN', 'ES',
62
- { tag_handling: 'xml', split_sentences: 'nonewlines',
63
- outline_detection: false, splitting_tags: %w[title par] })
64
-
65
- expect(result).to be_a(DeepL::Resources::Text)
66
- expect(result.text).not_to be_empty
67
- end
68
-
69
- it 'translates using a glossary' do
70
- with_managed_glossary(name: 'Translate Glossary', source_lang: 'en', target_lang: 'de',
71
- entries: [%w[Hello Hallo]]) do |glossary|
72
- result = DeepL.translate('Hello', 'EN', 'DE', { glossary_id: glossary.id })
73
-
74
- expect(result).to be_a(DeepL::Resources::Text)
75
- expect(result.text).not_to be_empty
76
- end
77
- end
78
-
79
- it 'translates using multiple glossaries via glossary_ids' do # rubocop:disable RSpec/ExampleLength
80
- with_managed_glossary(name: 'Translate Glossary IDs 1', source_lang: 'en', target_lang: 'de',
81
- entries: [%w[Hello Hallo]]) do |glossary1|
82
- with_managed_glossary(name: 'Translate Glossary IDs 2', source_lang: 'en',
83
- target_lang: 'de', entries: [%w[World Welt]]) do |glossary2|
84
- # Newly created glossaries can lag behind (eventual consistency), so
85
- # ensure they are resolvable before translating and tolerate a
86
- # transient "not found" on the translate call itself.
87
- wait_until_glossaries_ready(glossary1, glossary2)
88
- result = translate_retrying_missing_glossary(
89
- 'Hello', 'EN', 'DE', { glossary_ids: [glossary1.id, glossary2.id] }
90
- )
91
-
92
- expect(result).to be_a(DeepL::Resources::Text)
93
- expect(result.text).not_to be_empty
94
- end
95
- end
96
- end
97
-
98
- %w[quality_optimized latency_optimized prefer_quality_optimized].each do |model_type|
99
- it "returns model_type_used for #{model_type}" do
100
- result = DeepL.translate(text, 'EN', 'DE', { model_type: model_type })
101
-
102
- expect(result).to be_a(DeepL::Resources::Text)
103
- expect(result.model_type_used).not_to be_nil
104
- end
105
- end
106
-
107
- %w[v1 v2].each do |version|
108
- it "translates with tag_handling_version #{version}" do
109
- result = DeepL.translate('<p>Hello world</p>', 'EN', 'DE',
110
- { tag_handling: 'html', tag_handling_version: version })
111
-
112
- expect(result).to be_a(DeepL::Resources::Text)
113
- expect(result.text).not_to be_empty
114
- end
115
- end
116
- end
117
- end
@@ -1,48 +0,0 @@
1
- # Copyright 2026 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE file.
4
- # frozen_string_literal: true
5
-
6
- require 'spec_helper'
7
-
8
- describe 'DeepL.translate error paths' do # rubocop:disable RSpec/DescribeClass
9
- include_context 'with a live mock server'
10
-
11
- let(:text) { 'proton beam' }
12
-
13
- describe 'authorization failures' do
14
- it 'raises AuthorizationFailed when the auth key is invalid' do
15
- request = DeepL::Requests::Translate.new(unauthorized_api, text, 'EN', 'DE')
16
-
17
- expect { request.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
18
- end
19
- end
20
-
21
- describe 'bad request errors' do
22
- it 'raises BadRequest for an unsupported target language' do
23
- expect { DeepL.translate(text, 'EN', 'ZZ') }
24
- .to raise_error(DeepL::Exceptions::BadRequest)
25
- end
26
-
27
- it 'raises BadRequest when no target language is given' do
28
- expect { DeepL.translate(text, 'EN', nil) }
29
- .to raise_error(DeepL::Exceptions::BadRequest)
30
- end
31
-
32
- # Real-server only: the mock coerces text:[null] to text:[""] and accepts it,
33
- # so it cannot reproduce this; prod rejects [null] with 400.
34
- it 'raises BadRequest when the text is nil', :real_server_only do
35
- expect { DeepL.translate(nil, 'EN', 'DE') }
36
- .to raise_error(DeepL::Exceptions::BadRequest)
37
- end
38
- end
39
-
40
- describe 'request entity too large errors' do
41
- it 'raises RequestEntityTooLarge when too many texts are sent in a single request' do
42
- texts = Array.new(10_000) { |i| "This is the sentence number #{i}" }
43
-
44
- expect { DeepL.translate(texts, 'EN', 'DE') }
45
- .to raise_error(DeepL::Exceptions::RequestEntityTooLarge)
46
- end
47
- end
48
- end
@@ -1,244 +0,0 @@
1
- # Copyright 2026 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE file.
4
- # frozen_string_literal: true
5
-
6
- require 'spec_helper'
7
-
8
- describe DeepL::TranslationMemoryApi, :mock_server_only do
9
- include_context 'with temp dir'
10
-
11
- let(:default_tm_id) { 'a74d88fb-ed2a-4943-a664-a4512398b994' }
12
- let(:tmx_file_path) { write_example_tmx(File.join(temp_dir, 'example.tmx')) }
13
- let(:translation_memories) { DeepL.translation_memories }
14
-
15
- describe '#translate_with_translation_memory' do
16
- it 'when performing a request with translation_memory_id' do
17
- source_lang = 'DE'
18
- target_lang = 'EN'
19
- text = 'Protonenstrahl'
20
-
21
- result = DeepL.translate(text, source_lang, target_lang,
22
- { translation_memory: default_tm_id })
23
- expect(result).to be_a(DeepL::Resources::Text)
24
- end
25
-
26
- it 'when performing a request with translation_memory resource object' do
27
- source_lang = 'DE'
28
- target_lang = 'EN'
29
- text = 'Protonenstrahl'
30
- translation_memory = build_test_translation_memory
31
-
32
- result = DeepL.translate(text, source_lang, target_lang,
33
- { translation_memory: translation_memory })
34
- expect(result).to be_a(DeepL::Resources::Text)
35
- end
36
- end
37
-
38
- describe '#list_translation_memories' do
39
- it 'when requesting a list of all translation memories' do
40
- translation_memories = DeepL.translation_memories.list
41
- expect(translation_memories).to be_an(Array)
42
- expect(translation_memories).not_to be_empty
43
- expect(translation_memories.first).to be_a(DeepL::Resources::TranslationMemory)
44
- expect(translation_memories.first.translation_memory_id).not_to be_nil
45
- expect(translation_memories.first.name).not_to be_nil
46
- end
47
- end
48
-
49
- describe '#find' do
50
- it 'when requesting a single translation memory by ID' do
51
- translation_memory = DeepL.translation_memories.find(default_tm_id)
52
- expect(translation_memory).to be_a(DeepL::Resources::TranslationMemory)
53
- expect(translation_memory.translation_memory_id).to eq(default_tm_id)
54
- expect(translation_memory.name).to eq('Default Translation Memory')
55
- expect(translation_memory.source_language).to eq('de')
56
- expect(translation_memory.target_languages).to eq(%w[en es fr])
57
- expect(translation_memory.segment_count).to eq(12)
58
- end
59
-
60
- it 'when requesting a single translation memory by resource object' do
61
- translation_memory = DeepL.translation_memories.find(build_test_translation_memory)
62
- expect(translation_memory.translation_memory_id).to eq(default_tm_id)
63
- expect(translation_memory.creation_time).to be_a(Time)
64
- expect(translation_memory.updated_time).to be_a(Time)
65
- end
66
- end
67
-
68
- describe '#segments' do
69
- it 'when requesting the segments of a translation memory' do
70
- page = DeepL.translation_memories.segments(default_tm_id)
71
- expect(page).to be_a(DeepL::Resources::TranslationMemorySegments)
72
- expect(page.segments.length).to eq(12)
73
- expect(page.segment_count).to eq(12)
74
- expect(page.next_page?).to be(false)
75
- expect(page.segments.first.source_text).to eq('Quelltext Nummer 0')
76
- end
77
-
78
- it 'when reading segment timestamps' do
79
- # The API does not return creation_time/updated_time on segments at all, and returns
80
- # last_used_time only for segments that have been used.
81
- page = DeepL.translation_memories.segments(default_tm_id)
82
- expect(page.segments.first.creation_time).to be_nil
83
- expect(page.segments.first.updated_time).to be_nil
84
- expect(page.segments.first.last_used_time).to be_a(Time)
85
- expect(page.segments[1].last_used_time).to be_nil
86
- end
87
-
88
- it 'when reading the target segments of a source segment' do
89
- page = DeepL.translation_memories.segments(default_tm_id, page_size: 1)
90
- segment = page.segments.first
91
- expect(segment.targets.map(&:target_language)).to eq(%w[en es fr])
92
- expect(segment.targets.first.target_text).to eq('Target text number 0 (de->en)')
93
- expect(segment.targets.first.target_segment_id).not_to be_nil
94
- expect(segment.targets.first.last_used_time).to be_a(Time)
95
- end
96
-
97
- it 'when paginating through the segments with the page cursor' do
98
- first_page = DeepL.translation_memories.segments(default_tm_id, page_size: 5)
99
- second_page = next_segments_page(first_page)
100
- last_page = next_segments_page(second_page)
101
-
102
- expect(first_page.segments.length).to eq(5)
103
- expect(first_page.next_page?).to be(true)
104
- expect(second_page.segments.first.source_text).to eq('Quelltext Nummer 5')
105
- expect(last_page.segments.length).to eq(2)
106
- expect(last_page.next_page?).to be(false)
107
- end
108
-
109
- it 'when filtering the segments by a text containing a space' do
110
- page = DeepL.translation_memories.segments(default_tm_id, filter_text: 'Nummer 1')
111
- expect(page.segments.map(&:source_text))
112
- .to eq(['Quelltext Nummer 1', 'Quelltext Nummer 10', 'Quelltext Nummer 11'])
113
- # segment_count is the number of segments stored in the translation memory, a text filter
114
- # does not reduce it.
115
- expect(page.segment_count).to eq(12)
116
- end
117
-
118
- it 'when filtering the segments case-sensitively' do
119
- page = DeepL.translation_memories.segments(default_tm_id, filter_text: 'quelltext',
120
- filter_case_sensitive: true)
121
- expect(page.segments).to be_empty
122
- expect(page.segment_count).to eq(12)
123
- end
124
- end
125
-
126
- describe '#import_from_filepath' do
127
- it 'when importing a translation memory from a TMX file' do
128
- with_managed_translation_memory(tmx_file_path, display_name: 'Imported TM') do |job|
129
- expect(job).to be_a(DeepL::Resources::TranslationMemoryJob)
130
- expect(job.operation).to eq('import')
131
- expect(job.display_name).to eq('Imported TM')
132
- expect(job.status).to eq('completed')
133
- expect(job.finished?).to be(true)
134
- expect(job.result.translation_memory_id).not_to be_nil
135
- expect(job.result.skipped_segment_count).to eq(0)
136
- end
137
- end
138
-
139
- it 'when the imported translation memory can be retrieved afterwards' do
140
- with_managed_translation_memory(tmx_file_path) do |job|
141
- translation_memory = DeepL.translation_memories.find(job.result.translation_memory_id)
142
- expect(translation_memory.name).to eq('example.tmx')
143
- expect(translation_memory.source_language).to eq('de')
144
- expect(job.source_content_type).to eq('application/xml')
145
- expect(job.source_content_length).to eq(File.size(tmx_file_path))
146
- end
147
- end
148
- end
149
-
150
- describe '#create_import' do
151
- it 'when the import job is still awaiting the file upload' do
152
- created = DeepL.translation_memories.create_import('awaiting.tmx', 128,
153
- display_name: 'Awaiting Upload')
154
- expect(created).to be_a(DeepL::Resources::TranslationMemoryImport)
155
- expect(created.upload_url).to start_with('http')
156
- expect(created.expires_at).to be_a(Time)
157
-
158
- job = DeepL.translation_memories.find_job(created.job_id)
159
- expect(job.awaiting_input?).to be(true)
160
- expect(job.status).to eq('awaiting_input')
161
- expect(job.result.required_action).not_to be_nil
162
- end
163
-
164
- # The API detects the upload asynchronously, so an uploaded job keeps reporting
165
- # `awaiting_input` for a while. The wait loop must poll through that status.
166
- it 'when waiting on an uploaded job it polls through the awaiting_input status' do
167
- created = create_uploaded_import(translation_memories, tmx_file_path, processing_polls: 1)
168
- polled_statuses = []
169
- allow(translation_memories).to receive(:find_job).and_wrap_original do |original, *args|
170
- original.call(*args).tap { |status| polled_statuses << status.status }
171
- end
172
- expect(translation_memories).to receive(:sleep).once
173
-
174
- job = translation_memories.wait_until_job_done(created.job_id)
175
-
176
- expect(polled_statuses).to eq(%w[awaiting_input completed])
177
- expect(job.status).to eq('completed')
178
- DeepL.translation_memories.destroy(job.result.translation_memory_id)
179
- end
180
-
181
- it 'when the file is never uploaded it waits until the timeout of the caller' do
182
- created = translation_memories.create_import('never-uploaded.tmx', 128)
183
- # Such a job does not finish on its own, only the timeout ends the wait.
184
- expect(translation_memories).not_to receive(:sleep)
185
-
186
- expect { translation_memories.wait_until_job_done(created.job_id, timeout_s: 0) }
187
- .to raise_error(DeepL::Exceptions::Error, /Manual timeout of 0s exceeded/)
188
- end
189
- end
190
-
191
- describe '#export_to_filepath' do
192
- let(:output_path) { File.join(temp_dir, 'export.tmx') }
193
-
194
- it 'when exporting a translation memory to a TMX file' do
195
- job = DeepL.translation_memories.export_to_filepath(default_tm_id, output_path)
196
- expect(job).to be_a(DeepL::Resources::TranslationMemoryJob)
197
- expect(job.operation).to eq('export')
198
- expect(job.status).to eq('completed')
199
- expect(job.result.download_url).to start_with('http')
200
-
201
- contents = File.read(output_path)
202
- expect(contents).to include('<tmx version="1.4">')
203
- expect(contents).to include('<seg>Quelltext Nummer 0</seg>')
204
- expect(contents).to include('<tuv xml:lang="en"><seg>Target text number 0 (de->en)</seg>')
205
- end
206
-
207
- it 'when re-exporting an unchanged translation memory it reuses the completed job' do
208
- job = DeepL.translation_memories.export_to_filepath(default_tm_id, output_path)
209
- reused = DeepL.translation_memories.create_export(default_tm_id)
210
- expect(reused).to be_a(DeepL::Resources::TranslationMemoryExport)
211
- expect(reused.reused_existing?).to be(true)
212
- expect(reused.job_id).to eq(job.job_id)
213
- expect(reused.translation_memory_id).to eq(default_tm_id)
214
- end
215
- end
216
-
217
- describe '#destroy' do
218
- it 'when deleting a translation memory it can no longer be found' do
219
- job = DeepL.translation_memories.import_from_filepath(tmx_file_path)
220
- translation_memory_id = job.result.translation_memory_id
221
-
222
- expect(DeepL.translation_memories.destroy(translation_memory_id))
223
- .to eq(translation_memory_id)
224
- expect { DeepL.translation_memories.find(translation_memory_id) }
225
- .to raise_error(DeepL::Exceptions::NotFound)
226
- end
227
- end
228
-
229
- def next_segments_page(page)
230
- DeepL.translation_memories.segments(default_tm_id, page_size: 5,
231
- page_cursor: page.next_page_cursor)
232
- end
233
-
234
- def build_test_translation_memory
235
- tm_data = {
236
- 'translation_memory_id' => 'a74d88fb-ed2a-4943-a664-a4512398b994',
237
- 'name' => 'Default Translation Memory',
238
- 'source_language' => 'DE',
239
- 'target_languages' => %w[EN ES FR],
240
- 'segment_count' => 3542
241
- }
242
- DeepL::Resources::TranslationMemory.new(tm_data, nil, nil)
243
- end
244
- end
@@ -1,87 +0,0 @@
1
- # Copyright 2026 DeepL SE (https://www.deepl.com)
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE file.
4
- # frozen_string_literal: true
5
-
6
- require 'spec_helper'
7
-
8
- describe DeepL::TranslationMemoryApi, :mock_server_only do # rubocop:disable RSpec/SpecFilePathFormat
9
- include_context 'with a live mock server'
10
-
11
- let(:default_tm_id) { 'a74d88fb-ed2a-4943-a664-a4512398b994' }
12
- let(:missing_uuid) { '00000000-0000-0000-0000-000000000000' }
13
-
14
- describe 'authorization failures' do
15
- let(:unauthorized_translation_memories) { described_class.new(unauthorized_api) }
16
-
17
- it 'raises AuthorizationFailed when listing with an invalid auth key' do
18
- expect { unauthorized_translation_memories.list }
19
- .to raise_error(DeepL::Exceptions::AuthorizationFailed)
20
- end
21
-
22
- it 'raises AuthorizationFailed on a read operation (find)' do
23
- expect { unauthorized_translation_memories.find(default_tm_id) }
24
- .to raise_error(DeepL::Exceptions::AuthorizationFailed)
25
- end
26
-
27
- it 'raises AuthorizationFailed on a write operation (create_export)' do
28
- expect { unauthorized_translation_memories.create_export(default_tm_id) }
29
- .to raise_error(DeepL::Exceptions::AuthorizationFailed)
30
- end
31
- end
32
-
33
- describe 'not-found failures' do
34
- it 'raises NotFound when #find is called with a missing UUID' do
35
- expect { DeepL.translation_memories.find(missing_uuid) }
36
- .to raise_error(DeepL::Exceptions::NotFound)
37
- end
38
-
39
- it 'raises NotFound when #segments is called with a missing UUID' do
40
- expect { DeepL.translation_memories.segments(missing_uuid) }
41
- .to raise_error(DeepL::Exceptions::NotFound)
42
- end
43
-
44
- it 'raises NotFound when #find_job is called with a missing UUID' do
45
- expect { DeepL.translation_memories.find_job(missing_uuid) }
46
- .to raise_error(DeepL::Exceptions::NotFound)
47
- end
48
- end
49
-
50
- describe 'bad-request failures' do
51
- it 'raises BadRequest when #segments is called with a too short text filter' do
52
- expect { DeepL.translation_memories.segments(default_tm_id, filter_text: 'a') }
53
- .to raise_error(DeepL::Exceptions::BadRequest)
54
- end
55
-
56
- it 'raises an error when #destroy is called with a malformed UUID' do
57
- expect { DeepL.translation_memories.destroy('invalid-uuid') }
58
- .to raise_error(DeepL::Exceptions::Error)
59
- end
60
- end
61
-
62
- describe 'client-side validation failures' do
63
- it 'raises an error when #find is called with an empty ID' do
64
- expect { DeepL.translation_memories.find('') }
65
- .to raise_error(DeepL::Exceptions::Error, /must not be empty/)
66
- end
67
-
68
- it 'raises an error when #find_job is called with an empty job ID' do
69
- expect { DeepL.translation_memories.find_job(nil) }
70
- .to raise_error(DeepL::Exceptions::Error, /must not be empty/)
71
- end
72
-
73
- it 'raises an error when #import_from_filepath is given a missing file' do
74
- expect { DeepL.translation_memories.import_from_filepath('/no/such/file.tmx') }
75
- .to raise_error(DeepL::Exceptions::Error, /No file found/)
76
- end
77
-
78
- it 'raises an error when #download_export is given an unfinished job' do
79
- created = DeepL.translation_memories.create_export(default_tm_id)
80
- job = DeepL::Resources::TranslationMemoryJob.new(
81
- { 'job_id' => created.job_id, 'results' => [{ 'status' => 'processing' }] }, nil, nil
82
- )
83
- expect { DeepL.translation_memories.download_export(job, '/tmp/unused.tmx') }
84
- .to raise_error(DeepL::Exceptions::Error, /no download URL/)
85
- end
86
- end
87
- end