Class: Lara::Documents
- Inherits:
-
Object
- Object
- Lara::Documents
- Defined in:
- lib/lara/documents.rb
Constant Summary collapse
- ALLOWED_DOCUMENT_PARAMS =
%i[ id status target filename source created_at updated_at options translated_chars total_chars error_reason ].freeze
Instance Method Summary collapse
-
#download(id, output_format: nil) ⇒ String
Download translated document bytes.
-
#initialize(client, s3_client = S3Client.new) ⇒ Documents
constructor
A new instance of Documents.
-
#status(id) ⇒ Lara::Models::Document
Fetch document status.
-
#translate(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, output_format: nil, no_trace: false, style: nil, password: nil, extraction_params: nil) ⇒ String
Translates a document end-to-end.
-
#upload(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, password: nil, extraction_params: nil) ⇒ Lara::Models::Document
Uploads a file to S3.
Constructor Details
Instance Method Details
#download(id, output_format: nil) ⇒ String
Download translated document bytes
64 65 66 67 68 69 |
# File 'lib/lara/documents.rb', line 64 def download(id, output_format: nil) params = {} params[:output_format] = output_format if output_format url = @client.get("/v2/documents/#{id}/download-url", params: params)["url"] @s3.download(url: url) end |
#status(id) ⇒ Lara::Models::Document
Fetch document status
56 57 58 59 60 |
# File 'lib/lara/documents.rb', line 56 def status(id) response = @client.get("/v2/documents/#{id}") response_params = response.transform_keys(&:to_sym) Lara::Models::Document.new(**filter_document_params(response_params)) end |
#translate(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, output_format: nil, no_trace: false, style: nil, password: nil, extraction_params: nil) ⇒ String
Translates a document end-to-end
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/lara/documents.rb', line 73 def translate(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, output_format: nil, no_trace: false, style: nil, password: nil, extraction_params: nil) document = upload(file_path: file_path, filename: filename, target: target, source: source, adapt_to: adapt_to, glossaries: glossaries, no_trace: no_trace, style: style, password: password, extraction_params: extraction_params) max_wait_time = 60 * 15 # 15 minutes start = Time.now loop do |_| current = status(document.id) case current.status when Lara::Models::DocumentStatus::TRANSLATED return download(current.id, output_format: output_format) when Lara::Models::DocumentStatus::ERROR raise Lara::LaraApiError.new(500, "DocumentError", current.error_reason || "Unknown error") end raise Timeout::Error if Time.now - start > max_wait_time sleep @polling_interval end end |
#upload(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, password: nil, extraction_params: nil) ⇒ Lara::Models::Document
Uploads a file to S3
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/lara/documents.rb', line 27 def upload(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, password: nil, extraction_params: nil) response_data = @client.get("/v2/documents/upload-url", params: { filename: filename }) url = response_data["url"] fields = response_data["fields"] @s3.upload(url: url, fields: fields, io: file_path) body = { s3key: fields["key"], target: target, source: source, adapt_to: adapt_to, glossaries: glossaries, style: style, password: password, extraction_params: extraction_params&.to_h }.compact headers = {} headers["X-No-Trace"] = "true" if no_trace response = @client.post("/v2/documents", body: body, headers: headers) response_params = response.transform_keys(&:to_sym) Lara::Models::Document.new(**filter_document_params(response_params)) end |