Class: Mindee::HTTP::Endpoint
- Inherits:
-
Object
- Object
- Mindee::HTTP::Endpoint
- Defined in:
- lib/mindee/http/endpoint.rb
Overview
Generic API endpoint for a product.
Instance Attribute Summary collapse
- #api_key ⇒ String readonly
- #request_timeout ⇒ Integer readonly
- #url_root ⇒ String readonly
Instance Method Summary collapse
-
#initialize(owner, url_name, version, api_key: '') ⇒ Endpoint
constructor
A new instance of Endpoint.
-
#parse_async(job_id) ⇒ Array
Calls the parsed async doc.
-
#predict(input_source, all_words, full_text, close_file, cropper) ⇒ Array
Call the prediction API.
-
#predict_async(input_source, all_words, full_text, close_file, cropper) ⇒ Array
Call the prediction API.
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_key ⇒ String (readonly)
32 33 34 |
# File 'lib/mindee/http/endpoint.rb', line 32 def api_key @api_key end |
#request_timeout ⇒ Integer (readonly)
34 35 36 |
# File 'lib/mindee/http/endpoint.rb', line 34 def request_timeout @request_timeout end |
#url_root ⇒ String (readonly)
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.
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.
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.
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 |