Getting Started
This guide will help you get the most out of the Mindee Ruby client library to easily extract data from your documents.
Installation
Requirements
The following Ruby versions are tested and supported: 3.0, 3.1, 3.2, 3.3
Standard Installation
To quickly get started with the Ruby Client Library, Install by adding this line to your application's Gemfile:
gem 'mindee'
And then execute:
bundle install
Or you can install it like this:
gem install mindee
Finally, Ruby away!
Development Installation
If you'll be modifying the source code, you'll need to install the required libraries to get started.
We recommend using Bundler.
- First clone the repo.
git clone [email protected]:mindee/mindee-api-ruby.git
- Navigate to the cloned directory and install all required libraries.
cd mindee-api-ruby
bundle install
Updating the Library
It is important to always check the version of the Mindee OCR SDK you are using, as new and updated
features won’t work on older versions.
To get the latest version of your OCR SDK:
gem install mindee
To install a specific version of Mindee:
gem install mindee@<version>
Usage
Using Mindee's APIs can be broken down into the following steps:
- Initialize a Client
- Load a File
- Send the File to Mindee's API
- Process the Result in some way
Let's take a deep dive into how this works.
Initializing the Client
The Client
automatically connects to the default endpoints for each product (or creates one with given parameters for
Universal APIs).
The Client
requires your API key.
You can either pass these directly to the constructor or through environment variables.
Pass the API key directly
# Init a new client and passing the key directly
mindee_client = Mindee::Client.new(api_key: 'my-api-key')
Set the API key in the environment
API keys should be set as environment variables, especially for any production deployment.
The following environment variable will set the global API key:
MINDEE_API_KEY=my-api-key
Then in your code:
# Init a new client without an API key
mindee_client = Mindee::Client.new
Setting the Request Timeout
The request timeout can be set using an environment variable:
MINDEE_REQUEST_TIMEOUT=200
Loading a Document File
Before being able to send a document to the API, it must first be loaded.
You don't need to worry about different MIME types, the library will take care of handling
all supported types automatically.
Once a document is loaded, interacting with it is done in exactly the same way, regardless
of how it was loaded.
There are a few different ways of loading a document file, depending on your use case, you can use a:
- File path: using the Mindee Client's
source_from_path()
method. - File Object: using the Mindee Client's
source_from_file()
method. - Base64 String: using the Mindee Client's
source_from_b64string()
method. - Raw Byte sequence: using the Mindee Client's
source_from_bytes()
method. - URL: using the Mindee Client's
source_from_url()
method.
More details about file loading on the dedicated page.
Sending a File
To send a file to the API, we need to specify how to process the document.
This will determine which API endpoint is used and how the API return will be handled internally by the library.
More specifically, we need to set a Mindee::Product
class as the first parameter of the create_endpoint
method.
This is because the Endpoint
's urls will be set according to it
Each document type available in the library has its corresponding class, which inherit from the base
Mindee::Parsing::Common::Predict
class.
This is detailed in each document-specific guide.
Off-the-Shelf Documents
Simply setting the correct class is enough:
result = mindee_client.parse(
input_source,
Mindee::Product::Invoice::InvoiceV4
)
Specific call method
Some products, such as InvoiceV4, ReceiptV5 & FinancialDocumentV1 support both asynchronous polling and synchronous
HTTP calls.
We recommend letting the client library decide which is better by default, but you can override the behavior by setting
the enqueue
parameter to true
or false
.
result = mindee_client.parse(
input_source,
Mindee::Product::Invoice::InvoiceV4,
enqueue: false
)
WARNING: this feature is not available for all products, and may result in errors if used inappropriately.
Only use it if you are certain of what you are doing.
Universal Documents (docTI)
For custom documents, the endpoint to use must also be set, and it must take in an endpoint_name
:
endpoint = mindee_client.create_endpoint(endpoint_name: 'wnine', account_name: 'my-account')
result = mindee_client.parse(
input_source,
Mindee::Product::Universal::Universal,
endpoint: endpoint
)
This is because the Universal
class is enough to handle the return processing, but the actual endpoint needs to be
specified.
Process the Result
The response object is common to all documents, including custom documents (using the Universal product). The main
properties are:
id
— Mindee ID of the documentname
— Filename sent to the APIinference
— Inference
Inference
Regroups the predictions at the page level, as well as predictions for the entire document.
prediction
— Document level predictionpages
— Page level prediction
Document level prediction
The prediction
attribute is a Prediction
object specific to the type of document being processed.
It contains the data extracted from the entire document, all pages combined.
It's possible to have the same field in various pages, but at the document level,
only the highest confidence field data will be shown (this is all done automatically at the API level).
# as an object, complete
pp result.document.inference.prediction
# as a string, summary in RST format
puts result.document.inference.prediction
Page level prediction
The pages
attribute is a list of Prediction
objects.
Each page element contains the data extracted for a particular page of the document.
The order of the elements in the array matches the order of the pages in the document.
All response objects have this property, regardless of the number of pages.
Single page documents will have a single entry.
Iteration is done like any Ruby array:
response.document.inference.pages.each do |page|
# as an object, complete
pp page.prediction
# as a string, summary in RST format
puts page.prediction
end
Page Orientation
The orientation field is only available at the page level as it describes whether the page image should be rotated to
be upright.
If the page requires rotation for correct display, the orientation field gives a prediction among these 3 possible
outputs:
- 0 degrees: the page is already upright
- 90 degrees: the page must be rotated clockwise to be upright
- 270 degrees: the page must be rotated counterclockwise to be upright
response.document.inference.pages.each do |page|
puts page.orientation.value
end
Questions?
Updated 26 days ago