Class: Tabscanner::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/tabscanner/result.rb

Overview

Handles polling for OCR processing results

This class manages the polling logic to retrieve processing results from the Tabscanner API using a token, with retry logic and timeout handling.

Examples:

Poll for results with default timeout

Result.get_result('token123')

Poll for results with custom timeout

Result.get_result('token123', timeout: 30)

Class Method Summary collapse

Class Method Details

.get_result(token, timeout: 15) ⇒ Hash

Poll for OCR processing results using a token

Parameters:

  • token (String)

    Token from submit_receipt call

  • timeout (Integer) (defaults to: 15)

    Maximum time to wait in seconds (default: 15)

Returns:

  • (Hash)

    Parsed receipt data when processing is complete

Raises:



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tabscanner/result.rb', line 27

def self.get_result(token, timeout: 15)
  config = Tabscanner.config
  config.validate!

  start_time = Time.now
  conn = build_connection(config)

  config.logger.debug("Starting result polling for token: #{token} (timeout: #{timeout}s)") if config.debug?

  loop do
    # Check timeout
    elapsed = Time.now - start_time
    if elapsed >= timeout
      raise Error, "Timeout waiting for result after #{timeout} seconds"
    end

    # Make GET request to result endpoint
    response = conn.get("/api/result/#{token}")
    
    # Debug logging for request/response
    log_request_response('GET', "/api/result/#{token}", response, config) if config.debug?
    
    result = handle_response(response)

    # Check status in response
    case result['status']
    when 'complete', 'completed', 'success', 'done'
      config.logger.debug("Result ready for token: #{token}") if config.debug?
      return extract_result_data(result)
    when 'processing', 'pending', 'in_progress'
      # Wait 1 second before next poll
      config.logger.debug("Result still processing for token: #{token}, waiting 1s...") if config.debug?
      sleep 1
      next
    when 'failed', 'error'
      error_message = result['error'] || result['message'] || 'Processing failed'
      config.logger.debug("Result failed for token: #{token} - #{error_message}") if config.debug?
      raise Error, error_message
    else
      # Unknown status - treat as error
      config.logger.debug("Unknown status for token: #{token} - #{result['status']}") if config.debug?
      raise Error, "Unknown processing status: #{result['status']}"
    end
  end
end