deepl-rb 3.6.0 → 3.7.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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +27 -0
  3. data/.github/workflows/add_issues_to_kanban.yml +16 -0
  4. data/.gitlab-ci.yml +177 -0
  5. data/.rubocop.yml +51 -0
  6. data/CHANGELOG.md +40 -3
  7. data/CODE_OF_CONDUCT.md +132 -0
  8. data/CONTRIBUTING.md +37 -0
  9. data/Gemfile +24 -0
  10. data/README.md +153 -22
  11. data/Rakefile +58 -0
  12. data/SECURITY.md +58 -0
  13. data/VERSION +1 -0
  14. data/deepl-rb.gemspec +167 -0
  15. data/lib/deepl/api.rb +22 -0
  16. data/lib/deepl/configuration.rb +59 -0
  17. data/lib/deepl/constants/base_constant.rb +18 -0
  18. data/lib/deepl/constants/formality.rb +16 -0
  19. data/lib/deepl/constants/model_type.rb +14 -0
  20. data/lib/deepl/constants/split_sentences.rb +14 -0
  21. data/lib/deepl/constants/tag_handling.rb +13 -0
  22. data/lib/deepl/constants/tone.rb +20 -0
  23. data/lib/deepl/constants/writing_style.rb +20 -0
  24. data/lib/deepl/document_api.rb +121 -0
  25. data/lib/deepl/exceptions/authorization_failed.rb +14 -0
  26. data/lib/deepl/exceptions/bad_request.rb +16 -0
  27. data/lib/deepl/exceptions/document_translation_error.rb +15 -0
  28. data/lib/deepl/exceptions/error.rb +14 -0
  29. data/lib/deepl/exceptions/limit_exceeded.rb +18 -0
  30. data/lib/deepl/exceptions/not_found.rb +16 -0
  31. data/lib/deepl/exceptions/not_supported.rb +14 -0
  32. data/lib/deepl/exceptions/quota_exceeded.rb +14 -0
  33. data/lib/deepl/exceptions/request_entity_too_large.rb +15 -0
  34. data/lib/deepl/exceptions/request_error.rb +21 -0
  35. data/lib/deepl/exceptions/server_error.rb +18 -0
  36. data/lib/deepl/glossary_api.rb +38 -0
  37. data/lib/deepl/requests/base.rb +212 -0
  38. data/lib/deepl/requests/document/download.rb +44 -0
  39. data/lib/deepl/requests/document/get_status.rb +44 -0
  40. data/lib/deepl/requests/document/upload.rb +75 -0
  41. data/lib/deepl/requests/glossary/create.rb +59 -0
  42. data/lib/deepl/requests/glossary/destroy.rb +37 -0
  43. data/lib/deepl/requests/glossary/entries.rb +37 -0
  44. data/lib/deepl/requests/glossary/find.rb +38 -0
  45. data/lib/deepl/requests/glossary/language_pairs.rb +38 -0
  46. data/lib/deepl/requests/glossary/list.rb +37 -0
  47. data/lib/deepl/requests/languages.rb +37 -0
  48. data/lib/deepl/requests/rephrase.rb +55 -0
  49. data/lib/deepl/requests/style_rule/create.rb +46 -0
  50. data/lib/deepl/requests/style_rule/create_custom_instruction.rb +45 -0
  51. data/lib/deepl/requests/style_rule/destroy.rb +39 -0
  52. data/lib/deepl/requests/style_rule/destroy_custom_instruction.rb +40 -0
  53. data/lib/deepl/requests/style_rule/find.rb +40 -0
  54. data/lib/deepl/requests/style_rule/find_custom_instruction.rb +41 -0
  55. data/lib/deepl/requests/style_rule/list.rb +59 -0
  56. data/lib/deepl/requests/style_rule/update.rb +42 -0
  57. data/lib/deepl/requests/style_rule/update_configured_rules.rb +41 -0
  58. data/lib/deepl/requests/style_rule/update_custom_instruction.rb +47 -0
  59. data/lib/deepl/requests/translate.rb +106 -0
  60. data/lib/deepl/requests/translation_memory/list.rb +58 -0
  61. data/lib/deepl/requests/usage.rb +33 -0
  62. data/lib/deepl/resources/base.rb +17 -0
  63. data/lib/deepl/resources/document_handle.rb +57 -0
  64. data/lib/deepl/resources/document_translation_status.rb +54 -0
  65. data/lib/deepl/resources/glossary.rb +28 -0
  66. data/lib/deepl/resources/language.rb +30 -0
  67. data/lib/deepl/resources/language_pair.rb +23 -0
  68. data/lib/deepl/resources/style_rule.rb +86 -0
  69. data/lib/deepl/resources/text.rb +24 -0
  70. data/lib/deepl/resources/translation_memory.rb +25 -0
  71. data/lib/deepl/resources/usage.rb +27 -0
  72. data/lib/deepl/style_rule_api.rb +61 -0
  73. data/lib/deepl/translation_memory_api.rb +17 -0
  74. data/lib/deepl/utils/backoff_timer.rb +46 -0
  75. data/lib/deepl/utils/exception_builder.rb +34 -0
  76. data/lib/deepl.rb +175 -0
  77. data/lib/http_client_options.rb +22 -0
  78. data/lib/version.rb +8 -0
  79. data/spec/api/api_spec.rb +20 -0
  80. data/spec/api/configuration_spec.rb +122 -0
  81. data/spec/api/deepl_spec.rb +460 -0
  82. data/spec/constants/constants_spec.rb +158 -0
  83. data/spec/fixtures/vcr_cassettes/deepl_document.yml +95 -0
  84. data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +1214 -0
  85. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +1163 -0
  86. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +54 -0
  87. data/spec/fixtures/vcr_cassettes/deepl_rephrase.yml +87 -0
  88. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +358 -0
  89. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +129 -0
  90. data/spec/fixtures/vcr_cassettes/glossaries.yml +1702 -0
  91. data/spec/fixtures/vcr_cassettes/languages.yml +229 -0
  92. data/spec/fixtures/vcr_cassettes/rephrase_texts.yml +401 -0
  93. data/spec/fixtures/vcr_cassettes/style_rules.yml +92 -0
  94. data/spec/fixtures/vcr_cassettes/style_rules_crud.yml +926 -0
  95. data/spec/fixtures/vcr_cassettes/translate_texts.yml +10630 -0
  96. data/spec/fixtures/vcr_cassettes/translation_memories.yml +74 -0
  97. data/spec/fixtures/vcr_cassettes/usage.yml +171 -0
  98. data/spec/integration_tests/document_api_spec.rb +178 -0
  99. data/spec/integration_tests/integration_test_utils.rb +170 -0
  100. data/spec/integration_tests/style_rule_api_spec.rb +135 -0
  101. data/spec/integration_tests/translation_memory_api_spec.rb +70 -0
  102. data/spec/requests/extra_body_parameters_types_spec.rb +82 -0
  103. data/spec/requests/glossary/create_spec.rb +65 -0
  104. data/spec/requests/glossary/destroy_spec.rb +66 -0
  105. data/spec/requests/glossary/entries_spec.rb +62 -0
  106. data/spec/requests/glossary/find_spec.rb +68 -0
  107. data/spec/requests/glossary/language_pairs_spec.rb +40 -0
  108. data/spec/requests/glossary/list_spec.rb +54 -0
  109. data/spec/requests/languages_spec.rb +68 -0
  110. data/spec/requests/rephrase_spec.rb +172 -0
  111. data/spec/requests/style_rule/create_custom_instruction_spec.rb +54 -0
  112. data/spec/requests/style_rule/create_spec.rb +45 -0
  113. data/spec/requests/style_rule/destroy_custom_instruction_spec.rb +54 -0
  114. data/spec/requests/style_rule/destroy_spec.rb +54 -0
  115. data/spec/requests/style_rule/find_custom_instruction_spec.rb +56 -0
  116. data/spec/requests/style_rule/find_spec.rb +56 -0
  117. data/spec/requests/style_rule/list_spec.rb +58 -0
  118. data/spec/requests/style_rule/update_configured_rules_spec.rb +52 -0
  119. data/spec/requests/style_rule/update_custom_instruction_spec.rb +58 -0
  120. data/spec/requests/style_rule/update_spec.rb +48 -0
  121. data/spec/requests/translate_spec.rb +492 -0
  122. data/spec/requests/translation_memory/list_spec.rb +61 -0
  123. data/spec/requests/usage_spec.rb +43 -0
  124. data/spec/resources/glossary_spec.rb +38 -0
  125. data/spec/resources/language_pair_spec.rb +23 -0
  126. data/spec/resources/language_spec.rb +45 -0
  127. data/spec/resources/text_spec.rb +23 -0
  128. data/spec/resources/translation_memory_spec.rb +35 -0
  129. data/spec/resources/usage_spec.rb +35 -0
  130. data/spec/spec_helper.rb +92 -0
  131. metadata +130 -3
@@ -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 StyleRule
9
+ class FindCustomInstruction < Base
10
+ def initialize(api, style_id, instruction_id, options = {})
11
+ super(api, options)
12
+ @style_id = style_id
13
+ @instruction_id = instruction_id
14
+ end
15
+
16
+ def request
17
+ build_custom_instruction(*execute_request_with_retries(get_request))
18
+ end
19
+
20
+ def to_s
21
+ "GET #{uri.request_uri}"
22
+ end
23
+
24
+ private
25
+
26
+ def build_custom_instruction(_request, response)
27
+ data = JSON.parse(response.body)
28
+ DeepL::Resources::CustomInstruction.new(data)
29
+ end
30
+
31
+ def uri
32
+ @uri ||= URI("#{host}/v3/#{path}")
33
+ end
34
+
35
+ def path
36
+ "style_rules/#{@style_id}/custom_instructions/#{@instruction_id}"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,59 @@
1
+ # Copyright 2025 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Requests
8
+ module StyleRule
9
+ class List < Base
10
+ def initialize(api, options = {})
11
+ super
12
+ end
13
+
14
+ def request
15
+ build_style_rule_list(*execute_request_with_retries(get_request))
16
+ end
17
+
18
+ def to_s
19
+ "GET #{uri.request_uri}"
20
+ end
21
+
22
+ private
23
+
24
+ def get_request # rubocop:disable Naming/AccessorMethodName
25
+ http_headers = add_json_content_type(headers)
26
+ Net::HTTP::Get.new(uri.request_uri, http_headers)
27
+ end
28
+
29
+ def build_style_rule_list(request, response)
30
+ data = JSON.parse(response.body)
31
+ data['style_rules'].map do |style_rule|
32
+ Resources::StyleRule.new(style_rule, request, response)
33
+ end
34
+ end
35
+
36
+ def uri
37
+ @uri ||= begin
38
+ base_uri = URI("#{host}/v3/#{path}")
39
+ query_string = build_query_string
40
+ base_uri.query = query_string unless query_string.empty?
41
+ base_uri
42
+ end
43
+ end
44
+
45
+ def build_query_string
46
+ params = {}
47
+ params['page'] = option(:page).to_s if option?(:page)
48
+ params['page_size'] = option(:page_size).to_s if option?(:page_size)
49
+ params['detailed'] = option(:detailed).to_s.downcase if option?(:detailed)
50
+ URI.encode_www_form(params)
51
+ end
52
+
53
+ def path
54
+ 'style_rules'
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,42 @@
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
+ class Update < Base
10
+ def initialize(api, style_id, name, options = {})
11
+ super(api, options)
12
+ @style_id = style_id
13
+ @name = name
14
+ end
15
+
16
+ def request
17
+ payload = { name: @name }
18
+ build_style_rule(*execute_request_with_retries(patch_request(payload)))
19
+ end
20
+
21
+ def to_s
22
+ "PATCH #{uri.request_uri}"
23
+ end
24
+
25
+ private
26
+
27
+ def build_style_rule(request, response)
28
+ data = JSON.parse(response.body)
29
+ DeepL::Resources::StyleRule.new(data, request, response)
30
+ end
31
+
32
+ def uri
33
+ @uri ||= URI("#{host}/v3/#{path}")
34
+ end
35
+
36
+ def path
37
+ "style_rules/#{@style_id}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ 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 StyleRule
9
+ class UpdateConfiguredRules < Base
10
+ def initialize(api, style_id, configured_rules, options = {})
11
+ super(api, options)
12
+ @style_id = style_id
13
+ @configured_rules = configured_rules
14
+ end
15
+
16
+ def request
17
+ build_style_rule(*execute_request_with_retries(put_request(@configured_rules)))
18
+ end
19
+
20
+ def to_s
21
+ "PUT #{uri.request_uri}"
22
+ end
23
+
24
+ private
25
+
26
+ def build_style_rule(request, response)
27
+ data = JSON.parse(response.body)
28
+ DeepL::Resources::StyleRule.new(data, request, response)
29
+ end
30
+
31
+ def uri
32
+ @uri ||= URI("#{host}/v3/#{path}")
33
+ end
34
+
35
+ def path
36
+ "style_rules/#{@style_id}/configured_rules"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,47 @@
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
+ class UpdateCustomInstruction < Base
10
+ def initialize(api, style_id, instruction_id, label, prompt, # rubocop:disable Metrics/ParameterLists
11
+ source_language = nil, options = {})
12
+ super(api, options)
13
+ @style_id = style_id
14
+ @instruction_id = instruction_id
15
+ @label = label
16
+ @prompt = prompt
17
+ @source_language = source_language
18
+ end
19
+
20
+ def request
21
+ payload = { label: @label, prompt: @prompt }
22
+ payload[:source_language] = @source_language if @source_language
23
+ build_custom_instruction(*execute_request_with_retries(put_request(payload)))
24
+ end
25
+
26
+ def to_s
27
+ "PUT #{uri.request_uri}"
28
+ end
29
+
30
+ private
31
+
32
+ def build_custom_instruction(_request, response)
33
+ data = JSON.parse(response.body)
34
+ DeepL::Resources::CustomInstruction.new(data)
35
+ end
36
+
37
+ def uri
38
+ @uri ||= URI("#{host}/v3/#{path}")
39
+ end
40
+
41
+ def path
42
+ "style_rules/#{@style_id}/custom_instructions/#{@instruction_id}"
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,106 @@
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 Translate < Base
9
+ STRING_TO_BOOLEAN_MAP = { '1' => true, '0' => false }.freeze
10
+ BOOLEAN_TO_STRING_MAP = { true => '1', false => '0' }.freeze
11
+ STRING_TO_BOOLEAN_CONVERSION = ->(value) { STRING_TO_BOOLEAN_MAP[value] }
12
+ BOOLEAN_TO_STRING_CONVERSION = ->(value) { BOOLEAN_TO_STRING_MAP[value] }
13
+ STRING_TO_ARRAY_CONVERSION = lambda { |value|
14
+ if value.nil?
15
+ nil
16
+ else
17
+ (value.is_a?(Array) ? value : value.split(','))
18
+ end
19
+ }.freeze
20
+ OPTIONS_CONVERSIONS = {
21
+ split_sentences: BOOLEAN_TO_STRING_CONVERSION,
22
+ preserve_formatting: STRING_TO_BOOLEAN_CONVERSION,
23
+ outline_detection: STRING_TO_BOOLEAN_CONVERSION,
24
+ splitting_tags: STRING_TO_ARRAY_CONVERSION,
25
+ non_splitting_tags: STRING_TO_ARRAY_CONVERSION,
26
+ ignore_tags: STRING_TO_ARRAY_CONVERSION,
27
+ custom_instructions: STRING_TO_ARRAY_CONVERSION
28
+ }.freeze
29
+
30
+ attr_reader :text, :source_lang, :target_lang, :ignore_tags, :splitting_tags,
31
+ :non_splitting_tags, :model_type, :custom_instructions, :tag_handling_version
32
+
33
+ def initialize(api, text, source_lang, target_lang, options = {})
34
+ super(api, options)
35
+ @text = text
36
+ @source_lang = source_lang
37
+ @target_lang = target_lang
38
+
39
+ tweak_parameters!
40
+ end
41
+
42
+ def request # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
43
+ text_arrayified = text.is_a?(Array) ? text : [text]
44
+ payload = { text: text_arrayified, source_lang: source_lang, target_lang: target_lang }
45
+
46
+ if option?(:style_rule)
47
+ style_rule = delete_option(:style_rule)
48
+ payload[:style_id] = if style_rule.is_a?(DeepL::Resources::StyleRule)
49
+ style_rule.style_id
50
+ else
51
+ style_rule.to_s
52
+ end
53
+ end
54
+
55
+ if option?(:translation_memory)
56
+ tm = delete_option(:translation_memory)
57
+ payload[:translation_memory_id] = if tm.is_a?(DeepL::Resources::TranslationMemory)
58
+ tm.translation_memory_id
59
+ else
60
+ tm.to_s
61
+ end
62
+ end
63
+
64
+ if option?(:translation_memory_threshold)
65
+ payload[:translation_memory_threshold] = delete_option(:translation_memory_threshold)
66
+ end
67
+
68
+ build_texts(*execute_request_with_retries(post_request(payload)))
69
+ end
70
+
71
+ def details
72
+ "HTTP Headers: #{headers}\nPayload #{{ text: text, source_lang: source_lang,
73
+ target_lang: target_lang }}"
74
+ end
75
+
76
+ def to_s
77
+ "POST #{uri.request_uri}"
78
+ end
79
+
80
+ private
81
+
82
+ def tweak_parameters!
83
+ OPTIONS_CONVERSIONS.each do |param, converter|
84
+ next unless option?(param) && !converter[option(param)].nil?
85
+
86
+ set_option(param, converter[option(param)])
87
+ end
88
+ end
89
+
90
+ def build_texts(request, response)
91
+ data = JSON.parse(response.body)
92
+
93
+ texts = data['translations'].map do |translation|
94
+ Resources::Text.new(translation['text'], translation['detected_source_language'],
95
+ translation['model_type_used'], request, response)
96
+ end
97
+
98
+ texts.size == 1 ? texts.first : texts
99
+ end
100
+
101
+ def path
102
+ 'translate'
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,58 @@
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 TranslationMemory
9
+ class List < Base
10
+ def initialize(api, options = {})
11
+ super
12
+ end
13
+
14
+ def request
15
+ build_translation_memory_list(*execute_request_with_retries(get_request))
16
+ end
17
+
18
+ def to_s
19
+ "GET #{uri.request_uri}"
20
+ end
21
+
22
+ private
23
+
24
+ def get_request # rubocop:disable Naming/AccessorMethodName
25
+ http_headers = add_json_content_type(headers)
26
+ Net::HTTP::Get.new(uri.request_uri, http_headers)
27
+ end
28
+
29
+ def build_translation_memory_list(request, response)
30
+ data = JSON.parse(response.body)
31
+ data['translation_memories'].map do |tm|
32
+ Resources::TranslationMemory.new(tm, request, response)
33
+ end
34
+ end
35
+
36
+ def uri
37
+ @uri ||= begin
38
+ base_uri = URI("#{host}/v3/#{path}")
39
+ query_string = build_query_string
40
+ base_uri.query = query_string unless query_string.empty?
41
+ base_uri
42
+ end
43
+ end
44
+
45
+ def build_query_string
46
+ params = {}
47
+ params['page'] = option(:page).to_s if option?(:page)
48
+ params['page_size'] = option(:page_size).to_s if option?(:page_size)
49
+ URI.encode_www_form(params)
50
+ end
51
+
52
+ def path
53
+ 'translation_memories'
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,33 @@
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 Usage < Base
9
+ def initialize(api, options = {})
10
+ super
11
+ end
12
+
13
+ def request
14
+ build_usage(*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_usage(request, response)
24
+ data = JSON.parse(response.body)
25
+ Resources::Usage.new(data['character_count'], data['character_limit'], request, response)
26
+ end
27
+
28
+ def path
29
+ 'usage'
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
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 Resources
8
+ class Base
9
+ attr_reader :request, :response
10
+
11
+ def initialize(request, response)
12
+ @request = request
13
+ @response = response
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,57 @@
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 Resources
8
+ class DocumentHandle < Base
9
+ attr_reader :document_id, :document_key
10
+
11
+ def initialize(document_id, document_key, *args)
12
+ super(*args)
13
+
14
+ @document_id = document_id
15
+ @document_key = document_key
16
+ end
17
+
18
+ def to_s
19
+ "DocumentHandle: ID: #{document_id} - Key: #{document_key}"
20
+ end
21
+
22
+ ##
23
+ # For this DocumentHandle, waits until the document translation is finished and returns the
24
+ # final status of the document.
25
+ #
26
+ # @return [DeepL::Resources::DocumentTranslationStatus] Final status of the document
27
+ # translation.
28
+
29
+ def wait_until_document_translation_finished
30
+ doc_status = nil
31
+ max_tries = max_doc_status_queries
32
+ num_tries = 0
33
+ loop do
34
+ num_tries += 1
35
+ sleep(calculate_waiting_time(doc_status)) unless doc_status.nil?
36
+ doc_status = DeepL.document.get_status(self)
37
+ break if doc_status.finished? || num_tries > max_tries
38
+ end
39
+ doc_status
40
+ end
41
+
42
+ private
43
+
44
+ def calculate_waiting_time(_resp)
45
+ # ignore _resp.seconds_remaining for now, while it is unreliable
46
+ 5
47
+ end
48
+
49
+ def max_doc_status_queries
50
+ configured_value = DeepL.configuration.max_doc_status_queries
51
+ return configured_value unless configured_value.nil?
52
+
53
+ 10
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,54 @@
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 Resources
8
+ class DocumentTranslationStatus < Base
9
+ attr_reader :document_id, :status, :seconds_remaining, :billed_characters, :error_message
10
+
11
+ def initialize(response, *args)
12
+ super(*args)
13
+
14
+ @document_id = response['document_id']
15
+ @status = response['status']
16
+ @seconds_remaining = response['seconds_remaining']
17
+ @billed_characters = response['billed_characters']
18
+ @error_message = response['error_message']
19
+ end
20
+
21
+ def to_s
22
+ "DocumentTranslationStatus: ID: #{document_id} - Status: #{status} " \
23
+ "- Error message: #{error_message}"
24
+ end
25
+
26
+ ##
27
+ # Checks if the translation finished successfully
28
+ #
29
+ # @return [true] if so
30
+
31
+ def successful?
32
+ status == 'done'
33
+ end
34
+
35
+ ##
36
+ # Checks if there was an error during translation
37
+ #
38
+ # @return [true] if so
39
+
40
+ def error?
41
+ status == 'error'
42
+ end
43
+
44
+ ##
45
+ # Checks if the translation process terminated. Note that this could be due to an error as
46
+ # well, but means no further waiting is necessary.
47
+ #
48
+ # @return [true] if so
49
+ def finished?
50
+ successful? || error?
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,28 @@
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 Resources
8
+ class Glossary < Base
9
+ attr_reader :id, :name, :ready, :source_lang, :target_lang, :creation_time, :entry_count
10
+
11
+ def initialize(glossary, *args)
12
+ super(*args)
13
+
14
+ @id = glossary['glossary_id']
15
+ @name = glossary['name']
16
+ @ready = glossary['ready']
17
+ @source_lang = glossary['source_lang']
18
+ @target_lang = glossary['target_lang']
19
+ @creation_time = glossary['creation_time']
20
+ @entry_count = glossary['entry_count']
21
+ end
22
+
23
+ def to_s
24
+ "#{id} - #{name}"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ # Copyright 2021 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 Resources
8
+ class Language < Base
9
+ attr_reader :code, :name
10
+
11
+ def initialize(code, name, supports_formality, *args)
12
+ super(*args)
13
+
14
+ @code = code
15
+ @name = name
16
+ @supports_formality = supports_formality
17
+ end
18
+
19
+ def to_s
20
+ "#{code} - #{name}"
21
+ end
22
+
23
+ def supports_formality?
24
+ return @supports_formality unless @supports_formality.nil?
25
+
26
+ raise Exceptions::NotSupported, 'Support formality is only available on target languages'
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,23 @@
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 Resources
8
+ class LanguagePair < Base
9
+ attr_reader :source_lang, :target_lang
10
+
11
+ def initialize(source_lang, target_lang, *args)
12
+ super(*args)
13
+
14
+ @source_lang = source_lang
15
+ @target_lang = target_lang
16
+ end
17
+
18
+ def to_s
19
+ "#{source_lang} - #{target_lang}"
20
+ end
21
+ end
22
+ end
23
+ end