deepl-rb 2.5.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/add_issues_to_kanban.yml +16 -0
  3. data/.gitlab-ci.yml +135 -0
  4. data/.rubocop.yml +27 -0
  5. data/CHANGELOG.md +34 -0
  6. data/CODE_OF_CONDUCT.md +132 -0
  7. data/CONTRIBUTING.md +37 -0
  8. data/Gemfile +3 -1
  9. data/LICENSE.md +1 -0
  10. data/README.md +115 -5
  11. data/Rakefile +7 -5
  12. data/SECURITY.md +58 -0
  13. data/VERSION +1 -1
  14. data/deepl-rb.gemspec +36 -20
  15. data/lib/deepl/api.rb +11 -1
  16. data/lib/deepl/configuration.rb +34 -3
  17. data/lib/deepl/document_api.rb +120 -0
  18. data/lib/deepl/exceptions/authorization_failed.rb +3 -0
  19. data/lib/deepl/exceptions/bad_request.rb +3 -0
  20. data/lib/deepl/exceptions/document_translation_error.rb +15 -0
  21. data/lib/deepl/exceptions/error.rb +6 -0
  22. data/lib/deepl/exceptions/limit_exceeded.rb +7 -0
  23. data/lib/deepl/exceptions/not_found.rb +3 -0
  24. data/lib/deepl/exceptions/not_supported.rb +3 -0
  25. data/lib/deepl/exceptions/quota_exceeded.rb +3 -0
  26. data/lib/deepl/exceptions/request_entity_too_large.rb +3 -0
  27. data/lib/deepl/exceptions/request_error.rb +4 -2
  28. data/lib/deepl/exceptions/server_error.rb +18 -0
  29. data/lib/deepl/glossary_api.rb +3 -0
  30. data/lib/deepl/requests/base.rb +89 -34
  31. data/lib/deepl/requests/document/download.rb +44 -0
  32. data/lib/deepl/requests/document/get_status.rb +44 -0
  33. data/lib/deepl/requests/document/upload.rb +64 -0
  34. data/lib/deepl/requests/glossary/create.rb +15 -1
  35. data/lib/deepl/requests/glossary/destroy.rb +8 -1
  36. data/lib/deepl/requests/glossary/entries.rb +8 -1
  37. data/lib/deepl/requests/glossary/find.rb +8 -1
  38. data/lib/deepl/requests/glossary/language_pairs.rb +9 -2
  39. data/lib/deepl/requests/glossary/list.rb +9 -2
  40. data/lib/deepl/requests/languages.rb +9 -2
  41. data/lib/deepl/requests/translate.rb +33 -11
  42. data/lib/deepl/requests/usage.rb +9 -2
  43. data/lib/deepl/resources/base.rb +3 -0
  44. data/lib/deepl/resources/document_handle.rb +57 -0
  45. data/lib/deepl/resources/document_translation_status.rb +54 -0
  46. data/lib/deepl/resources/glossary.rb +3 -0
  47. data/lib/deepl/resources/language.rb +3 -0
  48. data/lib/deepl/resources/language_pair.rb +3 -0
  49. data/lib/deepl/resources/text.rb +3 -0
  50. data/lib/deepl/resources/usage.rb +3 -0
  51. data/lib/deepl/utils/backoff_timer.rb +46 -0
  52. data/lib/deepl/utils/exception_builder.rb +18 -13
  53. data/lib/deepl.rb +47 -0
  54. data/lib/http_client_options.rb +22 -0
  55. data/license_checker.sh +8 -0
  56. data/spec/api/api_spec.rb +8 -4
  57. data/spec/api/configuration_spec.rb +92 -18
  58. data/spec/api/deepl_spec.rb +225 -86
  59. data/spec/fixtures/vcr_cassettes/deepl_document.yml +95 -0
  60. data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +1214 -0
  61. data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +812 -23
  62. data/spec/fixtures/vcr_cassettes/deepl_languages.yml +28 -17
  63. data/spec/fixtures/vcr_cassettes/deepl_translate.yml +161 -53
  64. data/spec/fixtures/vcr_cassettes/deepl_usage.yml +93 -3
  65. data/spec/fixtures/vcr_cassettes/glossaries.yml +1237 -15
  66. data/spec/fixtures/vcr_cassettes/languages.yml +159 -44
  67. data/spec/fixtures/vcr_cassettes/translate_texts.yml +9742 -12
  68. data/spec/fixtures/vcr_cassettes/usage.yml +134 -2
  69. data/spec/integration_tests/document_api_spec.rb +143 -0
  70. data/spec/integration_tests/integration_test_utils.rb +170 -0
  71. data/spec/requests/glossary/create_spec.rb +23 -13
  72. data/spec/requests/glossary/destroy_spec.rb +33 -17
  73. data/spec/requests/glossary/entries_spec.rb +31 -17
  74. data/spec/requests/glossary/find_spec.rb +31 -17
  75. data/spec/requests/glossary/language_pairs_spec.rb +17 -7
  76. data/spec/requests/glossary/list_spec.rb +21 -11
  77. data/spec/requests/languages_spec.rb +31 -21
  78. data/spec/requests/translate_spec.rb +125 -131
  79. data/spec/requests/usage_spec.rb +17 -7
  80. data/spec/resources/glossary_spec.rb +15 -12
  81. data/spec/resources/language_pair_spec.rb +10 -7
  82. data/spec/resources/language_spec.rb +21 -18
  83. data/spec/resources/text_spec.rb +10 -7
  84. data/spec/resources/usage_spec.rb +16 -13
  85. data/spec/spec_helper.rb +63 -6
  86. metadata +32 -9
data/SECURITY.md ADDED
@@ -0,0 +1,58 @@
1
+ To report security concerns or vulnerabilities within deepl-rb, please email
2
+ us at [security@deepl.com](mailto:security@deepl.com).
3
+
4
+ You can send us PGP-encrypted email using the following PGP public key:
5
+
6
+ ```
7
+ -----BEGIN PGP PUBLIC KEY BLOCK-----
8
+
9
+ mQINBF7WSmABEADzRUp22VY7bVfUWScKLi9o8BRSEL4u3aPn9WOQoRLQH0j3dNNQ
10
+ FQlwTPn/Ez6qreEl8mX0aE+eLCEykXqsrU/UaTSTslF+H6UQyuGLXkRm8Lblt93I
11
+ OEhL069fC7rm+/zJq72+hishBF8DXqa+WtFd8VfK3i211vRhU/teKeAKT0xiuN/5
12
+ EQl1Bn7jR7mmQtNbPBhCsAlaC/tNUQ3Lyj6LYnnco7ums5Q/gCvfs2HM3mXJyvnG
13
+ 1MC2IrECPowTt04W3V1uXuMcm766orTG/AmtBIbPmOzao4sfqwRVHGvc8zcr1az9
14
+ 0nVyEJXx1eUVRDU1GAQuMjEkGgwvTd+nt6sHpn8C+9hMYJhon9veDSupViBuvNRC
15
+ p1PnpSLYYy7tA7DPXMhP3cMXe+Z4bYzgwi3xjOwh6SDyB4OFIxtGyuMrLGfZnd6A
16
+ hDH4H9zHPpciD5MxcOaqlKdgABQALvc6MvJ1Guf1ckGTbfHz1brtR1LPMK8rrnNu
17
+ kwQzgk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mhLFc+QxSYrs1W4ZEGnZ3+9utqzlEiMusy9Rq0T
49
+ /W/wrG6SckebjhrwWZJmy/hkW6V6LUX4++vCVV5+zwsvgEortCV8bhvLfqQDr/WN
50
+ fnmbNZtXJbyhTYbcYReOLeKidxO2lZEemnX6iOt5xCdoMcYU23xDT9+tE7Eh6Nfw
51
+ einZemBwfku5vxxPF73pOoQUCRq9tgvUrEq+3BqkqidhnFUOPi0J5726q1PBG65x
52
+ 5o+SQyvB3NA3al3mEH65z3V3/g0UHnhGcEMwVOXBkffgdKNhWYw59qhSVQnkiq0U
53
+ MG10g/RL7VdiISAFPTDmKWUaEDYosinKqOMHwcaVdJq9ssvPf89et6yP/ZkbLIHs
54
+ 2y3oiPonh2RMxi2OedlDz+Jp/A2o3qHmwNvBx/meGB0praGUonFVZTAA1EMS39Bi
55
+ NhG/L8giTyzA0mMkTJAPXtUVlRe5rEjORgYJsgRqZxEfpsJC9OkvYS4ayO0eCEs=
56
+ =jVHt
57
+ -----END PGP PUBLIC KEY BLOCK-----
58
+ ```%
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.5.3
1
+ 3.0.0
data/deepl-rb.gemspec CHANGED
@@ -2,36 +2,46 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: deepl-rb 2.5.3 ruby lib
5
+ # stub: deepl-rb 3.0.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "deepl-rb".freeze
9
- s.version = "2.5.3"
9
+ s.version = "3.0.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
- s.authors = ["Daniel Herzog".freeze]
14
- s.date = "2022-09-26"
15
- s.description = "A simple ruby wrapper for the DeepL translation API (v1). For more information, check this: https://www.deepl.com/docs/api-reference.html".freeze
16
- s.email = "info@danielherzog.es".freeze
13
+ s.authors = ["DeepL SE".freeze]
14
+ s.date = "2024-09-20"
15
+ 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
16
+ s.email = "open-source@deepl.com".freeze
17
17
  s.extra_rdoc_files = [
18
+ "CHANGELOG.md",
18
19
  "LICENSE.md",
19
- "README.md"
20
+ "README.md",
21
+ "license_checker.sh"
20
22
  ]
21
23
  s.files = [
22
24
  ".circleci/config.yml",
25
+ ".github/workflows/add_issues_to_kanban.yml",
26
+ ".gitlab-ci.yml",
23
27
  ".rubocop.yml",
28
+ "CHANGELOG.md",
29
+ "CODE_OF_CONDUCT.md",
30
+ "CONTRIBUTING.md",
24
31
  "Gemfile",
25
32
  "LICENSE.md",
26
33
  "README.md",
27
34
  "Rakefile",
35
+ "SECURITY.md",
28
36
  "VERSION",
29
37
  "deepl-rb.gemspec",
30
38
  "lib/deepl.rb",
31
39
  "lib/deepl/api.rb",
32
40
  "lib/deepl/configuration.rb",
41
+ "lib/deepl/document_api.rb",
33
42
  "lib/deepl/exceptions/authorization_failed.rb",
34
43
  "lib/deepl/exceptions/bad_request.rb",
44
+ "lib/deepl/exceptions/document_translation_error.rb",
35
45
  "lib/deepl/exceptions/error.rb",
36
46
  "lib/deepl/exceptions/limit_exceeded.rb",
37
47
  "lib/deepl/exceptions/not_found.rb",
@@ -39,8 +49,12 @@ Gem::Specification.new do |s|
39
49
  "lib/deepl/exceptions/quota_exceeded.rb",
40
50
  "lib/deepl/exceptions/request_entity_too_large.rb",
41
51
  "lib/deepl/exceptions/request_error.rb",
52
+ "lib/deepl/exceptions/server_error.rb",
42
53
  "lib/deepl/glossary_api.rb",
43
54
  "lib/deepl/requests/base.rb",
55
+ "lib/deepl/requests/document/download.rb",
56
+ "lib/deepl/requests/document/get_status.rb",
57
+ "lib/deepl/requests/document/upload.rb",
44
58
  "lib/deepl/requests/glossary/create.rb",
45
59
  "lib/deepl/requests/glossary/destroy.rb",
46
60
  "lib/deepl/requests/glossary/entries.rb",
@@ -51,15 +65,22 @@ Gem::Specification.new do |s|
51
65
  "lib/deepl/requests/translate.rb",
52
66
  "lib/deepl/requests/usage.rb",
53
67
  "lib/deepl/resources/base.rb",
68
+ "lib/deepl/resources/document_handle.rb",
69
+ "lib/deepl/resources/document_translation_status.rb",
54
70
  "lib/deepl/resources/glossary.rb",
55
71
  "lib/deepl/resources/language.rb",
56
72
  "lib/deepl/resources/language_pair.rb",
57
73
  "lib/deepl/resources/text.rb",
58
74
  "lib/deepl/resources/usage.rb",
75
+ "lib/deepl/utils/backoff_timer.rb",
59
76
  "lib/deepl/utils/exception_builder.rb",
77
+ "lib/http_client_options.rb",
78
+ "license_checker.sh",
60
79
  "spec/api/api_spec.rb",
61
80
  "spec/api/configuration_spec.rb",
62
81
  "spec/api/deepl_spec.rb",
82
+ "spec/fixtures/vcr_cassettes/deepl_document.yml",
83
+ "spec/fixtures/vcr_cassettes/deepl_document_download.yml",
63
84
  "spec/fixtures/vcr_cassettes/deepl_glossaries.yml",
64
85
  "spec/fixtures/vcr_cassettes/deepl_languages.yml",
65
86
  "spec/fixtures/vcr_cassettes/deepl_translate.yml",
@@ -68,6 +89,8 @@ Gem::Specification.new do |s|
68
89
  "spec/fixtures/vcr_cassettes/languages.yml",
69
90
  "spec/fixtures/vcr_cassettes/translate_texts.yml",
70
91
  "spec/fixtures/vcr_cassettes/usage.yml",
92
+ "spec/integration_tests/document_api_spec.rb",
93
+ "spec/integration_tests/integration_test_utils.rb",
71
94
  "spec/requests/glossary/create_spec.rb",
72
95
  "spec/requests/glossary/destroy_spec.rb",
73
96
  "spec/requests/glossary/entries_spec.rb",
@@ -84,21 +107,14 @@ Gem::Specification.new do |s|
84
107
  "spec/resources/usage_spec.rb",
85
108
  "spec/spec_helper.rb"
86
109
  ]
87
- s.homepage = "http://github.com/wikiti/deepl-rb".freeze
110
+ s.homepage = "https://github.com/DeepLcom/deepl-rb".freeze
88
111
  s.licenses = ["MIT".freeze]
89
- s.rubygems_version = "3.3.7".freeze
90
- s.summary = "A simple ruby wrapper for the DeepL API".freeze
112
+ s.rubygems_version = "3.4.6".freeze
113
+ s.summary = "Official Ruby library for the DeepL language translation API.".freeze
91
114
 
92
- if s.respond_to? :specification_version then
93
- s.specification_version = 4
94
- end
115
+ s.specification_version = 4
95
116
 
96
- if s.respond_to? :add_runtime_dependency then
97
- s.add_development_dependency(%q<juwelier>.freeze, [">= 0"])
98
- s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
99
- else
100
- s.add_dependency(%q<juwelier>.freeze, [">= 0"])
101
- s.add_dependency(%q<byebug>.freeze, [">= 0"])
102
- end
117
+ s.add_development_dependency(%q<juwelier>.freeze, [">= 0"])
118
+ s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
103
119
  end
104
120
 
data/lib/deepl/api.rb CHANGED
@@ -1,12 +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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
4
7
  class API
5
- attr_reader :configuration
8
+ attr_reader :configuration, :http_client
6
9
 
7
10
  def initialize(configuration)
8
11
  @configuration = configuration
9
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
10
20
  end
11
21
  end
12
22
  end
@@ -1,16 +1,32 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
4
7
  class Configuration
5
- ATTRIBUTES = %i[auth_key host version].freeze
8
+ ATTRIBUTES = %i[auth_key host logger max_doc_status_queries max_network_retries user_agent
9
+ version].freeze
6
10
 
7
11
  attr_accessor(*ATTRIBUTES)
8
12
 
9
- def initialize(data = {})
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
10
18
  data.each { |key, value| send("#{key}=", value) }
11
19
  @auth_key ||= ENV.fetch('DEEPL_AUTH_KEY', nil)
12
- @host ||= 'https://api.deepl.com'
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
13
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
14
30
  end
15
31
 
16
32
  def validate!
@@ -24,5 +40,20 @@ module DeepL
24
40
  def ==(other)
25
41
  attributes == other.attributes
26
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/2.5.3'
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
27
58
  end
28
59
  end
@@ -0,0 +1,120 @@
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).
87
+ # @param [Hash] options Additional options for the upload.
88
+ # @param [Hash] additional_headers Additional headers for the upload.
89
+ # @return [DeepL::Resources::DocumentTranslationStatus] Status of the document translation.
90
+
91
+ def translate_document(input_file, output_file, source_lang, target_lang, filename, # rubocop:disable Metrics/MethodLength,Metrics/ParameterLists
92
+ options = {}, additional_headers = {})
93
+ raise IOError 'File already exists at output path' if File.exist?(output_file)
94
+
95
+ begin
96
+ handle = upload(input_file, source_lang, target_lang, filename, options,
97
+ additional_headers)
98
+ translate_document_wait_and_download(handle, output_file)
99
+ rescue StandardError => e
100
+ FileUtils.rm_f(output_file)
101
+ raise Exceptions::DocumentTranslationError.new(
102
+ "Error occurred during document translation: #{e.message}", handle
103
+ )
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def translate_document_wait_and_download(document_handle, output_file)
110
+ doc_status = document_handle.wait_until_document_translation_finished
111
+ if doc_status.error?
112
+ raise Exceptions::DocumentTranslationError.new(
113
+ "Exception when querying document status #{doc_status.error_message}", document_handle
114
+ )
115
+ else
116
+ download(document_handle, output_file)
117
+ end
118
+ end
119
+ end
120
+ end
@@ -1,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
@@ -1,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
@@ -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
@@ -1,8 +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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
4
7
  module Exceptions
5
8
  class Error < StandardError
9
+ def should_retry?
10
+ false
11
+ end
6
12
  end
7
13
  end
8
14
  end
@@ -1,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
@@ -6,6 +9,10 @@ module DeepL
6
9
  def message
7
10
  'Limit exceeded. Please wait and send your request once again.'
8
11
  end
12
+
13
+ def should_retry?
14
+ true
15
+ end
9
16
  end
10
17
  end
11
18
  end
@@ -1,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
@@ -1,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
@@ -1,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
@@ -1,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
@@ -1,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL
@@ -5,9 +8,8 @@ module DeepL
5
8
  class RequestError < Error
6
9
  attr_reader :request, :response
7
10
 
8
- def initialize(request, response)
11
+ def initialize(response)
9
12
  super()
10
- @request = request
11
13
  @response = response
12
14
  end
13
15
 
@@ -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 message
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,3 +1,6 @@
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.
1
4
  # frozen_string_literal: true
2
5
 
3
6
  module DeepL