Class: Payabli::Ocr::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void

Parameters:



9
10
11
# File 'lib/payabli/ocr/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#ocr_document_form(request_options: {}, **params) ⇒ Payabli::Types::PayabliApiResponseOcr

Use this endpoint to upload a document file for OCR processing as multipart/form-data, with the file in a field named file. The accepted file formats include PDF, JPG, JPEG, PNG, and GIF. Specify the desired type of result (either 'bill' or 'invoice') in the path parameter typeResult. The response will contain the OCR processing results, including extracted data such as bill number, vendor information, bill items, and more. To send the file as a Base64-encoded string in a JSON body instead, use ocrDocumentJson.

Examples:

client.ocr.ocr_document_form(type_result: "typeResult")

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)

Options Hash (**params):

Returns:



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
# File 'lib/payabli/ocr/client.rb', line 32

def ocr_document_form(request_options: {}, **params)
  params = Payabli::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]

  headers = @client.auth_headers_for_endpoint(security: [{ "BearerAuth" => [] }, { "APIKeyAuth" => [] }])
  request = Payabli::Internal::Multipart::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "Import/ocrDocumentForm/#{URI.encode_uri_component(params[:type_result].to_s)}",
    headers: headers,
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Payabli::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Payabli::Types::PayabliApiResponseOcr.load(response.body)
  else
    error_class = Payabli::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#ocr_document_json(request_options: {}, **params) ⇒ Payabli::Types::PayabliApiResponseOcr

Use this endpoint to submit a Base64-encoded image file for OCR processing. The accepted file formats include PDF, JPG, JPEG, PNG, and GIF. Specify the desired type of result (either 'bill' or 'invoice') in the path parameter typeResult. The response will contain the OCR processing results, including extracted data such as bill number, vendor information, bill items, and more.

Examples:

client.ocr.ocr_document_json(type_result: "typeResult")

Parameters:

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)

Options Hash (**params):

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/payabli/ocr/client.rb', line 79

def ocr_document_json(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  request_data = Payabli::Ocr::Types::OcrDocumentJsonRequest.new(params).to_h
  non_body_param_names = %w[typeResult]
  body = request_data.except(*non_body_param_names)

  headers = @client.auth_headers_for_endpoint(security: [{ "BearerAuth" => [] }, { "APIKeyAuth" => [] }])
  request = Payabli::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "Import/ocrDocumentJson/#{URI.encode_uri_component(params[:type_result].to_s)}",
    headers: headers,
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Payabli::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Payabli::Types::PayabliApiResponseOcr.load(response.body)
  else
    error_class = Payabli::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end