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
@@ -0,0 +1,74 @@
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 SpokenTerms
9
+ # Encoding shared by the spoken terms requests. The API sends and returns the terms of a
10
+ # language as one newline-separated string; the SDK exposes them as an array.
11
+ module Helpers
12
+ private
13
+
14
+ def spoken_terms_id(spoken_terms)
15
+ id = if spoken_terms.is_a?(DeepL::Resources::SpokenTerms)
16
+ spoken_terms.spoken_terms_id
17
+ else
18
+ spoken_terms
19
+ end
20
+ raise ArgumentError, 'Spoken terms ID must not be empty' if id.to_s.empty?
21
+
22
+ id.to_s
23
+ end
24
+
25
+ # The path of a collection, with its ID URL-encoded.
26
+ def spoken_terms_path(id, suffix = nil)
27
+ ["spoken-terms/#{escape_path_segment(id)}", suffix].compact.join('/')
28
+ end
29
+
30
+ def entries_to_string(entries)
31
+ entries.map { |entry| Utils::TermValidator.validate!(entry) }.join("\n")
32
+ end
33
+
34
+ def entries_from_string(entries)
35
+ (entries || '').split("\n").reject(&:empty?)
36
+ end
37
+
38
+ def term_lists_payload(term_lists)
39
+ term_lists.map { |term_list| term_list_payload(term_list) }
40
+ end
41
+
42
+ # Accepts a SpokenTermsEntries object or a hash with +:lang+ and +:entries+.
43
+ def term_list_payload(term_list)
44
+ lang, entries = if term_list.is_a?(DeepL::Resources::SpokenTermsEntries)
45
+ [term_list.lang, term_list.entries]
46
+ else
47
+ [term_list[:lang] || term_list['lang'],
48
+ term_list[:entries] || term_list['entries']]
49
+ end
50
+ validate_language!('lang', lang)
51
+
52
+ { lang: lang, entries: entries_to_string(entries) }
53
+ end
54
+
55
+ def build_spoken_terms(request, response)
56
+ Resources::SpokenTerms.new(JSON.parse(response.body), request, response)
57
+ end
58
+
59
+ def uri
60
+ @uri ||= URI("#{host}/v3/#{path}")
61
+ end
62
+
63
+ # GET and DELETE send no body.
64
+ def get_request # rubocop:disable Naming/AccessorMethodName
65
+ Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
66
+ end
67
+
68
+ def delete_request
69
+ Net::HTTP::Delete.new(uri.request_uri, add_json_content_type(headers))
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,35 @@
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 SpokenTerms
9
+ class List < Base
10
+ include Helpers
11
+
12
+ def request
13
+ build_spoken_terms_list(*execute_request_with_retries(get_request))
14
+ end
15
+
16
+ def to_s
17
+ "GET #{uri.request_uri}"
18
+ end
19
+
20
+ private
21
+
22
+ def build_spoken_terms_list(request, response)
23
+ data = JSON.parse(response.body)
24
+ (data['spoken_terms'] || []).map do |spoken_terms|
25
+ Resources::SpokenTerms.new(spoken_terms, request, response)
26
+ end
27
+ end
28
+
29
+ def path
30
+ 'spoken-terms'
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -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.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Requests
8
+ module SpokenTerms
9
+ class ReplaceTermList < Base
10
+ include Helpers
11
+
12
+ attr_reader :id, :entries
13
+
14
+ def initialize(api, id, entries, options = {})
15
+ super(api, options)
16
+ @id = spoken_terms_id(id)
17
+ @entries = entries
18
+ end
19
+
20
+ def request
21
+ payload = term_list_payload(entries)
22
+ build_term_list(*execute_request_with_retries(put_request(payload)))
23
+ end
24
+
25
+ def to_s
26
+ "PUT #{uri.request_uri}"
27
+ end
28
+
29
+ private
30
+
31
+ def build_term_list(request, response)
32
+ Resources::SpokenTermsList.new(JSON.parse(response.body), request, response)
33
+ end
34
+
35
+ def path
36
+ spoken_terms_path(id, 'term-lists')
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,40 @@
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 SpokenTerms
9
+ class Update < Base
10
+ include Helpers
11
+
12
+ attr_reader :id, :name, :term_lists
13
+
14
+ def initialize(api, id, name, term_lists, options = {})
15
+ super(api, options)
16
+ @id = spoken_terms_id(id)
17
+ @name = name
18
+ @term_lists = term_lists
19
+ end
20
+
21
+ def request
22
+ payload = {}
23
+ payload[:name] = name unless name.nil?
24
+ payload[:term_lists] = term_lists_payload(term_lists) unless term_lists.nil?
25
+ build_spoken_terms(*execute_request_with_retries(patch_request(payload)))
26
+ end
27
+
28
+ def to_s
29
+ "PATCH #{uri.request_uri}"
30
+ end
31
+
32
+ private
33
+
34
+ def path
35
+ spoken_terms_path(id)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,83 @@
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 StyleProfile
9
+ # Common behaviour of the style profile requests: served under +v3+, with the ID escaped
10
+ # into the path and GET/DELETE sent without a body.
11
+ class Base < DeepL::Requests::Base
12
+ COMPONENT_ID_FIELDS = %i[glossary_ids style_rule_ids translation_memory_ids].freeze
13
+
14
+ private
15
+
16
+ def style_profile_id(style_profile)
17
+ id = if style_profile.is_a?(DeepL::Resources::StyleProfile)
18
+ style_profile.style_profile_id.to_s
19
+ else
20
+ style_profile.to_s
21
+ end
22
+ raise ArgumentError, 'style_profile_id must not be empty' if id.empty?
23
+
24
+ id
25
+ end
26
+
27
+ def escaped_id
28
+ URI.encode_www_form_component(id).gsub('+', '%20')
29
+ end
30
+
31
+ # Adds the optional fields that are set. nil is not sent; '' and [] are sent as given.
32
+ def optional_fields_payload(fields)
33
+ fields.each_with_object({}) do |(key, value), payload|
34
+ next if value.nil?
35
+
36
+ payload[key] = COMPONENT_ID_FIELDS.include?(key) ? component_ids(value) : value
37
+ end
38
+ end
39
+
40
+ # Replaces resource objects in a component list by their IDs; other entries are kept.
41
+ def component_ids(value)
42
+ return value unless value.is_a?(Array)
43
+
44
+ value.map { |component| component_id(component) }
45
+ end
46
+
47
+ def component_id(component)
48
+ case component
49
+ when DeepL::Resources::Glossary, DeepL::Resources::MultilingualGlossary then component.id
50
+ when DeepL::Resources::StyleRule then component.style_id
51
+ when DeepL::Resources::TranslationMemory then component.translation_memory_id
52
+ else component
53
+ end
54
+ end
55
+
56
+ def build_style_profile(request, response)
57
+ Resources::StyleProfile.new(JSON.parse(response.body), request, response)
58
+ end
59
+
60
+ def uri
61
+ @uri ||= begin
62
+ base_uri = URI("#{host}/v3/#{path}")
63
+ query_string = encode_query(query_params)
64
+ base_uri.query = query_string unless query_string.empty?
65
+ base_uri
66
+ end
67
+ end
68
+
69
+ def query_params
70
+ {}
71
+ end
72
+
73
+ def get_request # rubocop:disable Naming/AccessorMethodName
74
+ Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
75
+ end
76
+
77
+ def delete_request
78
+ Net::HTTP::Delete.new(uri.request_uri, add_json_content_type(headers))
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,40 @@
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 StyleProfile
9
+ class Create < Base
10
+ attr_reader :name, :fields
11
+
12
+ def initialize(api, name, fields, options = {})
13
+ super(api, options)
14
+ @name = name
15
+ @fields = fields
16
+ end
17
+
18
+ def request
19
+ payload = { name: name }.merge(optional_fields_payload(fields))
20
+ build_style_profile(*execute_request_with_retries(post_request(payload)))
21
+ end
22
+
23
+ def to_s
24
+ "POST #{uri.request_uri}"
25
+ end
26
+
27
+ private
28
+
29
+ # A repeated creation request could create a second resource.
30
+ def retry_only_unprocessed?
31
+ true
32
+ end
33
+
34
+ def path
35
+ 'style_profiles'
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,34 @@
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 StyleProfile
9
+ class Destroy < Base
10
+ attr_reader :id
11
+
12
+ def initialize(api, id, options = {})
13
+ super(api, options)
14
+ @id = style_profile_id(id)
15
+ end
16
+
17
+ def request
18
+ execute_request_with_retries(delete_request)
19
+ id
20
+ end
21
+
22
+ def to_s
23
+ "DELETE #{uri.request_uri}"
24
+ end
25
+
26
+ private
27
+
28
+ def path
29
+ "style_profiles/#{escaped_id}"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
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 StyleProfile
9
+ class Find < Base
10
+ attr_reader :id
11
+
12
+ def initialize(api, id, options = {})
13
+ super(api, options)
14
+ @id = style_profile_id(id)
15
+ end
16
+
17
+ def request
18
+ build_style_profile(*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 path
28
+ "style_profiles/#{escaped_id}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,43 @@
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 StyleProfile
9
+ class List < Base
10
+ accepts_options :page, :page_size
11
+
12
+ def request
13
+ build_style_profile_list(*execute_request_with_retries(get_request))
14
+ end
15
+
16
+ def to_s
17
+ "GET #{uri.request_uri}"
18
+ end
19
+
20
+ private
21
+
22
+ def build_style_profile_list(request, response)
23
+ profiles = JSON.parse(response.body)['style_profiles']
24
+ return [] unless profiles.is_a?(Array)
25
+
26
+ profiles.grep(Hash).map do |profile|
27
+ Resources::StyleProfile.new(profile, request, response)
28
+ end
29
+ end
30
+
31
+ def query_params
32
+ self.class.accepted_options.each_with_object({}) do |option_name, params|
33
+ params[option_name] = option(option_name).to_s if option?(option_name)
34
+ end
35
+ end
36
+
37
+ def path
38
+ 'style_profiles'
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
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 StyleProfile
9
+ class Update < Base
10
+ attr_reader :id, :fields
11
+
12
+ def initialize(api, id, fields, options = {})
13
+ super(api, options)
14
+ @id = style_profile_id(id)
15
+ @fields = fields
16
+ end
17
+
18
+ def request
19
+ payload = optional_fields_payload(fields)
20
+ build_style_profile(*execute_request_with_retries(patch_request(payload)))
21
+ end
22
+
23
+ def to_s
24
+ "PATCH #{uri.request_uri}"
25
+ end
26
+
27
+ private
28
+
29
+ def path
30
+ "style_profiles/#{escaped_id}"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -7,12 +7,18 @@ module DeepL
7
7
  module Requests
8
8
  module StyleRule
9
9
  class Create < Base
10
+ include Helpers
11
+
12
+ accepts_options :configured_rules, :custom_instructions
13
+
10
14
  def initialize(api, name, language, options = {})
11
15
  super(api, options)
16
+ check_non_empty!(name: name, language: language)
12
17
  @name = name
13
18
  @language = language
14
19
  @configured_rules = delete_option(:configured_rules)
15
20
  @custom_instructions = delete_option(:custom_instructions)
21
+ validate_instruction_languages!(@custom_instructions)
16
22
  end
17
23
 
18
24
  def request
@@ -28,6 +34,24 @@ module DeepL
28
34
 
29
35
  private
30
36
 
37
+ # A repeated creation request could create a second resource.
38
+ def retry_only_unprocessed?
39
+ true
40
+ end
41
+
42
+ # +custom_instructions+ is sent as given; only an empty +source_language+ is rejected.
43
+ def validate_instruction_languages!(instructions)
44
+ return unless instructions.is_a?(Array)
45
+
46
+ instructions.each do |instruction|
47
+ next unless instruction.is_a?(Hash)
48
+
49
+ validate_language!('source_language',
50
+ instruction[:source_language] || instruction['source_language'],
51
+ optional: true)
52
+ end
53
+ end
54
+
31
55
  def build_style_rule(request, response)
32
56
  data = JSON.parse(response.body)
33
57
  DeepL::Resources::StyleRule.new(data, request, response)
@@ -7,11 +7,15 @@ module DeepL
7
7
  module Requests
8
8
  module StyleRule
9
9
  class CreateCustomInstruction < Base
10
+ include Helpers
11
+
10
12
  def initialize(api, style_id, label, prompt, source_language = nil, options = {})
11
13
  super(api, options)
12
- @style_id = style_id
14
+ @style_id = style_rule_id(style_id)
15
+ check_non_empty!(label: label, prompt: prompt)
13
16
  @label = label
14
17
  @prompt = prompt
18
+ validate_language!('source_language', source_language, optional: true)
15
19
  @source_language = source_language
16
20
  end
17
21
 
@@ -27,6 +31,11 @@ module DeepL
27
31
 
28
32
  private
29
33
 
34
+ # A repeated creation request could create a second resource.
35
+ def retry_only_unprocessed?
36
+ true
37
+ end
38
+
30
39
  def build_custom_instruction(_request, response)
31
40
  data = JSON.parse(response.body)
32
41
  DeepL::Resources::CustomInstruction.new(data)
@@ -37,7 +46,7 @@ module DeepL
37
46
  end
38
47
 
39
48
  def path
40
- "style_rules/#{@style_id}/custom_instructions"
49
+ style_rule_path(@style_id, 'custom_instructions')
41
50
  end
42
51
  end
43
52
  end
@@ -7,9 +7,11 @@ module DeepL
7
7
  module Requests
8
8
  module StyleRule
9
9
  class Destroy < Base
10
+ include Helpers
11
+
10
12
  def initialize(api, style_id, options = {})
11
13
  super(api, options)
12
- @style_id = style_id
14
+ @style_id = style_rule_id(style_id)
13
15
  end
14
16
 
15
17
  def request
@@ -31,7 +33,7 @@ module DeepL
31
33
  end
32
34
 
33
35
  def path
34
- "style_rules/#{@style_id}"
36
+ style_rule_path(@style_id)
35
37
  end
36
38
  end
37
39
  end
@@ -7,10 +7,12 @@ module DeepL
7
7
  module Requests
8
8
  module StyleRule
9
9
  class DestroyCustomInstruction < Base
10
+ include Helpers
11
+
10
12
  def initialize(api, style_id, instruction_id, options = {})
11
13
  super(api, options)
12
- @style_id = style_id
13
- @instruction_id = instruction_id
14
+ @style_id = style_rule_id(style_id)
15
+ @instruction_id = custom_instruction_id(instruction_id)
14
16
  end
15
17
 
16
18
  def request
@@ -32,7 +34,7 @@ module DeepL
32
34
  end
33
35
 
34
36
  def path
35
- "style_rules/#{@style_id}/custom_instructions/#{@instruction_id}"
37
+ style_rule_path(@style_id, 'custom_instructions', @instruction_id)
36
38
  end
37
39
  end
38
40
  end
@@ -7,9 +7,11 @@ module DeepL
7
7
  module Requests
8
8
  module StyleRule
9
9
  class Find < Base
10
+ include Helpers
11
+
10
12
  def initialize(api, style_id, options = {})
11
13
  super(api, options)
12
- @style_id = style_id
14
+ @style_id = style_rule_id(style_id)
13
15
  end
14
16
 
15
17
  def request
@@ -32,7 +34,7 @@ module DeepL
32
34
  end
33
35
 
34
36
  def path
35
- "style_rules/#{@style_id}"
37
+ style_rule_path(@style_id)
36
38
  end
37
39
  end
38
40
  end
@@ -7,10 +7,12 @@ module DeepL
7
7
  module Requests
8
8
  module StyleRule
9
9
  class FindCustomInstruction < Base
10
+ include Helpers
11
+
10
12
  def initialize(api, style_id, instruction_id, options = {})
11
13
  super(api, options)
12
- @style_id = style_id
13
- @instruction_id = instruction_id
14
+ @style_id = style_rule_id(style_id)
15
+ @instruction_id = custom_instruction_id(instruction_id)
14
16
  end
15
17
 
16
18
  def request
@@ -33,7 +35,7 @@ module DeepL
33
35
  end
34
36
 
35
37
  def path
36
- "style_rules/#{@style_id}/custom_instructions/#{@instruction_id}"
38
+ style_rule_path(@style_id, 'custom_instructions', @instruction_id)
37
39
  end
38
40
  end
39
41
  end
@@ -0,0 +1,44 @@
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 StyleRule
9
+ # IDs and paths shared by the style rule requests. A style rule list and a custom
10
+ # instruction are given as their ID or as the resource object.
11
+ module Helpers
12
+ private
13
+
14
+ def style_rule_id(style_rule)
15
+ id = style_rule.is_a?(DeepL::Resources::StyleRule) ? style_rule.style_id : style_rule
16
+ raise ArgumentError, 'Style rule ID must not be empty' if id.to_s.empty?
17
+
18
+ id
19
+ end
20
+
21
+ def custom_instruction_id(instruction)
22
+ id = instruction.is_a?(DeepL::Resources::CustomInstruction) ? instruction.id : instruction
23
+ raise ArgumentError, 'Custom instruction ID must not be empty' if id.to_s.empty?
24
+
25
+ id
26
+ end
27
+
28
+ # Raises ArgumentError for each of +values+ (name => value) that is nil or empty.
29
+ def check_non_empty!(values)
30
+ values.each do |name, value|
31
+ raise ArgumentError, "`#{name}` must not be empty" if value.to_s.empty?
32
+ end
33
+ end
34
+
35
+ # The path of a style rule list, with its ID and the custom instruction ID URL-encoded.
36
+ def style_rule_path(style_id, suffix = nil, instruction_id = nil)
37
+ segments = ["style_rules/#{escape_path_segment(style_id)}", suffix]
38
+ segments << escape_path_segment(instruction_id) unless instruction_id.nil?
39
+ segments.compact.join('/')
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end