Class: Mindee::HTTP::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/http/endpoint.rb

Overview

Generic API endpoint for a product.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, url_name, version, api_key: '') ⇒ Endpoint

Returns a new instance of Endpoint.



38
39
40
41
42
43
44
45
46
# File 'lib/mindee/http/endpoint.rb', line 38

def initialize(owner, url_name, version, api_key: '')
  @owner = owner
  @url_name = url_name
  @version = version
  @request_timeout = ENV.fetch(REQUEST_TIMEOUT_ENV_NAME, TIMEOUT_DEFAULT).to_i
  @api_key = api_key.nil? || api_key.empty? ? ENV.fetch(API_KEY_ENV_NAME, API_KEY_DEFAULT) : api_key
  base_url = ENV.fetch(BASE_URL_ENV_NAME, BASE_URL_DEFAULT)
  @url_root = "#{base_url.chomp('/')}/products/#{@owner}/#{@url_name}/v#{@version}"
end

Instance Attribute Details

#api_keyString (readonly)

Returns:

  • (String)


32
33
34
# File 'lib/mindee/http/endpoint.rb', line 32

def api_key
  @api_key
end

#request_timeoutInteger (readonly)

Returns:

  • (Integer)


34
35
36
# File 'lib/mindee/http/endpoint.rb', line 34

def request_timeout
  @request_timeout
end

#url_rootString (readonly)

Returns:

  • (String)


36
37
38
# File 'lib/mindee/http/endpoint.rb', line 36

def url_root
  @url_root
end

Instance Method Details

#parse_async(job_id) ⇒ Array

Calls the parsed async doc.

Parameters:

  • job_id (String)

Returns:

  • (Array)


93
94
95
96
97
98
99
100
101
102
# File 'lib/mindee/http/endpoint.rb', line 93

def parse_async(job_id)
  check_api_key
  response = document_queue_req(job_id)
  hashed_response = JSON.parse(response.body, object_class: Hash)
  return [hashed_response, response.body] if ResponseValidation.valid_async_response?(response)

  ResponseValidation.clean_request!(response)
  error = Error.handle_error(@url_name, response)
  raise error
end

#predict(input_source, all_words, full_text, close_file, cropper) ⇒ Array

Call the prediction API.

Parameters:

  • input_source (Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::UrlInputSource)
  • all_words (Boolean)

    Whether the full word extraction needs to be performed

  • full_text (Boolean)

    Whether to include the full OCR text response in compatible APIs

  • close_file (Boolean)

    Whether the file will be closed after reading

  • cropper (Boolean)

    Whether a cropping operation will be applied

Returns:

  • (Array)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mindee/http/endpoint.rb', line 55

def predict(input_source, all_words, full_text, close_file, cropper)
  check_api_key
  response = predict_req_post(
    input_source,
    all_words: all_words,
    full_text: full_text,
    close_file: close_file,
    cropper: cropper
  )
  hashed_response = JSON.parse(response.body, object_class: Hash)
  return [hashed_response, response.body] if ResponseValidation.valid_sync_response?(response)

  ResponseValidation.clean_request!(response)
  error = Error.handle_error(@url_name, response)
  raise error
end

#predict_async(input_source, all_words, full_text, close_file, cropper) ⇒ Array

Call the prediction API.

Parameters:

  • input_source (Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::UrlInputSource)
  • all_words (Boolean)

    Whether the full word extraction needs to be performed

  • full_text (Boolean)

    Whether to include the full OCR text response in compatible APIs.

  • close_file (Boolean)

    Whether the file will be closed after reading

  • cropper (Boolean)

    Whether a cropping operation will be applied

Returns:

  • (Array)


79
80
81
82
83
84
85
86
87
88
# File 'lib/mindee/http/endpoint.rb', line 79

def predict_async(input_source, all_words, full_text, close_file, cropper)
  check_api_key
  response = document_queue_req_get(input_source, all_words, full_text, close_file, cropper)
  hashed_response = JSON.parse(response.body, object_class: Hash)
  return [hashed_response, response.body] if ResponseValidation.valid_async_response?(response)

  ResponseValidation.clean_request!(response)
  error = Error.handle_error(@url_name, response)
  raise error
end