Class: Mathpix::DocumentConversion
- Inherits:
-
Object
- Object
- Mathpix::DocumentConversion
- Defined in:
- lib/mathpix/document.rb
Overview
Document Conversion Result (async operation)
Polls Mathpix API until conversion completes
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#conversion_id ⇒ Object
readonly
Returns the value of attribute conversion_id.
-
#document_path ⇒ Object
readonly
Returns the value of attribute document_path.
-
#document_type ⇒ Object
readonly
Returns the value of attribute document_type.
Instance Method Summary collapse
-
#complete! ⇒ DocumentResult
Convenience method: wait and get result.
-
#initialize(client, conversion_id, document_path, document_type) ⇒ DocumentConversion
constructor
A new instance of DocumentConversion.
-
#result ⇒ DocumentResult
Get result (must wait_until_complete first).
-
#save_docx(path) ⇒ Object
Save DOCX output.
-
#save_html(path) ⇒ Object
Save HTML output.
-
#save_latex(path) ⇒ Object
Save LaTeX output.
-
#save_markdown(path) ⇒ Object
Save markdown output.
-
#wait_until_complete(max_wait: 600, poll_interval: 3.0) ⇒ self
Wait for conversion to complete.
Constructor Details
#initialize(client, conversion_id, document_path, document_type) ⇒ DocumentConversion
Returns a new instance of DocumentConversion.
121 122 123 124 125 126 |
# File 'lib/mathpix/document.rb', line 121 def initialize(client, conversion_id, document_path, document_type) @client = client @conversion_id = conversion_id @document_path = document_path @document_type = document_type end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
119 120 121 |
# File 'lib/mathpix/document.rb', line 119 def client @client end |
#conversion_id ⇒ Object (readonly)
Returns the value of attribute conversion_id.
119 120 121 |
# File 'lib/mathpix/document.rb', line 119 def conversion_id @conversion_id end |
#document_path ⇒ Object (readonly)
Returns the value of attribute document_path.
119 120 121 |
# File 'lib/mathpix/document.rb', line 119 def document_path @document_path end |
#document_type ⇒ Object (readonly)
Returns the value of attribute document_type.
119 120 121 |
# File 'lib/mathpix/document.rb', line 119 def document_type @document_type end |
Instance Method Details
#complete! ⇒ DocumentResult
Convenience method: wait and get result
172 173 174 175 |
# File 'lib/mathpix/document.rb', line 172 def complete! wait_until_complete result end |
#result ⇒ DocumentResult
Get result (must wait_until_complete first)
166 167 168 |
# File 'lib/mathpix/document.rb', line 166 def result @result || raise(ConversionError, 'Conversion not yet complete. Call wait_until_complete first.') end |
#save_docx(path) ⇒ Object
Save DOCX output
200 201 202 203 |
# File 'lib/mathpix/document.rb', line 200 def save_docx(path) complete! unless @result @result.save_docx(path) end |
#save_html(path) ⇒ Object
Save HTML output
193 194 195 196 |
# File 'lib/mathpix/document.rb', line 193 def save_html(path) complete! unless @result @result.save_html(path) end |
#save_latex(path) ⇒ Object
Save LaTeX output
186 187 188 189 |
# File 'lib/mathpix/document.rb', line 186 def save_latex(path) complete! unless @result @result.save_latex(path) end |
#save_markdown(path) ⇒ Object
Save markdown output
179 180 181 182 |
# File 'lib/mathpix/document.rb', line 179 def save_markdown(path) complete! unless @result @result.save_markdown(path) end |
#wait_until_complete(max_wait: 600, poll_interval: 3.0) ⇒ self
Wait for conversion to complete
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/mathpix/document.rb', line 133 def wait_until_complete(max_wait: 600, poll_interval: 3.0) start_time = Time.now loop do status_data = client.get_document_status(conversion_id) status = status_data['status'] case status when 'completed' @result = DocumentResult.new(build_result_data(status_data), document_path, document_type) return self when 'error', 'failed' raise ConversionError.new( "Document conversion failed: #{extract_status_error(status_data)}", conversion_id: conversion_id, conversion_status: status ) else # Any non-terminal status keeps polling. Mathpix reports several # intermediate states (received, loaded, split, processing, # pending, ...) — we only stop on 'completed' or an error. elapsed = Time.now - start_time if elapsed > max_wait raise TimeoutError, "Document conversion timed out after #{max_wait}s (last status: #{status})" end sleep poll_interval end end end |