Class: Mathpix::DocumentConversion

Inherits:
Object
  • Object
show all
Defined in:
lib/mathpix/document.rb

Overview

Document Conversion Result (async operation)

Polls Mathpix API until conversion completes

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#clientObject (readonly)

Returns the value of attribute client.



119
120
121
# File 'lib/mathpix/document.rb', line 119

def client
  @client
end

#conversion_idObject (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_pathObject (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_typeObject (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

Returns:



172
173
174
175
# File 'lib/mathpix/document.rb', line 172

def complete!
  wait_until_complete
  result
end

#resultDocumentResult

Get result (must wait_until_complete first)

Returns:



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

Parameters:

  • path (String)

    output file path



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

Parameters:

  • path (String)

    output file path



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

Parameters:

  • path (String)

    output file path



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

Parameters:

  • path (String)

    output file path



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

Parameters:

  • max_wait (Integer) (defaults to: 600)

    maximum wait time in seconds

  • poll_interval (Float) (defaults to: 3.0)

    seconds between polls

Returns:

  • (self)


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