Ruby for Uploading Documents in Rossum API

This Ruby code shows how to upload a document automatically using Rossum API to a particular Rossum queue.

This Ruby code demonstrates how to upload a document using Rossum API to a particular Rossum queue. Set the options at the beginning of the script – they store your credentials, and also the identifier of the queue to upload in. You can find out the queue id by looking at your main screen URL, it is the number 12345 in https://example.rossum.app/annotations/12345.

See the reference API documentation for more details about how to use the upload API.

require 'date'
require 'faraday'
require 'json'

def authorize(username, password)
  response = Faraday.post(
  'https://example.rossum.app/api/v1/auth/login',
  {'username': username, 'password': password}.to_json,
  {'Content-Type': 'application/json'})
  auth_token = JSON.parse(response.body)['key']
  return auth_token
end

def upload_document(file_path, queue_id, auth_token)
  conn = Faraday.new('https://example.rossum.app/api') do |f|
    f.request :multipart
    f.adapter Faraday.default_adapter 
  end

  payload = { :content => Faraday::UploadIO.new(file_path, 'application/pdf') }
  headers = {'Authorization': "token #{auth_token}"}
  response = conn.post("/v1/queues/#{queue_id}/upload", payload, headers)
  
  return JSON.parse(response.body)
end

username = '[email protected]'
password = 'some.secret.password'
queue_id = 12345
file_path = 'invoice.pdf'

auth_token = authorize(username, password)
upload_document(file_path, queue_id, auth_token)