deepl-rb 3.3.0 → 3.5.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 (97) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -2
  3. data/README.md +72 -0
  4. metadata +2 -95
  5. data/.circleci/config.yml +0 -27
  6. data/.github/workflows/add_issues_to_kanban.yml +0 -16
  7. data/.gitlab-ci.yml +0 -172
  8. data/.rubocop.yml +0 -51
  9. data/CODE_OF_CONDUCT.md +0 -132
  10. data/CONTRIBUTING.md +0 -37
  11. data/Gemfile +0 -24
  12. data/Rakefile +0 -54
  13. data/SECURITY.md +0 -58
  14. data/VERSION +0 -1
  15. data/deepl-rb.gemspec +0 -133
  16. data/lib/deepl/api.rb +0 -22
  17. data/lib/deepl/configuration.rb +0 -59
  18. data/lib/deepl/constants/base_constant.rb +0 -18
  19. data/lib/deepl/constants/formality.rb +0 -16
  20. data/lib/deepl/constants/model_type.rb +0 -14
  21. data/lib/deepl/constants/split_sentences.rb +0 -14
  22. data/lib/deepl/constants/tag_handling.rb +0 -13
  23. data/lib/deepl/constants/tone.rb +0 -20
  24. data/lib/deepl/constants/writing_style.rb +0 -20
  25. data/lib/deepl/document_api.rb +0 -121
  26. data/lib/deepl/exceptions/authorization_failed.rb +0 -14
  27. data/lib/deepl/exceptions/bad_request.rb +0 -16
  28. data/lib/deepl/exceptions/document_translation_error.rb +0 -15
  29. data/lib/deepl/exceptions/error.rb +0 -14
  30. data/lib/deepl/exceptions/limit_exceeded.rb +0 -18
  31. data/lib/deepl/exceptions/not_found.rb +0 -16
  32. data/lib/deepl/exceptions/not_supported.rb +0 -14
  33. data/lib/deepl/exceptions/quota_exceeded.rb +0 -14
  34. data/lib/deepl/exceptions/request_entity_too_large.rb +0 -15
  35. data/lib/deepl/exceptions/request_error.rb +0 -21
  36. data/lib/deepl/exceptions/server_error.rb +0 -18
  37. data/lib/deepl/glossary_api.rb +0 -38
  38. data/lib/deepl/requests/base.rb +0 -196
  39. data/lib/deepl/requests/document/download.rb +0 -44
  40. data/lib/deepl/requests/document/get_status.rb +0 -44
  41. data/lib/deepl/requests/document/upload.rb +0 -74
  42. data/lib/deepl/requests/glossary/create.rb +0 -59
  43. data/lib/deepl/requests/glossary/destroy.rb +0 -37
  44. data/lib/deepl/requests/glossary/entries.rb +0 -37
  45. data/lib/deepl/requests/glossary/find.rb +0 -38
  46. data/lib/deepl/requests/glossary/language_pairs.rb +0 -38
  47. data/lib/deepl/requests/glossary/list.rb +0 -37
  48. data/lib/deepl/requests/languages.rb +0 -37
  49. data/lib/deepl/requests/rephrase.rb +0 -55
  50. data/lib/deepl/requests/translate.rb +0 -82
  51. data/lib/deepl/requests/usage.rb +0 -33
  52. data/lib/deepl/resources/base.rb +0 -17
  53. data/lib/deepl/resources/document_handle.rb +0 -57
  54. data/lib/deepl/resources/document_translation_status.rb +0 -54
  55. data/lib/deepl/resources/glossary.rb +0 -28
  56. data/lib/deepl/resources/language.rb +0 -30
  57. data/lib/deepl/resources/language_pair.rb +0 -23
  58. data/lib/deepl/resources/text.rb +0 -24
  59. data/lib/deepl/resources/usage.rb +0 -27
  60. data/lib/deepl/utils/backoff_timer.rb +0 -46
  61. data/lib/deepl/utils/exception_builder.rb +0 -34
  62. data/lib/deepl.rb +0 -147
  63. data/lib/http_client_options.rb +0 -22
  64. data/spec/api/api_spec.rb +0 -20
  65. data/spec/api/configuration_spec.rb +0 -122
  66. data/spec/api/deepl_spec.rb +0 -460
  67. data/spec/constants/constants_spec.rb +0 -158
  68. data/spec/fixtures/vcr_cassettes/deepl_document.yml +0 -95
  69. data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +0 -1214
  70. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +0 -1163
  71. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +0 -54
  72. data/spec/fixtures/vcr_cassettes/deepl_rephrase.yml +0 -87
  73. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +0 -358
  74. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +0 -129
  75. data/spec/fixtures/vcr_cassettes/glossaries.yml +0 -1702
  76. data/spec/fixtures/vcr_cassettes/languages.yml +0 -229
  77. data/spec/fixtures/vcr_cassettes/rephrase_texts.yml +0 -401
  78. data/spec/fixtures/vcr_cassettes/translate_texts.yml +0 -10415
  79. data/spec/fixtures/vcr_cassettes/usage.yml +0 -171
  80. data/spec/integration_tests/document_api_spec.rb +0 -177
  81. data/spec/integration_tests/integration_test_utils.rb +0 -170
  82. data/spec/requests/glossary/create_spec.rb +0 -65
  83. data/spec/requests/glossary/destroy_spec.rb +0 -66
  84. data/spec/requests/glossary/entries_spec.rb +0 -62
  85. data/spec/requests/glossary/find_spec.rb +0 -68
  86. data/spec/requests/glossary/language_pairs_spec.rb +0 -40
  87. data/spec/requests/glossary/list_spec.rb +0 -54
  88. data/spec/requests/languages_spec.rb +0 -68
  89. data/spec/requests/rephrase_spec.rb +0 -172
  90. data/spec/requests/translate_spec.rb +0 -422
  91. data/spec/requests/usage_spec.rb +0 -43
  92. data/spec/resources/glossary_spec.rb +0 -38
  93. data/spec/resources/language_pair_spec.rb +0 -23
  94. data/spec/resources/language_spec.rb +0 -45
  95. data/spec/resources/text_spec.rb +0 -23
  96. data/spec/resources/usage_spec.rb +0 -35
  97. data/spec/spec_helper.rb +0 -92
@@ -1,16 +0,0 @@
1
- # Copyright 2018 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Exceptions
8
- class BadRequest < RequestError
9
- def to_s
10
- JSON.parse(response.body)['message']
11
- rescue JSON::ParserError
12
- response.body
13
- end
14
- end
15
- end
16
- end
@@ -1,15 +0,0 @@
1
- # Copyright 2024 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 Exceptions
8
- class DocumentTranslationError < Error
9
- def initialize(message, handle)
10
- super(message)
11
- @handle = handle
12
- end
13
- end
14
- end
15
- end
@@ -1,14 +0,0 @@
1
- # Copyright 2018 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Exceptions
8
- class Error < StandardError
9
- def should_retry?
10
- false
11
- end
12
- end
13
- end
14
- end
@@ -1,18 +0,0 @@
1
- # Copyright 2018 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Exceptions
8
- class LimitExceeded < RequestError
9
- def to_s
10
- 'Limit exceeded. Please wait and send your request once again.'
11
- end
12
-
13
- def should_retry?
14
- true
15
- end
16
- end
17
- end
18
- end
@@ -1,16 +0,0 @@
1
- # Copyright 2022 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Exceptions
8
- class NotFound < RequestError
9
- def to_s
10
- JSON.parse(response.body)['message']
11
- rescue JSON::ParserError
12
- response.body
13
- end
14
- end
15
- end
16
- end
@@ -1,14 +0,0 @@
1
- # Copyright 2022 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Exceptions
8
- class NotSupported < Error
9
- def initialize(msg = 'The feature is not supported')
10
- super
11
- end
12
- end
13
- end
14
- end
@@ -1,14 +0,0 @@
1
- # Copyright 2018 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Exceptions
8
- class QuotaExceeded < RequestError
9
- def to_s
10
- 'Quota exceeded. The character limit has been reached.'
11
- end
12
- end
13
- end
14
- end
@@ -1,15 +0,0 @@
1
- # Copyright 2022 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Exceptions
8
- class RequestEntityTooLarge < RequestError
9
- def to_s
10
- 'The request size has reached the supported limit. ' \
11
- "Make sure that you're not sending more than 50 text parts."
12
- end
13
- end
14
- end
15
- end
@@ -1,21 +0,0 @@
1
- # Copyright 2018 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Exceptions
8
- class RequestError < Error
9
- attr_reader :request, :response
10
-
11
- def initialize(response)
12
- super()
13
- @response = response
14
- end
15
-
16
- def to_s
17
- 'Unknown error.'
18
- end
19
- end
20
- end
21
- end
@@ -1,18 +0,0 @@
1
- # Copyright 2024 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 Exceptions
8
- class ServerError < RequestError
9
- def to_s
10
- 'An internal server error occured. Try again after waiting a short period.'
11
- end
12
-
13
- def should_retry?
14
- true
15
- end
16
- end
17
- end
18
- end
@@ -1,38 +0,0 @@
1
- # Copyright 2022 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- class GlossaryApi
8
- def initialize(api, options = {})
9
- @api = api
10
- @options = options
11
- end
12
-
13
- def create(name, source_lang, target_lang, entries, options = {})
14
- DeepL::Requests::Glossary::Create.new(@api, name, source_lang, target_lang, entries, options)
15
- .request
16
- end
17
-
18
- def destroy(glossary_id, options = {})
19
- DeepL::Requests::Glossary::Destroy.new(@api, glossary_id, options).request
20
- end
21
-
22
- def entries(glossary_id, options = {})
23
- DeepL::Requests::Glossary::Entries.new(@api, glossary_id, options).request
24
- end
25
-
26
- def find(glossary_id, options = {})
27
- DeepL::Requests::Glossary::Find.new(@api, glossary_id, options).request
28
- end
29
-
30
- def language_pairs(options = {})
31
- DeepL::Requests::Glossary::LanguagePairs.new(@api, options).request
32
- end
33
-
34
- def list(options = {})
35
- DeepL::Requests::Glossary::List.new(@api, options).request
36
- end
37
- end
38
- end
@@ -1,196 +0,0 @@
1
- # Copyright 2018 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Requests
8
- class Base # rubocop:disable Metrics/ClassLength
9
- attr_reader :api, :response, :options
10
-
11
- def initialize(api, options = {}, additional_headers = {})
12
- @api = api
13
- @options = options
14
- @additional_headers = additional_headers
15
- @num_retries = 0
16
- @backoff_timer = Utils::BackoffTimer.new
17
- end
18
-
19
- def request
20
- raise NotImplementedError
21
- end
22
-
23
- def details
24
- "HTTP Headers #{headers}"
25
- end
26
-
27
- private
28
-
29
- def option?(name)
30
- options.key?(name.to_s) || options.key?(name.to_sym)
31
- end
32
-
33
- def option(name)
34
- options[name.to_s] || options[name.to_sym]
35
- end
36
-
37
- def delete_option(name)
38
- options.delete(name.to_s) || options.delete(name.to_sym)
39
- end
40
-
41
- def set_option(name, value)
42
- if options.key?(name.to_sym)
43
- options[name.to_sym] = value
44
- else
45
- options[name.to_s] = value
46
- end
47
- end
48
-
49
- # Returns options excluding extra_body_parameters
50
- # extra_body_parameters are applied separately via apply_extra_body_parameters_* methods
51
- def options_without_extra_params
52
- options.reject do |k, _|
53
- k.to_s == 'extra_body_parameters' || k.to_sym == :extra_body_parameters
54
- end
55
- end
56
-
57
- # Apply extra body parameters to a hash payload (for JSON/form-encoded requests)
58
- # Extra parameters will override existing keys
59
- def apply_extra_body_parameters_to_json(payload)
60
- return unless option?(:extra_body_parameters)
61
-
62
- extra_params = option(:extra_body_parameters)
63
- extra_params.each do |key, value|
64
- payload[key] = value.to_s
65
- end
66
- end
67
-
68
- # Apply extra body parameters to multipart form data array
69
- # Extra parameters will override existing keys
70
- def apply_extra_body_parameters_to_form(form_data)
71
- return unless option?(:extra_body_parameters)
72
-
73
- extra_params = option(:extra_body_parameters)
74
- extra_params.each do |key, value|
75
- key_str = key.to_s
76
- # Remove existing key if present to allow override.
77
- # Required for form data as it would be duplicated otherwise
78
- form_data.reject! { |field| field[0].to_s == key_str }
79
- form_data.push([key_str, value.to_s])
80
- end
81
- end
82
-
83
- # Files to reset: list of file objects to rewind when retrying the request
84
- def execute_request_with_retries(req, files_to_reset = []) # rubocop:disable all
85
- api.configuration.logger&.info("Request to the DeepL API: #{self}")
86
- api.configuration.logger&.debug("Request details: #{details}")
87
- loop do
88
- resp = api.http_client.request(req)
89
- validate_response!(resp)
90
- return [req, resp]
91
- rescue DeepL::Exceptions::Error => e
92
- raise e unless should_retry?(resp, e, @backoff_timer.num_retries)
93
-
94
- unless e.nil?
95
- api.configuration.logger&.info("Encountered a retryable exception: #{e.message}")
96
- end
97
- api.configuration.logger&.info("Starting retry #{@backoff_timer.num_retries + 1} for " \
98
- "request #{request} after sleeping for " \
99
- "#{format('%.2f', @backoff_timer.time_until_deadline)}")
100
- files_to_reset.each(&:rewind)
101
- @backoff_timer.sleep_until_deadline
102
- next
103
- rescue Net::HTTPBadResponse, Net::HTTPServerError, Net::HTTPFatalError, Timeout::Error,
104
- SocketError => e
105
- unless e.nil?
106
- api.configuration.logger&.info("Encountered a retryable exception: #{e.message}")
107
- end
108
- api.configuration.logger&.info("Starting retry #{@backoff_timer.num_retries + 1} for " \
109
- "request #{request} after sleeping for " \
110
- "#{format('%.2f', @backoff_timer.time_until_deadline)}")
111
- files_to_reset.each(&:rewind)
112
- @backoff_timer.sleep_until_deadline
113
- next
114
- end
115
- end
116
-
117
- def should_retry?(response, exception, num_retries)
118
- return false if num_retries >= api.configuration.max_network_retries
119
- return exception.should_retry? if response.nil?
120
-
121
- response.is_a?(Net::HTTPTooManyRequests) || response.is_a?(Net::HTTPInternalServerError)
122
- end
123
-
124
- def post_request(payload)
125
- apply_extra_body_parameters_to_json(payload)
126
- http_headers = add_json_content_type(headers)
127
- post_req = Net::HTTP::Post.new(uri.path, http_headers)
128
- post_req.body = payload.merge(options_without_extra_params).to_json
129
- post_req
130
- end
131
-
132
- def post_request_with_file(form_data)
133
- http_headers = add_multipart_form_content_type(headers)
134
- post_req = Net::HTTP::Post.new(uri.request_uri, http_headers)
135
- post_req.set_form(form_data, 'multipart/form-data')
136
- post_req
137
- end
138
-
139
- def get_request # rubocop:disable Naming/AccessorMethodName
140
- http_headers = add_json_content_type(headers)
141
- get_req = Net::HTTP::Get.new(uri.path, http_headers)
142
- get_req.body = options_without_extra_params.to_json
143
- get_req
144
- end
145
-
146
- def delete_request
147
- http_headers = add_json_content_type(headers)
148
- del_req = Net::HTTP::Delete.new(uri.path, http_headers)
149
- del_req.body = options_without_extra_params.to_json
150
- del_req
151
- end
152
-
153
- def validate_response!(response)
154
- return if response.is_a?(Net::HTTPSuccess)
155
-
156
- raise Utils::ExceptionBuilder.new(response).build
157
- end
158
-
159
- def path
160
- raise NotImplementedError
161
- end
162
-
163
- def url
164
- "#{host}/#{api.configuration.version}/#{path}"
165
- end
166
-
167
- def uri
168
- @uri ||= URI(url)
169
- @uri
170
- end
171
-
172
- def host
173
- api.configuration.host
174
- end
175
-
176
- def query_params
177
- options
178
- end
179
-
180
- def headers
181
- { 'Authorization' => "DeepL-Auth-Key #{api.configuration.auth_key}",
182
- 'User-Agent' => api.configuration.user_agent }.merge(@additional_headers)
183
- end
184
-
185
- def add_json_content_type(headers_to_add_to)
186
- headers_to_add_to['Content-Type'] = 'application/json'
187
- headers_to_add_to
188
- end
189
-
190
- def add_multipart_form_content_type(headers_to_add_to)
191
- headers_to_add_to['Content-Type'] = 'multipart/form-data'
192
- headers_to_add_to
193
- end
194
- end
195
- end
196
- end
@@ -1,44 +0,0 @@
1
- # Copyright 2024 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
- module Document
9
- class Download < Base
10
- attr_reader :document_id, :document_key
11
-
12
- def initialize(api, document_id, document_key, output_file)
13
- super(api, {})
14
- @document_id = document_id
15
- @document_key = document_key
16
- @output_file = output_file
17
- end
18
-
19
- def request
20
- payload = { document_key: document_key }
21
- extract_file(*execute_request_with_retries(post_request(payload)))
22
- end
23
-
24
- def details
25
- "HTTP Headers: #{headers}\nPayload #{{ document_key: document_key }}"
26
- end
27
-
28
- def to_s
29
- "POST #{uri.request_uri}"
30
- end
31
-
32
- private
33
-
34
- def extract_file(_request, response)
35
- File.write(@output_file, response.body)
36
- end
37
-
38
- def path
39
- "document/#{document_id}/result"
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,44 +0,0 @@
1
- # Copyright 2024 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
- module Document
9
- class GetStatus < Base
10
- attr_reader :document_id, :document_key
11
-
12
- def initialize(api, document_id, document_key, options = {}, additional_headers = {})
13
- super(api, options, additional_headers)
14
- @document_id = document_id
15
- @document_key = document_key
16
- end
17
-
18
- def request
19
- payload = { document_key: document_key }
20
- build_doc_translation_status(*execute_request_with_retries(post_request(payload)))
21
- end
22
-
23
- def details
24
- "HTTP Headers: #{headers}\nPayload #{{ document_key: document_key }}"
25
- end
26
-
27
- def to_s
28
- "POST #{uri.request_uri}"
29
- end
30
-
31
- private
32
-
33
- def build_doc_translation_status(request, response)
34
- document_translation_status = JSON.parse(response.body)
35
- Resources::DocumentTranslationStatus.new(document_translation_status, request, response)
36
- end
37
-
38
- def path
39
- "document/#{document_id}"
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,74 +0,0 @@
1
- # Copyright 2024 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
- module Document
9
- class Upload < Base
10
- attr_reader :input_file_path, :source_lang, :target_lang, :filename
11
-
12
- SUPPORTED_OPTIONS = %w[formality glossary_id output_format].freeze
13
-
14
- def initialize(api, input_file_path, source_lang, target_lang, filename = nil, # rubocop:disable Metrics/ParameterLists
15
- options = {}, additional_headers = {})
16
- super(api, options, additional_headers)
17
- @input_file_path = input_file_path
18
- @source_lang = source_lang
19
- @target_lang = target_lang
20
- @filename = filename
21
- end
22
-
23
- def request
24
- input_file = File.open(input_file_path, 'rb')
25
- form_data = build_base_form_data(input_file)
26
- apply_extra_body_parameters_to_form(form_data)
27
- build_doc_handle(*execute_request_with_retries(post_request_with_file(form_data),
28
- [input_file]))
29
- end
30
-
31
- def details
32
- "HTTP Headers: #{headers}\nPayload #{[
33
- ['file', "File at #{input_file_path} opened in binary mode"],
34
- ['source_lang', source_lang], ['target_lang', target_lang], ['filename', filename]
35
- ]}"
36
- end
37
-
38
- def to_s
39
- "POST #{uri.request_uri}"
40
- end
41
-
42
- private
43
-
44
- def build_base_form_data(input_file)
45
- form_data = [
46
- ['file', input_file], ['source_lang', source_lang],
47
- ['target_lang', target_lang]
48
- ]
49
- filename_param = filename || File.basename(input_file_path)
50
- form_data.push(['filename', filename_param]) unless filename_param.nil?
51
- add_supported_options_to_form(form_data)
52
- form_data
53
- end
54
-
55
- def add_supported_options_to_form(form_data)
56
- SUPPORTED_OPTIONS.each do |option_name|
57
- option_value = option(option_name)
58
- form_data.push([option_name, option_value]) unless option_value.nil?
59
- end
60
- end
61
-
62
- def build_doc_handle(request, response)
63
- parsed_response = JSON.parse(response.body)
64
- Resources::DocumentHandle.new(parsed_response['document_id'],
65
- parsed_response['document_key'], request, response)
66
- end
67
-
68
- def path
69
- 'document'
70
- end
71
- end
72
- end
73
- end
74
- end
@@ -1,59 +0,0 @@
1
- # Copyright 2022 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Requests
8
- module Glossary
9
- class Create < Base
10
- attr_reader :name, :source_lang, :target_lang, :entries, :entries_format
11
-
12
- def initialize(api, name, source_lang, target_lang, entries, options = {})
13
- super(api, options)
14
- @name = name
15
- @source_lang = source_lang
16
- @target_lang = target_lang
17
- @entries = entries
18
- @entries_format = delete_option(:entries_format) || 'tsv'
19
- end
20
-
21
- 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
- build_glossary(*execute_request_with_retries(post_request(payload)))
27
- end
28
-
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
- }}"
34
- end
35
-
36
- def to_s
37
- "POST #{uri.request_uri}"
38
- end
39
-
40
- private
41
-
42
- def entries_to_tsv
43
- return entries if entries.is_a?(String)
44
-
45
- entries.reduce('') { |tsv, entry| "#{tsv}#{entry.first}\t#{entry.last}\n" }
46
- end
47
-
48
- def build_glossary(request, response)
49
- glossary = JSON.parse(response.body)
50
- Resources::Glossary.new(glossary, request, response)
51
- end
52
-
53
- def path
54
- 'glossaries'
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,37 +0,0 @@
1
- # Copyright 2022 Daniel Herzog
2
- # Use of this source code is governed by an MIT
3
- # license that can be found in the LICENSE.md file.
4
- # frozen_string_literal: true
5
-
6
- module DeepL
7
- module Requests
8
- module Glossary
9
- class Destroy < Base
10
- attr_reader :id
11
-
12
- def initialize(api, id, options = {})
13
- super(api, options)
14
- @id = id
15
- end
16
-
17
- def request
18
- build_response(*execute_request_with_retries(delete_request))
19
- end
20
-
21
- def to_s
22
- "DELETE #{uri.request_uri}"
23
- end
24
-
25
- private
26
-
27
- def build_response(_, _)
28
- id
29
- end
30
-
31
- def path
32
- "glossaries/#{id}"
33
- end
34
- end
35
- end
36
- end
37
- end