Class: Ultrasafeai::Ocr::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ultrasafeai/ocr/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:, base_url: nil, environment: nil) ⇒ void

Parameters:



11
12
13
14
15
# File 'lib/ultrasafeai/ocr/client.rb', line 11

def initialize(client:, base_url: nil, environment: nil)
  @client = client
  @base_url = base_url
  @environment = environment
end

Instance Method Details

#extract(request_options: {}, **params) ⇒ Ultrasafeai::Types::UltraparserOcrResponse

Extract structured data from scanned documents or images using OCR. Provide a JSON Schema to define the fields you want to extract.

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (void)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:



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
72
73
74
75
76
77
78
# File 'lib/ultrasafeai/ocr/client.rb', line 29

def extract(request_options: {}, **params)
  params = Ultrasafeai::Internal::Types::Utils.normalize_keys(params)
  body = Internal::Multipart::FormData.new

  body.add_part(params[:file].to_form_data_part(name: "file")) if params[:file]
  if params[:json_schema]
    body.add(
      name: "json_schema",
      value: params[:json_schema]
    )
  end
  if params[:extraction_target]
    body.add(
      name: "extraction_target",
      value: params[:extraction_target]
    )
  end
  if params[:dpi]
    body.add(
      name: "dpi",
      value: params[:dpi]
    )
  end
  if params[:batch_size]
    body.add(
      name: "batch_size",
      value: params[:batch_size]
    )
  end

  request = Ultrasafeai::Internal::Multipart::Request.new(
    base_url: request_options[:base_url] || @base_url || @environment&.dig(:base),
    method: "POST",
    path: "ocr",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Ultrasafeai::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Ultrasafeai::Types::UltraparserOcrResponse.load(response.body)
  else
    error_class = Ultrasafeai::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#generate_schema(request_options: {}, **params) ⇒ Ultrasafeai::Types::UltraparserGenerateSchemaResponse

Upload a sample document to automatically generate a JSON Schema and extraction instruction. This endpoint is free and does not consume credits.

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (void)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ultrasafeai/ocr/client.rb', line 152

def generate_schema(request_options: {}, **params)
  params = Ultrasafeai::Internal::Types::Utils.normalize_keys(params)
  body = Internal::Multipart::FormData.new

  body.add_part(params[:file].to_form_data_part(name: "file")) if params[:file]
  if params[:prompt]
    body.add(
      name: "prompt",
      value: params[:prompt]
    )
  end

  request = Ultrasafeai::Internal::Multipart::Request.new(
    base_url: request_options[:base_url] || @base_url || @environment&.dig(:base),
    method: "POST",
    path: "ocr/generate-schema",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Ultrasafeai::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Ultrasafeai::Types::UltraparserGenerateSchemaResponse.load(response.body)
  else
    error_class = Ultrasafeai::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#stream(request_options: {}, **params) ⇒ untyped

Same as POST /ocr but returns an SSE stream of progress events. Set Accept: text/event-stream.

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (void)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:

  • (untyped)

Raises:

  • (error_class)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ultrasafeai/ocr/client.rb', line 91

def stream(request_options: {}, **params)
  params = Ultrasafeai::Internal::Types::Utils.normalize_keys(params)
  body = Internal::Multipart::FormData.new

  body.add_part(params[:file].to_form_data_part(name: "file")) if params[:file]
  if params[:json_schema]
    body.add(
      name: "json_schema",
      value: params[:json_schema]
    )
  end
  if params[:extraction_target]
    body.add(
      name: "extraction_target",
      value: params[:extraction_target]
    )
  end
  if params[:dpi]
    body.add(
      name: "dpi",
      value: params[:dpi]
    )
  end
  if params[:batch_size]
    body.add(
      name: "batch_size",
      value: params[:batch_size]
    )
  end

  request = Ultrasafeai::Internal::Multipart::Request.new(
    base_url: request_options[:base_url] || @base_url || @environment&.dig(:base),
    method: "POST",
    path: "ocr/stream",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Ultrasafeai::Errors::TimeoutError
  end
  code = response.code.to_i
  return if code.between?(200, 299)

  error_class = Ultrasafeai::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(response.body, code: code)
end