Class: Mindee::V2::ClientOptions::BaseProductParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/v2/client_options/base_product_parameters.rb,
sig/mindee/v2/client_options/base_product_parameters.rbs

Overview

Base class for parameters accepted by all V2 endpoints.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_id, file_alias: nil, webhook_ids: nil, polling_options: nil, close_file: true) ⇒ BaseProductParameters

issues.

Parameters:

  • model_id (String) —

    ID of the model

  • file_alias (String, nil) (defaults to: nil) —

    File alias, if applicable.

  • webhook_ids (Array<String>, nil) (defaults to: nil) —

    List of webhook IDs to propagate the API response to.

  • polling_options (Hash, Input::PollingOptions, nil) (defaults to: nil) —

    Options for polling. Set only if having timeout

  • close_file (Boolean, nil) (defaults to: true) —

    Whether to close the file after parsing.

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 32

def initialize(
  model_id,
  file_alias: nil,
  webhook_ids: nil,
  polling_options: nil,
  close_file: true
)
  raise Error::MindeeInputError, 'Model ID is required.' if model_id.nil? || model_id.empty?

  @model_id = model_id
  @file_alias = file_alias
  @webhook_ids = webhook_ids || []
  @polling_options = get_clean_polling_options(polling_options)
  @close_file = close_file.nil? || close_file
end

Class Attribute Details

.slug ⇒ String (readonly)

Returns Slug for the endpoint.

Returns:

  • (String) —

    Slug for the endpoint.



56
57
58
59
60
61
62
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 56

def self.slug
  if self == BaseProductParameters
    raise NotImplementedError, 'Cannot access `slug` directly on the BaseProductParameters class.'
  end

  ''
end

Instance Attribute Details

#close_file ⇒ Boolean? (readonly)

Returns Whether to close the file after parsing.

Returns:

  • (Boolean, nil) —

    Whether to close the file after parsing.



24
25
26
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 24

def close_file
  @close_file
end

#file_alias ⇒ String? (readonly)

For example, an internal document ID, reference number, or database key. If set, it will be included in the job and result responses.

Returns:

  • (String, nil) —

    Optional: a free-form string to tag the request with your own identifier.



14
15
16
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 14

def file_alias
  @file_alias
end

#model_id ⇒ String (readonly)

Returns Model ID to use for the inference. Required.

Returns:

  • (String) —

    Model ID to use for the inference. Required.



9
10
11
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 9

def model_id
  @model_id
end

#polling_options ⇒ Input::PollingOptions

Returns Options for polling. Set only if having timeout issues.

Returns:



21
22
23
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 21

def polling_options
  @polling_options
end

#webhook_ids ⇒ Array<String>? (readonly)

If empty, no webhooks will be used.

Returns:

  • (Array<String>, nil) —

    Webhook IDs to call after all processing is finished.



18
19
20
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 18

def webhook_ids
  @webhook_ids
end

Class Method Details

.from_hash(params: {}) ⇒ BaseProductParameters

Load from a hash

Parameters:

  • params (Hash) (defaults to: {}) —

    Parameters to provide as a hash.

  • params: (Hash[String | Symbol, untyped]) (defaults to: {})

Returns:



72
73
74
75
76
77
78
79
80
81
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 72

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.

Parameters:

  • params (Hash) (defaults to: {}) —

    Parameters to provide as a hash.

  • params: (Hash[String | Symbol, untyped]) (defaults to: {})

Returns:

  • (Hash)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 86

def self.load_from_hash(params: {})
  params.transform_keys!(&:to_sym)

  if params.empty? || params[:model_id].nil? || params[:model_id].empty?
    raise Error::MindeeInputError, 'Model ID is required.'
  end

  polling_options_input = params.fetch(:page_options, Input::PollingOptions.new)
  if polling_options_input.is_a?(Hash)
    polling_options_input = polling_options_input.transform_keys(&:to_sym)
    Input::PollingOptions.new(
      initial_delay_sec: polling_options_input.fetch(:initial_delay_sec, 2.0),
      delay_sec: polling_options_input.fetch(:delay_sec, 1.5),
      max_retries: polling_options_input.fetch(:max_retries, 80)
    )
  end
  params
end

Instance Method Details

#append_form_data(form_data) ⇒ Array

Appends base form data to the provided array.

Parameters:

  • form_data (Array) —

    Array of form fields

  • (Array[Array[untyped]])

Returns:

  • (Array)


108
109
110
111
112
113
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 108

def append_form_data(form_data)
  form_data.push(['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

#get_clean_polling_options(polling_options) ⇒ Input::PollingOptions

Cleans a proper polling options object potentially from a hash.

Parameters:

Returns:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 140

def get_clean_polling_options(polling_options)
  return Input::PollingOptions.new if polling_options.is_a?(Input::PollingOptions)

  if polling_options.is_a?(Hash)
    polling_options = polling_options.transform_keys(&:to_sym)
    output_polling_options = Input::PollingOptions.new(
      initial_delay_sec: polling_options.fetch(:initial_delay_sec, 2.0),
      delay_sec: polling_options.fetch(:delay_sec, 1.5),
      max_retries: polling_options.fetch(:max_retries, 80)
    )
  else
    output_polling_options = if polling_options.is_a?(Input::PollingOptions)
                               polling_options || Input::PollingOptions.new
                             else
                               Input::PollingOptions.new
                             end
  end
  output_polling_options
end

#slug ⇒ String

Returns Slug for the endpoint.

Returns:

  • (String) —

    Slug for the endpoint.



65
66
67
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 65

def slug
  self.class.slug
end

#validate_async_params ⇒ void

This method returns an undefined value.

Validates the parameters for async auto-polling

Raises:

  • (ArgumentError)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mindee/v2/client_options/base_product_parameters.rb', line 116

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