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
@@ -22,7 +22,7 @@ module DeepL
22
22
  end
23
23
 
24
24
  def details
25
- "HTTP Headers: #{headers}\nPayload #{{ document_key: document_key }}"
25
+ "HTTP Headers: #{loggable_headers}\nPayload #{{ document_key: document_key }}"
26
26
  end
27
27
 
28
28
  def to_s
@@ -31,12 +31,20 @@ module DeepL
31
31
 
32
32
  private
33
33
 
34
+ # Binary mode: text mode would convert newlines on Windows and transcode the bytes when
35
+ # Encoding.default_internal is set.
34
36
  def extract_file(_request, response)
35
- File.write(@output_file, response.body)
37
+ File.binwrite(@output_file, response.body)
38
+ end
39
+
40
+ def validate_response!(response, request = nil)
41
+ raise Exceptions::DocumentNotReadyError.new(response, request) if response.code == '503'
42
+
43
+ super
36
44
  end
37
45
 
38
46
  def path
39
- "document/#{document_id}/result"
47
+ "document/#{escape_path_segment(document_id)}/result"
40
48
  end
41
49
  end
42
50
  end
@@ -21,7 +21,7 @@ module DeepL
21
21
  end
22
22
 
23
23
  def details
24
- "HTTP Headers: #{headers}\nPayload #{{ document_key: document_key }}"
24
+ "HTTP Headers: #{loggable_headers}\nPayload #{{ document_key: document_key }}"
25
25
  end
26
26
 
27
27
  def to_s
@@ -36,7 +36,7 @@ module DeepL
36
36
  end
37
37
 
38
38
  def path
39
- "document/#{document_id}"
39
+ "document/#{escape_path_segment(document_id)}"
40
40
  end
41
41
  end
42
42
  end
@@ -6,10 +6,12 @@
6
6
  module DeepL
7
7
  module Requests
8
8
  module Document
9
- class Upload < Base
9
+ class Upload < Base # rubocop:disable Metrics/ClassLength
10
10
  attr_reader :input_file_path, :source_lang, :target_lang, :filename
11
11
 
12
- SUPPORTED_OPTIONS = %w[formality glossary_id output_format].freeze
12
+ SUPPORTED_OPTIONS = %w[formality output_format enable_watermark].freeze
13
+ accepts_options(*SUPPORTED_OPTIONS, :glossary, :glossary_ids, :style_rule, :style_profile,
14
+ :translation_memory, :translation_memory_threshold)
13
15
 
14
16
  def initialize(api, input_file_path, source_lang, target_lang, filename = nil, # rubocop:disable Metrics/ParameterLists
15
17
  options = {}, additional_headers = {})
@@ -18,6 +20,9 @@ module DeepL
18
20
  @source_lang = source_lang
19
21
  @target_lang = target_lang
20
22
  @filename = filename
23
+ validate_languages!(source_lang, target_lang)
24
+ validate_translation_memory_threshold!
25
+ validate_glossary_source_lang!
21
26
  end
22
27
 
23
28
  def request
@@ -30,7 +35,7 @@ module DeepL
30
35
  end
31
36
 
32
37
  def details
33
- "HTTP Headers: #{headers}\nPayload #{[
38
+ "HTTP Headers: #{loggable_headers}\nPayload #{[
34
39
  ['file', "File at #{input_file_path} opened in binary mode"],
35
40
  ['source_lang', source_lang], ['target_lang', target_lang], ['filename', filename]
36
41
  ]}"
@@ -42,11 +47,22 @@ module DeepL
42
47
 
43
48
  private
44
49
 
50
+ # Text translation detects the source language for glossaries; documents do not.
51
+ def validate_glossary_source_lang!
52
+ return unless option?(:glossary) && source_lang.nil?
53
+
54
+ raise ArgumentError, 'The `glossary` option requires `source_lang` to be set'
55
+ end
56
+
57
+ # A repeated creation request could create a second resource.
58
+ def retry_only_unprocessed?
59
+ true
60
+ end
61
+
45
62
  def build_base_form_data(input_file)
46
- form_data = [
47
- ['file', input_file], ['source_lang', source_lang],
48
- ['target_lang', target_lang]
49
- ]
63
+ form_data = [['file', input_file]]
64
+ form_data.push(['source_lang', source_lang]) unless source_lang.nil?
65
+ form_data.push(['target_lang', target_lang])
50
66
  filename_param = filename || File.basename(input_file_path)
51
67
  form_data.push(['filename', filename_param]) unless filename_param.nil?
52
68
  add_extra_options_to_form(form_data)
@@ -56,7 +72,9 @@ module DeepL
56
72
  def add_extra_options_to_form(form_data)
57
73
  add_supported_options_to_form(form_data)
58
74
  add_glossary_ids_to_form(form_data)
75
+ add_glossary_to_form(form_data)
59
76
  add_style_rule_to_form(form_data)
77
+ add_style_profile_to_form(form_data)
60
78
  add_translation_memory_to_form(form_data)
61
79
  add_translation_memory_threshold_to_form(form_data)
62
80
  end
@@ -64,21 +82,38 @@ module DeepL
64
82
  # Validates and serializes the `glossary_ids` option (see
65
83
  # `DeepL::Requests::Base#build_glossary_ids_param`) and appends it to the form data as a
66
84
  # comma-separated `glossary_ids` field. Requires `source_lang`, cannot be combined with
67
- # the singular `glossary_id` option, and allows at most 5 IDs.
85
+ # the singular `glossary` option, and allows at most 5 IDs.
68
86
  def add_glossary_ids_to_form(form_data)
69
- glossary_ids = build_glossary_ids_param(source_lang)
87
+ glossary_ids = build_glossary_ids_param
88
+ # Text translation detects the source language for glossaries; documents do not.
89
+ if !glossary_ids.nil? && source_lang.nil?
90
+ raise ArgumentError, 'The `glossary_ids` option requires `source_lang` to be set'
91
+ end
92
+
70
93
  form_data.push(['glossary_ids', glossary_ids.join(',')]) unless glossary_ids.nil?
71
94
  end
72
95
 
96
+ # Resolves the `glossary` option (an ID string, a `DeepL::Resources::Glossary` or a
97
+ # `DeepL::Resources::MultilingualGlossary`) and appends it as a `glossary_id` field.
98
+ def add_glossary_to_form(form_data)
99
+ glossary_id = build_glossary_param
100
+ form_data.push(['glossary_id', glossary_id]) unless glossary_id.nil?
101
+ end
102
+
73
103
  # Serializes the `style_rule` option and appends it to the form data as a `style_id`
74
104
  # field. Mirrors text translation: accepts either a style rule ID string or a
75
105
  # `DeepL::Resources::StyleRule` object.
76
106
  def add_style_rule_to_form(form_data)
77
- return unless option?(:style_rule)
107
+ style_id = take_resource_option(:style_rule, [DeepL::Resources::StyleRule], :style_id)
108
+ form_data.push(['style_id', style_id]) unless style_id.nil?
109
+ end
78
110
 
79
- rule = delete_option(:style_rule)
80
- style_id = rule.is_a?(DeepL::Resources::StyleRule) ? rule.style_id : rule
81
- form_data.push(['style_id', style_id.to_s])
111
+ # Serializes the `style_profile` option (a style profile ID string or a
112
+ # `DeepL::Resources::StyleProfile` object) as a `style_profile_id` field.
113
+ def add_style_profile_to_form(form_data)
114
+ profile_id = take_resource_option(:style_profile, [DeepL::Resources::StyleProfile],
115
+ :style_profile_id)
116
+ form_data.push(['style_profile_id', profile_id]) unless profile_id.nil?
82
117
  end
83
118
 
84
119
  # Serializes the `translation_memory` option and appends it to the form data as a
@@ -86,11 +121,9 @@ module DeepL
86
121
  # option accepts either a translation memory ID string or a
87
122
  # `DeepL::Resources::TranslationMemory` object.
88
123
  def add_translation_memory_to_form(form_data)
89
- return unless option?(:translation_memory)
90
-
91
- tm = delete_option(:translation_memory)
92
- tm_id = tm.is_a?(DeepL::Resources::TranslationMemory) ? tm.translation_memory_id : tm
93
- form_data.push(['translation_memory_id', tm_id.to_s])
124
+ tm_id = take_resource_option(:translation_memory, [DeepL::Resources::TranslationMemory],
125
+ :translation_memory_id)
126
+ form_data.push(['translation_memory_id', tm_id]) unless tm_id.nil?
94
127
  end
95
128
 
96
129
  # Serializes the `translation_memory_threshold` option (integer 0-100) and appends it to
@@ -99,20 +132,23 @@ module DeepL
99
132
  return unless option?(:translation_memory_threshold)
100
133
 
101
134
  threshold = delete_option(:translation_memory_threshold)
135
+ return if threshold.nil?
136
+
102
137
  form_data.push(['translation_memory_threshold', threshold.to_s])
103
138
  end
104
139
 
105
140
  def add_supported_options_to_form(form_data)
106
141
  SUPPORTED_OPTIONS.each do |option_name|
107
142
  option_value = option(option_name)
108
- form_data.push([option_name, option_value]) unless option_value.nil?
143
+ form_data.push([option_name, option_value.to_s]) unless option_value.nil?
109
144
  end
110
145
  end
111
146
 
112
147
  def build_doc_handle(request, response)
113
148
  parsed_response = JSON.parse(response.body)
114
149
  Resources::DocumentHandle.new(parsed_response['document_id'],
115
- parsed_response['document_key'], request, response)
150
+ parsed_response['document_key'], request, response,
151
+ api: api)
116
152
  end
117
153
 
118
154
  def path
@@ -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 Requests
8
+ module Glossary
9
+ # A request for one v2 glossary, given as its ID or a Resources::Glossary.
10
+ class Base < DeepL::Requests::Base
11
+ attr_reader :id
12
+
13
+ def initialize(api, glossary)
14
+ super(api, {})
15
+ @id = glossary.is_a?(DeepL::Resources::Glossary) ? glossary.id : glossary.to_s
16
+ end
17
+
18
+ def to_s
19
+ "#{self.class::METHOD} #{uri.request_uri}"
20
+ end
21
+
22
+ private
23
+
24
+ def glossary_path(suffix = nil)
25
+ ["glossaries/#{escape_path_segment(id)}", suffix].compact.join('/')
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -6,31 +6,28 @@
6
6
  module DeepL
7
7
  module Requests
8
8
  module Glossary
9
- class Create < Base
9
+ # Creates a v2 glossary from entries that are already encoded as TSV or CSV.
10
+ class Create < DeepL::Requests::Base
10
11
  attr_reader :name, :source_lang, :target_lang, :entries, :entries_format
11
12
 
12
- def initialize(api, name, source_lang, target_lang, entries, options = {})
13
+ def initialize(api, name, source_lang, target_lang, entries, entries_format, options = {}) # rubocop:disable Metrics/ParameterLists
13
14
  super(api, options)
15
+ raise ArgumentError, 'Glossary name must not be empty' if name.to_s.empty?
16
+
14
17
  @name = name
15
- @source_lang = source_lang
16
- @target_lang = target_lang
18
+ # v2 glossaries are not regional: EN-GB is sent as EN.
19
+ @source_lang = source_lang.to_s.split('-').first.upcase
20
+ @target_lang = target_lang.to_s.split('-').first.upcase
17
21
  @entries = entries
18
- @entries_format = delete_option(:entries_format) || 'tsv'
22
+ @entries_format = entries_format
19
23
  end
20
24
 
21
25
  def request
22
- payload = {
23
- name: name, source_lang: source_lang, target_lang: target_lang, entries: entries_to_tsv,
24
- entries_format: entries_format
25
- }
26
26
  build_glossary(*execute_request_with_retries(post_request(payload)))
27
27
  end
28
28
 
29
29
  def details
30
- "HTTP Headers: #{headers}\nPayload #{{
31
- name: name, source_lang: source_lang, target_lang: target_lang, entries: entries_to_tsv,
32
- entries_format: entries_format
33
- }}"
30
+ "HTTP Headers: #{loggable_headers}\nPayload #{payload}"
34
31
  end
35
32
 
36
33
  def to_s
@@ -39,15 +36,18 @@ module DeepL
39
36
 
40
37
  private
41
38
 
42
- def entries_to_tsv
43
- return entries if entries.is_a?(String)
39
+ # A repeated creation request could create a second resource.
40
+ def retry_only_unprocessed?
41
+ true
42
+ end
44
43
 
45
- entries.reduce('') { |tsv, entry| "#{tsv}#{entry.first}\t#{entry.last}\n" }
44
+ def payload
45
+ { name: name, source_lang: source_lang, target_lang: target_lang, entries: entries,
46
+ entries_format: entries_format }
46
47
  end
47
48
 
48
49
  def build_glossary(request, response)
49
- glossary = JSON.parse(response.body)
50
- Resources::Glossary.new(glossary, request, response)
50
+ Resources::Glossary.new(JSON.parse(response.body), request, response)
51
51
  end
52
52
 
53
53
  def path
@@ -7,29 +7,17 @@ module DeepL
7
7
  module Requests
8
8
  module Glossary
9
9
  class Destroy < Base
10
- attr_reader :id
11
-
12
- def initialize(api, id, options = {})
13
- super(api, options)
14
- @id = id
15
- end
10
+ METHOD = 'DELETE'
16
11
 
17
12
  def request
18
- build_response(*execute_request_with_retries(delete_request))
19
- end
20
-
21
- def to_s
22
- "DELETE #{uri.request_uri}"
13
+ execute_request_with_retries(delete_request)
14
+ nil
23
15
  end
24
16
 
25
17
  private
26
18
 
27
- def build_response(_, _)
28
- id
29
- end
30
-
31
19
  def path
32
- "glossaries/#{id}"
20
+ glossary_path
33
21
  end
34
22
  end
35
23
  end
@@ -7,29 +7,21 @@ module DeepL
7
7
  module Requests
8
8
  module Glossary
9
9
  class Entries < Base
10
- attr_reader :id
11
-
12
- def initialize(api, id, options = {})
13
- super(api, options)
14
- @id = id
15
- end
10
+ METHOD = 'GET'
16
11
 
17
12
  def request
18
- build_entries(*execute_request_with_retries(get_request))
19
- end
20
-
21
- def to_s
22
- "GET #{uri.request_uri}"
13
+ _, response = execute_request_with_retries(get_request)
14
+ Utils::GlossaryEntries.from_tsv(response.body)
23
15
  end
24
16
 
25
17
  private
26
18
 
27
- def build_entries(_, response)
28
- response.body.split("\n").map { |entry| entry.split("\t") }
19
+ def get_request # rubocop:disable Naming/AccessorMethodName
20
+ Net::HTTP::Get.new(uri.path, headers.merge('Accept' => 'text/tab-separated-values'))
29
21
  end
30
22
 
31
23
  def path
32
- "glossaries/#{id}/entries"
24
+ glossary_path('entries')
33
25
  end
34
26
  end
35
27
  end
@@ -7,30 +7,20 @@ module DeepL
7
7
  module Requests
8
8
  module Glossary
9
9
  class Find < Base
10
- attr_reader :id
11
-
12
- def initialize(api, id, options = {})
13
- super(api, options)
14
- @id = id
15
- end
10
+ METHOD = 'GET'
16
11
 
17
12
  def request
18
13
  build_glossary(*execute_request_with_retries(get_request))
19
14
  end
20
15
 
21
- def to_s
22
- "GET #{uri.request_uri}"
23
- end
24
-
25
16
  private
26
17
 
27
18
  def build_glossary(request, response)
28
- glossary = JSON.parse(response.body)
29
- Resources::Glossary.new(glossary, request, response)
19
+ Resources::Glossary.new(JSON.parse(response.body), request, response)
30
20
  end
31
21
 
32
22
  def path
33
- "glossaries/#{id}"
23
+ glossary_path
34
24
  end
35
25
  end
36
26
  end
@@ -7,8 +7,8 @@ module DeepL
7
7
  module Requests
8
8
  module Glossary
9
9
  class LanguagePairs < Base
10
- def initialize(api, options = {})
11
- super
10
+ def initialize(api)
11
+ super(api, {})
12
12
  end
13
13
 
14
14
  def request
@@ -23,14 +23,30 @@ module DeepL
23
23
 
24
24
  def build_language_pair_list(request, response)
25
25
  data = JSON.parse(response.body)
26
- data['supported_languages'].map do |language_pair|
27
- Resources::LanguagePair.new(language_pair['source_lang'], language_pair['target_lang'],
28
- request, response)
26
+ sources = langs_where(data, 'usable_as_source')
27
+ targets = langs_where(data, 'usable_as_target')
28
+
29
+ sources.product(targets).each_with_object([]) do |(source, target), pairs|
30
+ next if source == target
31
+
32
+ pairs << Resources::LanguagePair.new(source, target, request, response)
29
33
  end
30
34
  end
31
35
 
36
+ def langs_where(data, usable_key)
37
+ data.select { |language| language[usable_key] }.map { |language| language['lang'] }
38
+ end
39
+
40
+ def uri
41
+ @uri ||= URI("#{host}/v3/#{path}?resource=glossary")
42
+ end
43
+
44
+ def get_request # rubocop:disable Naming/AccessorMethodName
45
+ Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
46
+ end
47
+
32
48
  def path
33
- 'glossary-language-pairs'
49
+ 'languages'
34
50
  end
35
51
  end
36
52
  end
@@ -7,8 +7,8 @@ module DeepL
7
7
  module Requests
8
8
  module Glossary
9
9
  class List < Base
10
- def initialize(api, options = {})
11
- super
10
+ def initialize(api)
11
+ super(api, {})
12
12
  end
13
13
 
14
14
  def request
@@ -0,0 +1,41 @@
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
+ module DeepL
7
+ module Requests
8
+ class LanguageResources < Base
9
+ def initialize(api, options = {})
10
+ super
11
+ end
12
+
13
+ def request
14
+ build_language_resource_list(*execute_request_with_retries(get_request))
15
+ end
16
+
17
+ def to_s
18
+ "GET #{uri.request_uri}"
19
+ end
20
+
21
+ private
22
+
23
+ def build_language_resource_list(request, response)
24
+ data = JSON.parse(response.body)
25
+ data.map { |resource| Resources::LanguageResource.new(resource, request, response) }
26
+ end
27
+
28
+ def uri
29
+ @uri ||= URI("#{host}/v3/#{path}")
30
+ end
31
+
32
+ def get_request # rubocop:disable Naming/AccessorMethodName
33
+ Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
34
+ end
35
+
36
+ def path
37
+ 'languages/resources'
38
+ end
39
+ end
40
+ end
41
+ end
@@ -6,8 +6,10 @@
6
6
  module DeepL
7
7
  module Requests
8
8
  class Languages < Base
9
- def initialize(api, options = {})
10
- super
9
+ # +type+ is +:source+ or +:target+.
10
+ def initialize(api, type)
11
+ super(api, {})
12
+ @type = type
11
13
  end
12
14
 
13
15
  def request
@@ -20,15 +22,32 @@ module DeepL
20
22
 
21
23
  private
22
24
 
25
+ def target?
26
+ @type == :target
27
+ end
28
+
23
29
  def build_languages(request, response)
24
30
  data = JSON.parse(response.body)
25
- data.map do |language|
26
- Resources::Language.new(language['language'], language['name'],
27
- language['supports_formality'],
28
- request, response)
31
+ usable_key = target? ? 'usable_as_target' : 'usable_as_source'
32
+ data.select { |language| language[usable_key] }.map do |language|
33
+ build_language(language, request, response)
29
34
  end
30
35
  end
31
36
 
37
+ def build_language(language, request, response)
38
+ supports_formality = target? ? language.fetch('features', {}).to_h.key?('formality') : nil
39
+ Resources::Language.new(language['lang'].upcase, language['name'], supports_formality,
40
+ request, response)
41
+ end
42
+
43
+ def uri
44
+ @uri ||= URI("#{host}/v3/#{path}?resource=translate_text")
45
+ end
46
+
47
+ def get_request # rubocop:disable Naming/AccessorMethodName
48
+ Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
49
+ end
50
+
32
51
  def path
33
52
  'languages'
34
53
  end
@@ -0,0 +1,56 @@
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
+ module DeepL
7
+ module Requests
8
+ class LanguagesForResource < Base
9
+ accepts_options :include
10
+
11
+ def initialize(api, resource, options = {})
12
+ super(api, options)
13
+ @resource = resource.to_s
14
+ raise ArgumentError, 'resource must not be empty' if @resource.empty?
15
+ end
16
+
17
+ def request
18
+ build_language_support_list(*execute_request_with_retries(get_request))
19
+ end
20
+
21
+ def to_s
22
+ "GET #{uri.request_uri}"
23
+ end
24
+
25
+ private
26
+
27
+ def build_language_support_list(request, response)
28
+ data = JSON.parse(response.body)
29
+ data.map { |language| Resources::LanguageSupport.new(language, request, response) }
30
+ end
31
+
32
+ def uri
33
+ @uri ||= begin
34
+ base_uri = URI("#{host}/v3/#{path}")
35
+ base_uri.query = build_query_string
36
+ base_uri
37
+ end
38
+ end
39
+
40
+ # include is sent as repeated include=<value> parameters
41
+ def build_query_string
42
+ params = [['resource', @resource]]
43
+ Array(option(:include)).each { |value| params << ['include', value.to_s] }
44
+ encode_query(params)
45
+ end
46
+
47
+ def get_request # rubocop:disable Naming/AccessorMethodName
48
+ Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
49
+ end
50
+
51
+ def path
52
+ 'languages'
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,45 @@
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 Requests
8
+ module MultilingualGlossary
9
+ class Create < Base
10
+ include Helpers
11
+
12
+ attr_reader :name, :dictionaries
13
+
14
+ def initialize(api, name, dictionaries, options = {})
15
+ super(api, options)
16
+ raise ArgumentError, 'Glossary name must not be empty' if name.to_s.empty?
17
+ raise ArgumentError, 'Glossary needs at least one dictionary' if dictionaries.to_a.empty?
18
+
19
+ @name = name
20
+ @dictionaries = dictionaries.map { |dictionary| dictionary_payload(dictionary) }
21
+ end
22
+
23
+ def request
24
+ payload = { name: name, dictionaries: dictionaries }
25
+ build_glossary(*execute_request_with_retries(post_request(payload)))
26
+ end
27
+
28
+ def to_s
29
+ "POST #{uri.request_uri}"
30
+ end
31
+
32
+ private
33
+
34
+ # A repeated creation request could create a second resource.
35
+ def retry_only_unprocessed?
36
+ true
37
+ end
38
+
39
+ def path
40
+ 'glossaries'
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end