Class: DeepL::DocumentApi
- Inherits:
-
Object
- Object
- DeepL::DocumentApi
- Defined in:
- lib/deepl/document_api.rb
Instance Method Summary collapse
-
#download(document_handle, output_file) ⇒ DeepL::Resources::DocumentTranslationStatus
Downloads the document identified by the
document_handletooutput_file. -
#get_status(document_handle, options = {}, additional_headers = {}) ⇒ DeepL::Resources::DocumentTranslationStatus
Queries the status of the translation of the document with the given
document_handle. -
#initialize(api, options = {}) ⇒ DocumentApi
constructor
A new instance of DocumentApi.
-
#translate_document(input_file, output_file, source_lang, target_lang, filename = nil, options = {}, additional_headers = {}) ⇒ DeepL::Resources::DocumentTranslationStatus
Translates a document with the DeepL API,
sleeping during waiting periods. -
#upload(input_file_path, source_lang, target_lang, filename = nil, options = {}, additional_headers = {}) ⇒ DeepL::Resources::DocumentHandle
Uploads the file at the given
input_file_pathto be translated fromsource_langintotarget_lang.
Constructor Details
#initialize(api, options = {}) ⇒ DocumentApi
Returns a new instance of DocumentApi.
8 9 10 11 |
# File 'lib/deepl/document_api.rb', line 8 def initialize(api, = {}) @api = api @options = end |
Instance Method Details
#download(document_handle, output_file) ⇒ DeepL::Resources::DocumentTranslationStatus
Downloads the document identified by the document_handle to output_file
method.
73 74 75 76 |
# File 'lib/deepl/document_api.rb', line 73 def download(document_handle, output_file) DeepL::Requests::Document::Download.new(@api, document_handle.document_id, document_handle.document_key, output_file).request end |
#get_status(document_handle, options = {}, additional_headers = {}) ⇒ DeepL::Resources::DocumentTranslationStatus
Queries the status of the translation of the document with the given document_handle.
method.
58 59 60 61 62 |
# File 'lib/deepl/document_api.rb', line 58 def get_status(document_handle, = {}, additional_headers = {}) DeepL::Requests::Document::GetStatus.new(@api, document_handle.document_id, document_handle.document_key, , additional_headers).request end |
#translate_document(input_file, output_file, source_lang, target_lang, filename = nil, options = {}, additional_headers = {}) ⇒ DeepL::Resources::DocumentTranslationStatus
Translates a document with the DeepL API, sleeping during waiting periods. Returns the
status that was queried last. This can be either because the document translation terminated
(successfully or with an error) or because the maximum number of status requests have been
made. See the parameter max_doc_status_queries for details.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/deepl/document_api.rb', line 102 def translate_document(input_file, output_file, source_lang, target_lang, # rubocop:disable Metrics/MethodLength,Metrics/ParameterLists filename = nil, = {}, additional_headers = {}) raise IOError 'File already exists at output path' if File.exist?(output_file) begin handle = upload(input_file, source_lang, target_lang, filename, , additional_headers) translate_document_wait_and_download(handle, output_file) rescue StandardError => e FileUtils.rm_f(output_file) raise Exceptions::DocumentTranslationError.new( "Error occurred during document translation: #{e.}", handle ) end end |
#upload(input_file_path, source_lang, target_lang, filename = nil, options = {}, additional_headers = {}) ⇒ DeepL::Resources::DocumentHandle
Uploads the file at the given input_file_path to be translated from source_lang into
target_lang. The API interface is async, so you need to poll using the returned
DeepL::Resources::DocumentHandle until the translation is finished, then you can download it
42 43 44 45 46 47 |
# File 'lib/deepl/document_api.rb', line 42 def upload(input_file_path, source_lang, target_lang, filename = nil, = {}, additional_headers = {}) DeepL::Requests::Document::Upload.new(@api, input_file_path, source_lang, target_lang, filename, , additional_headers) .request end |