deepl-rb 3.6.0 → 3.6.1

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 (103) 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 +172 -0
  5. data/.rubocop.yml +51 -0
  6. data/CHANGELOG.md +22 -3
  7. data/CODE_OF_CONDUCT.md +132 -0
  8. data/CONTRIBUTING.md +37 -0
  9. data/Gemfile +24 -0
  10. data/Rakefile +56 -0
  11. data/SECURITY.md +58 -0
  12. data/VERSION +1 -0
  13. data/deepl-rb.gemspec +140 -0
  14. data/lib/deepl/api.rb +22 -0
  15. data/lib/deepl/configuration.rb +59 -0
  16. data/lib/deepl/constants/base_constant.rb +18 -0
  17. data/lib/deepl/constants/formality.rb +16 -0
  18. data/lib/deepl/constants/model_type.rb +14 -0
  19. data/lib/deepl/constants/split_sentences.rb +14 -0
  20. data/lib/deepl/constants/tag_handling.rb +13 -0
  21. data/lib/deepl/constants/tone.rb +20 -0
  22. data/lib/deepl/constants/writing_style.rb +20 -0
  23. data/lib/deepl/document_api.rb +121 -0
  24. data/lib/deepl/exceptions/authorization_failed.rb +14 -0
  25. data/lib/deepl/exceptions/bad_request.rb +16 -0
  26. data/lib/deepl/exceptions/document_translation_error.rb +15 -0
  27. data/lib/deepl/exceptions/error.rb +14 -0
  28. data/lib/deepl/exceptions/limit_exceeded.rb +18 -0
  29. data/lib/deepl/exceptions/not_found.rb +16 -0
  30. data/lib/deepl/exceptions/not_supported.rb +14 -0
  31. data/lib/deepl/exceptions/quota_exceeded.rb +14 -0
  32. data/lib/deepl/exceptions/request_entity_too_large.rb +15 -0
  33. data/lib/deepl/exceptions/request_error.rb +21 -0
  34. data/lib/deepl/exceptions/server_error.rb +18 -0
  35. data/lib/deepl/glossary_api.rb +38 -0
  36. data/lib/deepl/requests/base.rb +196 -0
  37. data/lib/deepl/requests/document/download.rb +44 -0
  38. data/lib/deepl/requests/document/get_status.rb +44 -0
  39. data/lib/deepl/requests/document/upload.rb +74 -0
  40. data/lib/deepl/requests/glossary/create.rb +59 -0
  41. data/lib/deepl/requests/glossary/destroy.rb +37 -0
  42. data/lib/deepl/requests/glossary/entries.rb +37 -0
  43. data/lib/deepl/requests/glossary/find.rb +38 -0
  44. data/lib/deepl/requests/glossary/language_pairs.rb +38 -0
  45. data/lib/deepl/requests/glossary/list.rb +37 -0
  46. data/lib/deepl/requests/languages.rb +37 -0
  47. data/lib/deepl/requests/rephrase.rb +55 -0
  48. data/lib/deepl/requests/style_rule/list.rb +59 -0
  49. data/lib/deepl/requests/translate.rb +93 -0
  50. data/lib/deepl/requests/usage.rb +33 -0
  51. data/lib/deepl/resources/base.rb +17 -0
  52. data/lib/deepl/resources/document_handle.rb +57 -0
  53. data/lib/deepl/resources/document_translation_status.rb +54 -0
  54. data/lib/deepl/resources/glossary.rb +28 -0
  55. data/lib/deepl/resources/language.rb +30 -0
  56. data/lib/deepl/resources/language_pair.rb +23 -0
  57. data/lib/deepl/resources/style_rule.rb +85 -0
  58. data/lib/deepl/resources/text.rb +24 -0
  59. data/lib/deepl/resources/usage.rb +27 -0
  60. data/lib/deepl/style_rule_api.rb +17 -0
  61. data/lib/deepl/utils/backoff_timer.rb +46 -0
  62. data/lib/deepl/utils/exception_builder.rb +34 -0
  63. data/lib/deepl.rb +158 -0
  64. data/lib/http_client_options.rb +22 -0
  65. data/lib/version.rb +8 -0
  66. data/spec/api/api_spec.rb +20 -0
  67. data/spec/api/configuration_spec.rb +122 -0
  68. data/spec/api/deepl_spec.rb +460 -0
  69. data/spec/constants/constants_spec.rb +158 -0
  70. data/spec/fixtures/vcr_cassettes/deepl_document.yml +95 -0
  71. data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +1214 -0
  72. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +1163 -0
  73. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +54 -0
  74. data/spec/fixtures/vcr_cassettes/deepl_rephrase.yml +87 -0
  75. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +358 -0
  76. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +129 -0
  77. data/spec/fixtures/vcr_cassettes/glossaries.yml +1702 -0
  78. data/spec/fixtures/vcr_cassettes/languages.yml +229 -0
  79. data/spec/fixtures/vcr_cassettes/rephrase_texts.yml +401 -0
  80. data/spec/fixtures/vcr_cassettes/style_rules.yml +92 -0
  81. data/spec/fixtures/vcr_cassettes/translate_texts.yml +10630 -0
  82. data/spec/fixtures/vcr_cassettes/usage.yml +171 -0
  83. data/spec/integration_tests/document_api_spec.rb +178 -0
  84. data/spec/integration_tests/integration_test_utils.rb +170 -0
  85. data/spec/integration_tests/style_rule_api_spec.rb +79 -0
  86. data/spec/requests/extra_body_parameters_types_spec.rb +82 -0
  87. data/spec/requests/glossary/create_spec.rb +65 -0
  88. data/spec/requests/glossary/destroy_spec.rb +66 -0
  89. data/spec/requests/glossary/entries_spec.rb +62 -0
  90. data/spec/requests/glossary/find_spec.rb +68 -0
  91. data/spec/requests/glossary/language_pairs_spec.rb +40 -0
  92. data/spec/requests/glossary/list_spec.rb +54 -0
  93. data/spec/requests/languages_spec.rb +68 -0
  94. data/spec/requests/rephrase_spec.rb +172 -0
  95. data/spec/requests/translate_spec.rb +493 -0
  96. data/spec/requests/usage_spec.rb +43 -0
  97. data/spec/resources/glossary_spec.rb +38 -0
  98. data/spec/resources/language_pair_spec.rb +23 -0
  99. data/spec/resources/language_spec.rb +45 -0
  100. data/spec/resources/text_spec.rb +23 -0
  101. data/spec/resources/usage_spec.rb +35 -0
  102. data/spec/spec_helper.rb +92 -0
  103. metadata +102 -2
data/deepl-rb.gemspec ADDED
@@ -0,0 +1,140 @@
1
+ # Generated by juwelier
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: deepl-rb 3.6.1 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "deepl-rb".freeze
9
+ s.version = "3.6.1"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
+ s.metadata = { "bug_tracker_uri" => "https://github.com/DeepLcom/deepl-rb/issues", "changelog_uri" => "https://github.com/DeepLcom/deepl-rb/blob/main/CHANGELOG.md", "documentation_uri" => "https://github.com/DeepLcom/deepl-rb/blob/main/README.md", "homepage_uri" => "https://github.com/DeepLcom/deepl-rb" } if s.respond_to? :metadata=
13
+ s.require_paths = ["lib".freeze]
14
+ s.authors = ["DeepL SE".freeze]
15
+ s.date = "2025-12-19"
16
+ s.description = "Official Ruby library for the DeepL language translation API (v2). For more information, check this: https://www.deepl.com/docs/api-reference.html".freeze
17
+ s.email = "open-source@deepl.com".freeze
18
+ s.extra_rdoc_files = [
19
+ "CHANGELOG.md",
20
+ "LICENSE.md",
21
+ "README.md",
22
+ "license_checker.sh"
23
+ ]
24
+ s.files = [
25
+ ".circleci/config.yml",
26
+ ".github/workflows/add_issues_to_kanban.yml",
27
+ ".gitlab-ci.yml",
28
+ ".rubocop.yml",
29
+ "CHANGELOG.md",
30
+ "CODE_OF_CONDUCT.md",
31
+ "CONTRIBUTING.md",
32
+ "Gemfile",
33
+ "LICENSE.md",
34
+ "README.md",
35
+ "Rakefile",
36
+ "SECURITY.md",
37
+ "VERSION",
38
+ "deepl-rb.gemspec",
39
+ "lib/deepl.rb",
40
+ "lib/deepl/api.rb",
41
+ "lib/deepl/configuration.rb",
42
+ "lib/deepl/constants/base_constant.rb",
43
+ "lib/deepl/constants/formality.rb",
44
+ "lib/deepl/constants/model_type.rb",
45
+ "lib/deepl/constants/split_sentences.rb",
46
+ "lib/deepl/constants/tag_handling.rb",
47
+ "lib/deepl/constants/tone.rb",
48
+ "lib/deepl/constants/writing_style.rb",
49
+ "lib/deepl/document_api.rb",
50
+ "lib/deepl/exceptions/authorization_failed.rb",
51
+ "lib/deepl/exceptions/bad_request.rb",
52
+ "lib/deepl/exceptions/document_translation_error.rb",
53
+ "lib/deepl/exceptions/error.rb",
54
+ "lib/deepl/exceptions/limit_exceeded.rb",
55
+ "lib/deepl/exceptions/not_found.rb",
56
+ "lib/deepl/exceptions/not_supported.rb",
57
+ "lib/deepl/exceptions/quota_exceeded.rb",
58
+ "lib/deepl/exceptions/request_entity_too_large.rb",
59
+ "lib/deepl/exceptions/request_error.rb",
60
+ "lib/deepl/exceptions/server_error.rb",
61
+ "lib/deepl/glossary_api.rb",
62
+ "lib/deepl/requests/base.rb",
63
+ "lib/deepl/requests/document/download.rb",
64
+ "lib/deepl/requests/document/get_status.rb",
65
+ "lib/deepl/requests/document/upload.rb",
66
+ "lib/deepl/requests/glossary/create.rb",
67
+ "lib/deepl/requests/glossary/destroy.rb",
68
+ "lib/deepl/requests/glossary/entries.rb",
69
+ "lib/deepl/requests/glossary/find.rb",
70
+ "lib/deepl/requests/glossary/language_pairs.rb",
71
+ "lib/deepl/requests/glossary/list.rb",
72
+ "lib/deepl/requests/languages.rb",
73
+ "lib/deepl/requests/rephrase.rb",
74
+ "lib/deepl/requests/style_rule/list.rb",
75
+ "lib/deepl/requests/translate.rb",
76
+ "lib/deepl/requests/usage.rb",
77
+ "lib/deepl/resources/base.rb",
78
+ "lib/deepl/resources/document_handle.rb",
79
+ "lib/deepl/resources/document_translation_status.rb",
80
+ "lib/deepl/resources/glossary.rb",
81
+ "lib/deepl/resources/language.rb",
82
+ "lib/deepl/resources/language_pair.rb",
83
+ "lib/deepl/resources/style_rule.rb",
84
+ "lib/deepl/resources/text.rb",
85
+ "lib/deepl/resources/usage.rb",
86
+ "lib/deepl/style_rule_api.rb",
87
+ "lib/deepl/utils/backoff_timer.rb",
88
+ "lib/deepl/utils/exception_builder.rb",
89
+ "lib/http_client_options.rb",
90
+ "lib/version.rb",
91
+ "license_checker.sh",
92
+ "spec/api/api_spec.rb",
93
+ "spec/api/configuration_spec.rb",
94
+ "spec/api/deepl_spec.rb",
95
+ "spec/constants/constants_spec.rb",
96
+ "spec/fixtures/vcr_cassettes/deepl_document.yml",
97
+ "spec/fixtures/vcr_cassettes/deepl_document_download.yml",
98
+ "spec/fixtures/vcr_cassettes/deepl_glossaries.yml",
99
+ "spec/fixtures/vcr_cassettes/deepl_languages.yml",
100
+ "spec/fixtures/vcr_cassettes/deepl_rephrase.yml",
101
+ "spec/fixtures/vcr_cassettes/deepl_translate.yml",
102
+ "spec/fixtures/vcr_cassettes/deepl_usage.yml",
103
+ "spec/fixtures/vcr_cassettes/glossaries.yml",
104
+ "spec/fixtures/vcr_cassettes/languages.yml",
105
+ "spec/fixtures/vcr_cassettes/rephrase_texts.yml",
106
+ "spec/fixtures/vcr_cassettes/style_rules.yml",
107
+ "spec/fixtures/vcr_cassettes/translate_texts.yml",
108
+ "spec/fixtures/vcr_cassettes/usage.yml",
109
+ "spec/integration_tests/document_api_spec.rb",
110
+ "spec/integration_tests/integration_test_utils.rb",
111
+ "spec/integration_tests/style_rule_api_spec.rb",
112
+ "spec/requests/extra_body_parameters_types_spec.rb",
113
+ "spec/requests/glossary/create_spec.rb",
114
+ "spec/requests/glossary/destroy_spec.rb",
115
+ "spec/requests/glossary/entries_spec.rb",
116
+ "spec/requests/glossary/find_spec.rb",
117
+ "spec/requests/glossary/language_pairs_spec.rb",
118
+ "spec/requests/glossary/list_spec.rb",
119
+ "spec/requests/languages_spec.rb",
120
+ "spec/requests/rephrase_spec.rb",
121
+ "spec/requests/translate_spec.rb",
122
+ "spec/requests/usage_spec.rb",
123
+ "spec/resources/glossary_spec.rb",
124
+ "spec/resources/language_pair_spec.rb",
125
+ "spec/resources/language_spec.rb",
126
+ "spec/resources/text_spec.rb",
127
+ "spec/resources/usage_spec.rb",
128
+ "spec/spec_helper.rb"
129
+ ]
130
+ s.homepage = "https://github.com/DeepLcom/deepl-rb".freeze
131
+ s.licenses = ["MIT".freeze]
132
+ s.rubygems_version = "3.4.6".freeze
133
+ s.summary = "Official Ruby library for the DeepL language translation API.".freeze
134
+
135
+ s.specification_version = 4
136
+
137
+ s.add_development_dependency(%q<juwelier>.freeze, [">= 0"])
138
+ s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
139
+ end
140
+
data/lib/deepl/api.rb ADDED
@@ -0,0 +1,22 @@
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
+ class API
8
+ attr_reader :configuration, :http_client
9
+
10
+ def initialize(configuration)
11
+ @configuration = configuration
12
+ configuration.validate!
13
+ uri = URI(configuration.host)
14
+ @http_client = Net::HTTP.new(uri.host, uri.port)
15
+ @http_client.use_ssl = uri.scheme == 'https'
16
+ end
17
+
18
+ def update_http_client(client)
19
+ @http_client = client
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,59 @@
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
+ class Configuration
8
+ ATTRIBUTES = %i[auth_key host logger max_doc_status_queries max_network_retries user_agent
9
+ version].freeze
10
+
11
+ attr_accessor(*ATTRIBUTES)
12
+
13
+ DEEPL_SERVER_URL = 'https://api.deepl.com'
14
+ DEEPL_SERVER_URL_FREE = 'https://api-free.deepl.com'
15
+ private_constant :DEEPL_SERVER_URL, :DEEPL_SERVER_URL_FREE
16
+
17
+ def initialize(data = {}, app_info_name = nil, app_info_version = nil, send_platform_info = true) # rubocop:disable all
18
+ data.each { |key, value| send("#{key}=", value) }
19
+ @auth_key ||= ENV.fetch('DEEPL_AUTH_KEY', nil)
20
+ @host ||= ENV.fetch('DEEPL_SERVER_URL', nil)
21
+ @host ||= if self.class.free_account_auth_key?(@auth_key)
22
+ DEEPL_SERVER_URL_FREE
23
+ else
24
+ DEEPL_SERVER_URL
25
+ end
26
+ @version ||= 'v2'
27
+ @user_agent ||= construct_user_agent(send_platform_info, app_info_name, app_info_version)
28
+ @max_network_retries ||= 5
29
+ @logger ||= nil
30
+ end
31
+
32
+ def validate!
33
+ raise Exceptions::Error, 'auth_key not provided' if auth_key.nil? || auth_key.empty?
34
+ end
35
+
36
+ def attributes
37
+ ATTRIBUTES.to_h { |attr| [attr, send(attr)] }
38
+ end
39
+
40
+ def ==(other)
41
+ attributes == other.attributes
42
+ end
43
+
44
+ def self.free_account_auth_key?(key)
45
+ key&.end_with?(':fx')
46
+ end
47
+
48
+ def construct_user_agent(send_platform_info, app_info_name, app_info_version)
49
+ library_info_str = "deepl-ruby/#{DeepL::VERSION}"
50
+ if send_platform_info
51
+ library_info_str += " (#{RbConfig::CONFIG['host_os']}) ruby/#{RUBY_VERSION}"
52
+ end
53
+ if app_info_name && app_info_version
54
+ library_info_str += " #{app_info_name}/#{app_info_version}"
55
+ end
56
+ library_info_str
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,18 @@
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 Constants
8
+ class BaseConstant
9
+ def self.options
10
+ constants.map { |const| const_get(const) }
11
+ end
12
+
13
+ def self.valid?(value)
14
+ options.include?(value)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
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 Constants
8
+ class Formality < BaseConstant
9
+ DEFAULT = 'default'
10
+ MORE = 'more'
11
+ LESS = 'less'
12
+ PREFER_MORE = 'prefer_more'
13
+ PREFER_LESS = 'prefer_less'
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
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 Constants
8
+ class ModelType < BaseConstant
9
+ QUALITY_OPTIMIZED = 'quality_optimized'
10
+ PREFER_QUALITY_OPTIMIZED = 'prefer_quality_optimized'
11
+ LATENCY_OPTIMIZED = 'latency_optimized'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
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 Constants
8
+ class SplitSentences < BaseConstant
9
+ NO_SPLITTING = '0'
10
+ SPLIT_ON_PUNCTUATION_AND_NEWLINES = '1'
11
+ SPLIT_ON_PUNCTUATION_ONLY = 'nonewlines'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
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 Constants
8
+ class TagHandling < BaseConstant
9
+ XML = 'xml'
10
+ HTML = 'html'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
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 Constants
8
+ class Tone < BaseConstant
9
+ DEFAULT = 'default'
10
+ ENTHUSIASTIC = 'enthusiastic'
11
+ FRIENDLY = 'friendly'
12
+ CONFIDENT = 'confident'
13
+ DIPLOMATIC = 'diplomatic'
14
+ PREFER_ENTHUSIASTIC = 'prefer_enthusiastic'
15
+ PREFER_FRIENDLY = 'prefer_friendly'
16
+ PREFER_CONFIDENT = 'prefer_confident'
17
+ PREFER_DIPLOMATIC = 'prefer_diplomatic'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
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 Constants
8
+ class WritingStyle < BaseConstant
9
+ DEFAULT = 'default'
10
+ SIMPLE = 'simple'
11
+ BUSINESS = 'business'
12
+ ACADEMIC = 'academic'
13
+ CASUAL = 'casual'
14
+ PREFER_SIMPLE = 'prefer_simple'
15
+ PREFER_BUSINESS = 'prefer_business'
16
+ PREFER_ACADEMIC = 'prefer_academic'
17
+ PREFER_CASUAL = 'prefer_casual'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,121 @@
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
+ class DocumentApi
8
+ def initialize(api, options = {})
9
+ @api = api
10
+ @options = options
11
+ end
12
+
13
+ ##
14
+ # Uploads the file at the given +input_file_path+ to be translated from +source_lang+ into
15
+ # +target_lang+. The API interface is async, so you need to poll using the returned
16
+ # `DeepL::Resources::DocumentHandle` until the translation is finished, then you can download it
17
+ #
18
+ # @param [String] input_file_path File path to the file to be translated
19
+ # @param [String, nil] source_lang Source language to use for the translation. `nil` will cause
20
+ # automatic source langauge detection to be used. Must be
21
+ # formatted as ISO 639-1, 2-letter language codes.
22
+ # @param [String] target_lang Target language to use for the translation. Must be formatted as
23
+ # ISO 639-1, 2-letter language codes, plus a hyphen "-" with the
24
+ # variant identifier for languages with variants/dialects/... .
25
+ # @param [String, nil] filename The filename of the file, including its extension. Used to open
26
+ # the different kinds of documents (PDFs, etc). If nil, will use
27
+ # the filename of +input_file_path+.
28
+ # @param [Hash] options Additional (body) options for the upload.
29
+ # @param [Hash] additional_headers Additional HTTP headers for the upload.
30
+ # @return [DeepL::Resources::DocumentHandle] Document handle for the uploaded document.
31
+
32
+ def upload(input_file_path, source_lang, target_lang, filename = nil, options = {},
33
+ additional_headers = {})
34
+ DeepL::Requests::Document::Upload.new(@api, input_file_path, source_lang, target_lang,
35
+ filename, options, additional_headers)
36
+ .request
37
+ end
38
+
39
+ ##
40
+ # Queries the status of the translation of the document with the given +document_handle+.
41
+ #
42
+ # @param [DeepL::Resources::DocumentHandle] document_handle Handle returned by the `upload`
43
+ # method.
44
+ # @param [Hash] options Additional options for the upload.
45
+ # @param [Hash] additional_headers Additional HTTP headers for the status check.
46
+ # @return [DeepL::Resources::DocumentTranslationStatus] Status of the document translation.
47
+
48
+ def get_status(document_handle, options = {}, additional_headers = {})
49
+ DeepL::Requests::Document::GetStatus.new(@api, document_handle.document_id,
50
+ document_handle.document_key, options,
51
+ additional_headers).request
52
+ end
53
+
54
+ ##
55
+ # Downloads the document identified by the +document_handle+ to +output_file+
56
+ #
57
+ # @param [DeepL::Resources::DocumentHandle] document_handle Handle returned by the `upload`
58
+ # method.
59
+ # @param [String] output_file Path to the file to write to. Will be overwritten if the file
60
+ # already exists.
61
+ # @return [DeepL::Resources::DocumentTranslationStatus] Status of the document translation.
62
+
63
+ def download(document_handle, output_file)
64
+ DeepL::Requests::Document::Download.new(@api, document_handle.document_id,
65
+ document_handle.document_key, output_file).request
66
+ end
67
+
68
+ ##
69
+ # Translates a document with the DeepL API, `sleep`ing during waiting periods. Returns the
70
+ # status that was queried last. This can be either because the document translation terminated
71
+ # (successfully or with an error) or because the maximum number of status requests have been
72
+ # made. See the parameter `max_doc_status_queries` for details.
73
+ #
74
+ # @raise [DocumentTranslationError] If any error occurs during the process.
75
+ #
76
+ # @param [String] input_file Path to the file to be translated
77
+ # @param [String] output_file Path to the file to write to. Will be overwritten if the file
78
+ # already exists.
79
+ # @param [String, nil] source_lang Source language to use for the translation. `nil` will cause
80
+ # automatic source langauge detection to be used. Must be
81
+ # formatted as ISO 639-1, 2-letter language codes.
82
+ # @param [String] target_lang Target language to use for the translation. Must be formatted as
83
+ # ISO 639-1, 2-letter language codes, plus a hyphen "-" with the
84
+ # variant identifier for languages with variants/dialects/... .
85
+ # @param [String, nil] filename The filename of the file, including its extension. Used to open
86
+ # the different kinds of documents (PDFs, etc). If nil, will use
87
+ # the filename of +input_file_path+.
88
+ # @param [Hash] options Additional options for the upload.
89
+ # @param [Hash] additional_headers Additional headers for the upload.
90
+ # @return [DeepL::Resources::DocumentTranslationStatus] Status of the document translation.
91
+
92
+ def translate_document(input_file, output_file, source_lang, target_lang, # rubocop:disable Metrics/MethodLength,Metrics/ParameterLists
93
+ filename = nil, options = {}, additional_headers = {})
94
+ raise IOError 'File already exists at output path' if File.exist?(output_file)
95
+
96
+ begin
97
+ handle = upload(input_file, source_lang, target_lang, filename, options,
98
+ additional_headers)
99
+ translate_document_wait_and_download(handle, output_file)
100
+ rescue StandardError => e
101
+ FileUtils.rm_f(output_file)
102
+ raise Exceptions::DocumentTranslationError.new(
103
+ "Error occurred during document translation: #{e.message}", handle
104
+ )
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def translate_document_wait_and_download(document_handle, output_file)
111
+ doc_status = document_handle.wait_until_document_translation_finished
112
+ if doc_status.error?
113
+ raise Exceptions::DocumentTranslationError.new(
114
+ "Exception when querying document status #{doc_status.error_message}", document_handle
115
+ )
116
+ else
117
+ download(document_handle, output_file)
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,14 @@
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 AuthorizationFailed < RequestError
9
+ def to_s
10
+ 'Authorization failed. Please supply a valid auth_key parameter.'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
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
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,18 @@
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
@@ -0,0 +1,16 @@
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
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,21 @@
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
@@ -0,0 +1,18 @@
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