deepl-rb 3.10.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/CHANGELOG.md +313 -1
  4. data/README.md +225 -309
  5. data/Rakefile +17 -8
  6. data/VERSION +1 -1
  7. data/deepl-rb.gemspec +73 -86
  8. data/lib/deepl/admin_client.rb +39 -14
  9. data/lib/deepl/api.rb +3 -17
  10. data/lib/deepl/app_info.rb +0 -1
  11. data/lib/deepl/client.rb +399 -84
  12. data/lib/deepl/configuration.rb +19 -70
  13. data/lib/deepl/exceptions/authorization_failed.rb +1 -1
  14. data/lib/deepl/exceptions/connection_error.rb +10 -7
  15. data/lib/deepl/exceptions/limit_exceeded.rb +1 -1
  16. data/lib/deepl/exceptions/quota_exceeded.rb +1 -1
  17. data/lib/deepl/exceptions/request_entity_too_large.rb +1 -2
  18. data/lib/deepl/exceptions/request_error.rb +7 -0
  19. data/lib/deepl/exceptions/server_error.rb +2 -1
  20. data/lib/deepl/exceptions/translation_memory_job_error.rb +32 -0
  21. data/lib/deepl/exceptions/validation_error.rb +3 -2
  22. data/lib/deepl/requests/base.rb +129 -51
  23. data/lib/deepl/requests/correct.rb +2 -8
  24. data/lib/deepl/requests/document/upload.rb +29 -26
  25. data/lib/deepl/requests/glossary/base.rb +30 -0
  26. data/lib/deepl/requests/glossary/create.rb +18 -22
  27. data/lib/deepl/requests/glossary/destroy.rb +4 -16
  28. data/lib/deepl/requests/glossary/entries.rb +6 -14
  29. data/lib/deepl/requests/glossary/find.rb +3 -13
  30. data/lib/deepl/requests/glossary/language_pairs.rb +2 -2
  31. data/lib/deepl/requests/glossary/list.rb +2 -2
  32. data/lib/deepl/requests/languages.rb +6 -11
  33. data/lib/deepl/requests/languages_for_resource.rb +2 -0
  34. data/lib/deepl/requests/multilingual_glossary/create.rb +8 -0
  35. data/lib/deepl/requests/multilingual_glossary/destroy_dictionary.rb +4 -0
  36. data/lib/deepl/requests/multilingual_glossary/entries.rb +5 -3
  37. data/lib/deepl/requests/multilingual_glossary/helpers.rb +22 -14
  38. data/lib/deepl/requests/rephrase.rb +3 -1
  39. data/lib/deepl/requests/spoken_terms/create.rb +5 -0
  40. data/lib/deepl/requests/spoken_terms/destroy_term_list.rb +1 -0
  41. data/lib/deepl/requests/spoken_terms/entries.rb +1 -0
  42. data/lib/deepl/requests/spoken_terms/helpers.rb +9 -2
  43. data/lib/deepl/requests/style_profile/create.rb +5 -0
  44. data/lib/deepl/requests/style_profile/list.rb +2 -2
  45. data/lib/deepl/requests/style_rule/create.rb +25 -1
  46. data/lib/deepl/requests/style_rule/create_custom_instruction.rb +7 -0
  47. data/lib/deepl/requests/style_rule/helpers.rb +13 -4
  48. data/lib/deepl/requests/style_rule/list.rb +2 -0
  49. data/lib/deepl/requests/style_rule/update.rb +1 -0
  50. data/lib/deepl/requests/style_rule/update_custom_instruction.rb +2 -0
  51. data/lib/deepl/requests/translate.rb +20 -36
  52. data/lib/deepl/requests/translation_memory/create_export.rb +6 -1
  53. data/lib/deepl/requests/translation_memory/create_import.rb +10 -0
  54. data/lib/deepl/requests/translation_memory/download_export.rb +0 -4
  55. data/lib/deepl/requests/translation_memory/list.rb +2 -2
  56. data/lib/deepl/requests/translation_memory/segments.rb +2 -2
  57. data/lib/deepl/requests/translation_memory/storage_base.rb +5 -9
  58. data/lib/deepl/requests/write_improvements.rb +7 -0
  59. data/lib/deepl/resources/document_handle.rb +1 -26
  60. data/lib/deepl/resources/document_translation_status.rb +0 -12
  61. data/lib/deepl/resources/language.rb +4 -10
  62. data/lib/deepl/resources/multilingual_glossary.rb +4 -4
  63. data/lib/deepl/resources/style_rule.rb +1 -23
  64. data/lib/deepl/resources/translation_memory_job.rb +3 -4
  65. data/lib/deepl/resources/usage.rb +5 -38
  66. data/lib/deepl/utils/client_builder.rb +44 -9
  67. data/lib/deepl/utils/glossary_entries.rb +30 -0
  68. data/lib/deepl/utils/http_client_builder.rb +50 -13
  69. data/lib/deepl/utils/output_path.rb +25 -0
  70. data/lib/deepl/utils/term_validator.rb +3 -3
  71. data/lib/deepl/utils/translation_memory_operations.rb +230 -0
  72. data/lib/deepl.rb +6 -171
  73. data/lib/http_client_options.rb +2 -2
  74. data/lib/version.rb +1 -1
  75. data/upgrading_to_v4.md +374 -0
  76. metadata +9 -50
  77. data/.gitlab-ci.yml +0 -197
  78. data/lib/deepl/deprecation.rb +0 -42
  79. data/lib/deepl/document_api.rb +0 -150
  80. data/lib/deepl/exceptions/not_supported.rb +0 -20
  81. data/lib/deepl/glossary_api.rb +0 -38
  82. data/lib/deepl/style_rule_api.rb +0 -72
  83. data/lib/deepl/translation_memory_api.rb +0 -389
  84. data/license_checker.sh +0 -8
  85. data/spec/fixtures.rb +0 -33
  86. data/spec/offline/admin_client_spec.rb +0 -551
  87. data/spec/offline/client_spec.rb +0 -273
  88. data/spec/offline/configuration_spec.rb +0 -164
  89. data/spec/offline/constants_spec.rb +0 -180
  90. data/spec/offline/correct_text_spec.rb +0 -50
  91. data/spec/offline/deprecation_spec.rb +0 -32
  92. data/spec/offline/errors_spec.rb +0 -92
  93. data/spec/offline/glossaries_spec.rb +0 -98
  94. data/spec/offline/http_client_spec.rb +0 -319
  95. data/spec/offline/languages_spec.rb +0 -155
  96. data/spec/offline/module_methods_spec.rb +0 -13
  97. data/spec/offline/multilingual_glossaries_spec.rb +0 -358
  98. data/spec/offline/offline_helper.rb +0 -71
  99. data/spec/offline/rephrase_text_spec.rb +0 -73
  100. data/spec/offline/spoken_terms_spec.rb +0 -215
  101. data/spec/offline/style_profiles_spec.rb +0 -266
  102. data/spec/offline/style_rules_spec.rb +0 -318
  103. data/spec/offline/translate_document_spec.rb +0 -606
  104. data/spec/offline/translate_text_spec.rb +0 -286
  105. data/spec/offline/translation_memories_client_spec.rb +0 -256
  106. data/spec/offline/translation_memories_spec.rb +0 -463
  107. data/spec/offline/usage_spec.rb +0 -151
  108. data/spec/real_api/admin_client_spec.rb +0 -77
  109. data/spec/real_api/correct_text_spec.rb +0 -20
  110. data/spec/real_api/glossaries_spec.rb +0 -28
  111. data/spec/real_api/languages_spec.rb +0 -27
  112. data/spec/real_api/multilingual_glossaries_spec.rb +0 -53
  113. data/spec/real_api/proxy_spec.rb +0 -41
  114. data/spec/real_api/real_api_helper.rb +0 -65
  115. data/spec/real_api/rephrase_text_spec.rb +0 -16
  116. data/spec/real_api/spoken_terms_spec.rb +0 -56
  117. data/spec/real_api/style_profiles_spec.rb +0 -46
  118. data/spec/real_api/style_rules_spec.rb +0 -46
  119. data/spec/real_api/translate_document_spec.rb +0 -18
  120. data/spec/real_api/translate_text_spec.rb +0 -16
  121. data/spec/real_api/translation_memories_spec.rb +0 -56
  122. data/spec/real_api/usage_spec.rb +0 -20
  123. data/spec/spec_helper.rb +0 -13
@@ -6,19 +6,23 @@
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 enable_watermark].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 = {})
16
- # Options set to nil are unset and not sent.
17
- super(api, options.compact, additional_headers)
18
+ super(api, options, additional_headers)
18
19
  @input_file_path = input_file_path
19
20
  @source_lang = source_lang
20
21
  @target_lang = target_lang
21
22
  @filename = filename
23
+ validate_languages!(source_lang, target_lang)
24
+ validate_translation_memory_threshold!
25
+ validate_glossary_source_lang!
22
26
  end
23
27
 
24
28
  def request
@@ -43,6 +47,18 @@ module DeepL
43
47
 
44
48
  private
45
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
+
46
62
  def build_base_form_data(input_file)
47
63
  form_data = [['file', input_file]]
48
64
  form_data.push(['source_lang', source_lang]) unless source_lang.nil?
@@ -66,7 +82,7 @@ module DeepL
66
82
  # Validates and serializes the `glossary_ids` option (see
67
83
  # `DeepL::Requests::Base#build_glossary_ids_param`) and appends it to the form data as a
68
84
  # comma-separated `glossary_ids` field. Requires `source_lang`, cannot be combined with
69
- # the singular `glossary_id` option, and allows at most 5 IDs.
85
+ # the singular `glossary` option, and allows at most 5 IDs.
70
86
  def add_glossary_ids_to_form(form_data)
71
87
  glossary_ids = build_glossary_ids_param
72
88
  # Text translation detects the source language for glossaries; documents do not.
@@ -88,25 +104,16 @@ module DeepL
88
104
  # field. Mirrors text translation: accepts either a style rule ID string or a
89
105
  # `DeepL::Resources::StyleRule` object.
90
106
  def add_style_rule_to_form(form_data)
91
- return unless option?(:style_rule)
92
-
93
- rule = delete_option(:style_rule)
94
- return if rule.nil?
95
-
96
- style_id = rule.is_a?(DeepL::Resources::StyleRule) ? rule.style_id : rule
97
- form_data.push(['style_id', style_id.to_s])
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?
98
109
  end
99
110
 
100
111
  # Serializes the `style_profile` option (a style profile ID string or a
101
112
  # `DeepL::Resources::StyleProfile` object) as a `style_profile_id` field.
102
113
  def add_style_profile_to_form(form_data)
103
- return unless option?(:style_profile)
104
-
105
- profile = delete_option(:style_profile)
106
- return if profile.nil?
107
-
108
- profile = profile.style_profile_id if profile.is_a?(DeepL::Resources::StyleProfile)
109
- form_data.push(['style_profile_id', profile.to_s])
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?
110
117
  end
111
118
 
112
119
  # Serializes the `translation_memory` option and appends it to the form data as a
@@ -114,13 +121,9 @@ module DeepL
114
121
  # option accepts either a translation memory ID string or a
115
122
  # `DeepL::Resources::TranslationMemory` object.
116
123
  def add_translation_memory_to_form(form_data)
117
- return unless option?(:translation_memory)
118
-
119
- tm = delete_option(:translation_memory)
120
- return if tm.nil?
121
-
122
- tm_id = tm.is_a?(DeepL::Resources::TranslationMemory) ? tm.translation_memory_id : tm
123
- 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?
124
127
  end
125
128
 
126
129
  # Serializes the `translation_memory_threshold` option (integer 0-100) and appends it to
@@ -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: #{loggable_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,19 +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('') do |tsv, entry|
46
- source = Utils::TermValidator.validate!(entry.first)
47
- target = Utils::TermValidator.validate!(entry.last)
48
- "#{tsv}#{source}\t#{target}\n"
49
- end
44
+ def payload
45
+ { name: name, source_lang: source_lang, target_lang: target_lang, entries: entries,
46
+ entries_format: entries_format }
50
47
  end
51
48
 
52
49
  def build_glossary(request, response)
53
- glossary = JSON.parse(response.body)
54
- Resources::Glossary.new(glossary, request, response)
50
+ Resources::Glossary.new(JSON.parse(response.body), request, response)
55
51
  end
56
52
 
57
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
@@ -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
@@ -6,15 +6,10 @@
6
6
  module DeepL
7
7
  module Requests
8
8
  class Languages < Base
9
- SUPPORTED_TYPES = %w[source target].freeze
10
-
11
- def initialize(api, options = {})
12
- super
13
- @type = (delete_option(:type) || :source).to_s
14
- return if SUPPORTED_TYPES.include?(@type)
15
-
16
- raise Exceptions::ValidationError, "Unsupported `type` option: #{@type.inspect} " \
17
- "(supported: #{SUPPORTED_TYPES.join(', ')})"
9
+ # +type+ is +:source+ or +:target+.
10
+ def initialize(api, type)
11
+ super(api, {})
12
+ @type = type
18
13
  end
19
14
 
20
15
  def request
@@ -28,7 +23,7 @@ module DeepL
28
23
  private
29
24
 
30
25
  def target?
31
- @type == 'target'
26
+ @type == :target
32
27
  end
33
28
 
34
29
  def build_languages(request, response)
@@ -42,7 +37,7 @@ module DeepL
42
37
  def build_language(language, request, response)
43
38
  supports_formality = target? ? language.fetch('features', {}).to_h.key?('formality') : nil
44
39
  Resources::Language.new(language['lang'].upcase, language['name'], supports_formality,
45
- request, response, client: api.client?)
40
+ request, response)
46
41
  end
47
42
 
48
43
  def uri
@@ -6,6 +6,8 @@
6
6
  module DeepL
7
7
  module Requests
8
8
  class LanguagesForResource < Base
9
+ accepts_options :include
10
+
9
11
  def initialize(api, resource, options = {})
10
12
  super(api, options)
11
13
  @resource = resource.to_s
@@ -13,6 +13,9 @@ module DeepL
13
13
 
14
14
  def initialize(api, name, dictionaries, options = {})
15
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
+
16
19
  @name = name
17
20
  @dictionaries = dictionaries.map { |dictionary| dictionary_payload(dictionary) }
18
21
  end
@@ -28,6 +31,11 @@ module DeepL
28
31
 
29
32
  private
30
33
 
34
+ # A repeated creation request could create a second resource.
35
+ def retry_only_unprocessed?
36
+ true
37
+ end
38
+
31
39
  def path
32
40
  'glossaries'
33
41
  end
@@ -14,6 +14,10 @@ module DeepL
14
14
  def initialize(api, id, source_lang, target_lang, options = {})
15
15
  super(api, options)
16
16
  @id = glossary_id(id)
17
+ if source_lang.to_s.empty? || target_lang.to_s.empty?
18
+ raise ArgumentError, 'source_lang and target_lang must not be empty'
19
+ end
20
+
17
21
  @source_lang = source_lang
18
22
  @target_lang = target_lang
19
23
  end
@@ -14,6 +14,7 @@ module DeepL
14
14
  def initialize(api, id, source_lang, target_lang, options = {})
15
15
  super(api, options)
16
16
  @id = glossary_id(id)
17
+ validate_dictionary_languages!(source_lang, target_lang)
17
18
  @source_lang = source_lang
18
19
  @target_lang = target_lang
19
20
  end
@@ -33,13 +34,14 @@ module DeepL
33
34
  Net::HTTP::Get.new(uri.request_uri, http_headers)
34
35
  end
35
36
 
36
- # The API wraps the dictionary in a list, which is empty if the glossary has no
37
- # dictionary for the language pair.
37
+ # The API wraps the dictionary in a list and answers a missing dictionary with 404. If it
38
+ # answers 200 with an empty list instead, that is no failed response, so the NotFound
39
+ # carries none and has no HTTP status.
38
40
  def build_entries(request, response)
39
41
  dictionary = (JSON.parse(response.body)['dictionaries'] || []).first
40
42
  if dictionary.nil?
41
43
  raise Exceptions::NotFound.new(
42
- response, request,
44
+ nil, request,
43
45
  "Glossary #{id} has no dictionary for #{source_lang} -> #{target_lang}"
44
46
  )
45
47
  end
@@ -11,9 +11,10 @@ module DeepL
11
11
  private
12
12
 
13
13
  def glossary_id(glossary)
14
- return glossary.id if glossary.is_a?(DeepL::Resources::MultilingualGlossary)
14
+ id = glossary.is_a?(DeepL::Resources::MultilingualGlossary) ? glossary.id : glossary
15
+ raise ArgumentError, 'Glossary ID must not be empty' if id.to_s.empty?
15
16
 
16
- glossary.to_s
17
+ id.to_s
17
18
  end
18
19
 
19
20
  # The path of a glossary, with its ID URL-encoded.
@@ -26,28 +27,35 @@ module DeepL
26
27
  # string is sent as CSV; all other entries are converted and sent as TSV.
27
28
  def dictionary_payload(dictionary)
28
29
  if dictionary.is_a?(DeepL::Resources::MultilingualGlossaryDictionaryEntries)
29
- return { source_lang: dictionary.source_lang, target_lang: dictionary.target_lang,
30
- entries: entries_to_string(dictionary.entries), entries_format: 'tsv' }
30
+ return entries_object_payload(dictionary)
31
31
  end
32
32
 
33
33
  value = ->(key) { dictionary[key] || dictionary[key.to_s] }
34
+ validate_dictionary_languages!(value.call(:source_lang), value.call(:target_lang))
34
35
  entries = value.call(:entries)
35
36
  # CSV content is sent as given; TSV content is parsed so its terms are checked too.
36
37
  csv = value.call(:entries_format) == 'csv' && entries.is_a?(String)
37
38
  { source_lang: value.call(:source_lang), target_lang: value.call(:target_lang),
38
- entries: csv ? entries : entries_to_string(entries),
39
+ entries: csv ? entries : non_empty_tsv(entries),
39
40
  entries_format: csv ? 'csv' : 'tsv' }
40
41
  end
41
42
 
42
- # A hash, source/target pairs or a TSV string of them, checked and joined as TSV.
43
- def entries_to_string(entries)
44
- entries_class = Resources::MultilingualGlossaryDictionaryEntries
45
- entries = entries.is_a?(String) ? entries_class.tsv_to_pairs(entries) : entries.to_a
46
- entries_class.check_pairs!(entries).map do |entry|
47
- source = Utils::TermValidator.validate!(entry.first)
48
- target = Utils::TermValidator.validate!(entry.last)
49
- "#{source}\t#{target}"
50
- end.join("\n")
43
+ def entries_object_payload(dictionary)
44
+ validate_dictionary_languages!(dictionary.source_lang, dictionary.target_lang)
45
+ { source_lang: dictionary.source_lang, target_lang: dictionary.target_lang,
46
+ entries: non_empty_tsv(dictionary.entries), entries_format: 'tsv' }
47
+ end
48
+
49
+ def validate_dictionary_languages!(source_lang, target_lang)
50
+ validate_language!('source_lang', source_lang)
51
+ validate_language!('target_lang', target_lang)
52
+ end
53
+
54
+ def non_empty_tsv(entries)
55
+ tsv = Utils::GlossaryEntries.to_tsv(entries || [])
56
+ raise ArgumentError, 'Glossary dictionary entries must not be empty' if tsv.empty?
57
+
58
+ tsv
51
59
  end
52
60
 
53
61
  def build_glossary(request, response)
@@ -17,6 +17,8 @@ module DeepL
17
17
  @target_lang = target_lang
18
18
  @writing_style = writing_style
19
19
  @tone = tone
20
+ validate_text!(text)
21
+ validate_optional_target_lang!(target_lang)
20
22
  end
21
23
 
22
24
  def request # rubocop:disable Metrics/AbcSize
@@ -41,7 +43,7 @@ module DeepL
41
43
 
42
44
  def build_texts(request, response)
43
45
  texts = build_improvements(text.is_a?(Array) ? text.size : 1, request, response)
44
- texts.size == 1 ? texts.first : texts
46
+ text.is_a?(Array) ? texts : texts.first
45
47
  end
46
48
 
47
49
  def path
@@ -28,6 +28,11 @@ module DeepL
28
28
 
29
29
  private
30
30
 
31
+ # A repeated creation request could create a second resource.
32
+ def retry_only_unprocessed?
33
+ true
34
+ end
35
+
31
36
  def path
32
37
  'spoken-terms'
33
38
  end
@@ -14,6 +14,7 @@ module DeepL
14
14
  def initialize(api, id, lang)
15
15
  super(api)
16
16
  @id = spoken_terms_id(id)
17
+ validate_language!('lang', lang)
17
18
  @lang = lang
18
19
  end
19
20
 
@@ -14,6 +14,7 @@ module DeepL
14
14
  def initialize(api, id, lang)
15
15
  super(api)
16
16
  @id = spoken_terms_id(id)
17
+ validate_language!('lang', lang)
17
18
  @lang = lang
18
19
  end
19
20
 
@@ -12,9 +12,14 @@ module DeepL
12
12
  private
13
13
 
14
14
  def spoken_terms_id(spoken_terms)
15
- return spoken_terms.spoken_terms_id if spoken_terms.is_a?(DeepL::Resources::SpokenTerms)
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?
16
21
 
17
- spoken_terms.to_s
22
+ id.to_s
18
23
  end
19
24
 
20
25
  # The path of a collection, with its ID URL-encoded.
@@ -42,6 +47,8 @@ module DeepL
42
47
  [term_list[:lang] || term_list['lang'],
43
48
  term_list[:entries] || term_list['entries']]
44
49
  end
50
+ validate_language!('lang', lang)
51
+
45
52
  { lang: lang, entries: entries_to_string(entries) }
46
53
  end
47
54
 
@@ -26,6 +26,11 @@ module DeepL
26
26
 
27
27
  private
28
28
 
29
+ # A repeated creation request could create a second resource.
30
+ def retry_only_unprocessed?
31
+ true
32
+ end
33
+
29
34
  def path
30
35
  'style_profiles'
31
36
  end
@@ -7,7 +7,7 @@ module DeepL
7
7
  module Requests
8
8
  module StyleProfile
9
9
  class List < Base
10
- SUPPORTED_OPTIONS = %w[page page_size].freeze
10
+ accepts_options :page, :page_size
11
11
 
12
12
  def request
13
13
  build_style_profile_list(*execute_request_with_retries(get_request))
@@ -29,7 +29,7 @@ module DeepL
29
29
  end
30
30
 
31
31
  def query_params
32
- SUPPORTED_OPTIONS.each_with_object({}) do |option_name, params|
32
+ self.class.accepted_options.each_with_object({}) do |option_name, params|
33
33
  params[option_name] = option(option_name).to_s if option?(option_name)
34
34
  end
35
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
- super(api, options.dup)
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)