Class: Mindee::Input::BaseParameters
- Inherits:
-
Object
- Object
- Mindee::Input::BaseParameters
- Defined in:
- lib/mindee/input/base_parameters.rb
Overview
Base class for parameters accepted by all V2 endpoints.
Direct Known Subclasses
InferenceParameters, V2::Product::Classification::Params::ClassificationParameters, V2::Product::Crop::Params::CropParameters, V2::Product::Ocr::Params::OcrParameters, V2::Product::Split::Params::SplitParameters
Instance Attribute Summary collapse
-
#close_file ⇒ Boolean?
readonly
Whether to close the file after parsing.
-
#file_alias ⇒ String?
readonly
Optional alias for the file.
-
#model_id ⇒ String
readonly
ID of the model (required).
-
#polling_options ⇒ PollingOptions
readonly
Options for polling.
-
#webhook_ids ⇒ Array<String>?
readonly
Optional list of Webhooks IDs to propagate the API response to.
Class Method Summary collapse
- .from_hash(params: {}) ⇒ Object
-
.load_from_hash(params: {}) ⇒ Hash
Loads a prediction from a Hash.
-
.slug ⇒ String
Slug for the endpoint.
Instance Method Summary collapse
-
#append_form_data(form_data) ⇒ Array
Appends base form data to the provided array.
-
#initialize(model_id, file_alias: nil, webhook_ids: nil, polling_options: nil, close_file: true) ⇒ BaseParameters
constructor
A new instance of BaseParameters.
-
#slug ⇒ String
Slug for the endpoint.
-
#validate_async_params ⇒ Object
Validates the parameters for async auto-polling.
Constructor Details
#initialize(model_id, file_alias: nil, webhook_ids: nil, polling_options: nil, close_file: true) ⇒ BaseParameters
Returns a new instance of BaseParameters.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mindee/input/base_parameters.rb', line 27 def initialize( model_id, file_alias: nil, webhook_ids: nil, polling_options: nil, close_file: true ) raise Errors::MindeeInputError, 'Model ID is required.' if model_id.empty? || model_id.nil? @model_id = model_id @file_alias = file_alias @webhook_ids = webhook_ids || [] @polling_options = () @close_file = close_file.nil? || close_file end |
Instance Attribute Details
#close_file ⇒ Boolean? (readonly)
Returns Whether to close the file after parsing.
20 21 22 |
# File 'lib/mindee/input/base_parameters.rb', line 20 def close_file @close_file end |
#file_alias ⇒ String? (readonly)
Returns Optional alias for the file.
11 12 13 |
# File 'lib/mindee/input/base_parameters.rb', line 11 def file_alias @file_alias end |
#model_id ⇒ String (readonly)
Returns ID of the model (required).
8 9 10 |
# File 'lib/mindee/input/base_parameters.rb', line 8 def model_id @model_id end |
#polling_options ⇒ PollingOptions (readonly)
Returns Options for polling. Set only if having timeout issues.
17 18 19 |
# File 'lib/mindee/input/base_parameters.rb', line 17 def @polling_options end |
#webhook_ids ⇒ Array<String>? (readonly)
Returns Optional list of Webhooks IDs to propagate the API response to.
14 15 16 |
# File 'lib/mindee/input/base_parameters.rb', line 14 def webhook_ids @webhook_ids end |
Class Method Details
.from_hash(params: {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mindee/input/base_parameters.rb', line 57 def self.from_hash(params: {}) load_from_hash(params: params) new( params[:model_id], file_alias: params[:file_alias], webhook_ids: params[:webhook_ids], polling_options: params[:polling_options], close_file: params[:close_file] ) end |
.load_from_hash(params: {}) ⇒ Hash
Loads a prediction from a Hash.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mindee/input/base_parameters.rb', line 71 def self.load_from_hash(params: {}) params.transform_keys!(&:to_sym) if params.empty? || params[:model_id].nil? || params[:model_id].empty? raise Errors::MindeeInputError, 'Model ID is required.' end = params.fetch(:page_options, PollingOptions.new) if .is_a?(Hash) = .transform_keys(&:to_sym) PollingOptions.new( initial_delay_sec: .fetch(:initial_delay_sec, 2.0), delay_sec: .fetch(:delay_sec, 1.5), max_retries: .fetch(:max_retries, 80) ) end params end |
.slug ⇒ String
Returns Slug for the endpoint.
44 45 46 47 48 49 50 |
# File 'lib/mindee/input/base_parameters.rb', line 44 def self.slug if self == BaseParameters raise NotImplementedError, 'Cannot access `slug` directly on the BaseParameters class.' end '' end |
Instance Method Details
#append_form_data(form_data) ⇒ Array
Appends base form data to the provided array.
93 94 95 96 97 98 |
# File 'lib/mindee/input/base_parameters.rb', line 93 def append_form_data(form_data) form_data.push(['file_alias', @file_alias]) if @file_alias webhook_ids = @webhook_ids || [] form_data.push(['webhook_ids', webhook_ids.join(',')]) unless @webhook_ids.nil? || webhook_ids.empty? form_data end |
#slug ⇒ String
Returns Slug for the endpoint.
53 54 55 |
# File 'lib/mindee/input/base_parameters.rb', line 53 def slug self.class.slug end |
#validate_async_params ⇒ Object
Validates the parameters for async auto-polling
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mindee/input/base_parameters.rb', line 101 def validate_async_params min_delay_sec = 1 min_initial_delay_sec = 1 min_retries = 2 if @polling_options.delay_sec < min_delay_sec raise ArgumentError, "Cannot set auto-poll delay to less than #{min_delay_sec} second(s)" end if @polling_options.initial_delay_sec < min_initial_delay_sec raise ArgumentError, "Cannot set initial parsing delay to less than #{min_initial_delay_sec} second(s)" end return unless @polling_options.max_retries < min_retries raise ArgumentError, "Cannot set auto-poll retries to less than #{min_retries}" end |