Class: Mindee::Input::BaseParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/input/base_parameters.rb

Overview

Base class for parameters accepted by all V2 endpoints.

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) ⇒ BaseParameters

Returns a new instance of BaseParameters.

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, nil) (defaults to: nil)

    Options for polling. Set only if having timeout issues.

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

    Whether to close the file after parsing.

Raises:



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 = get_clean_polling_options(polling_options)
  @close_file = close_file.nil? || close_file
end

Instance Attribute Details

#close_fileBoolean? (readonly)

Returns Whether to close the file after parsing.

Returns:

  • (Boolean, nil)

    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_aliasString? (readonly)

Returns Optional alias for the file.

Returns:

  • (String, nil)

    Optional alias for the file.



11
12
13
# File 'lib/mindee/input/base_parameters.rb', line 11

def file_alias
  @file_alias
end

#model_idString (readonly)

Returns ID of the model (required).

Returns:

  • (String)

    ID of the model (required).



8
9
10
# File 'lib/mindee/input/base_parameters.rb', line 8

def model_id
  @model_id
end

#polling_optionsPollingOptions (readonly)

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

Returns:

  • (PollingOptions)

    Options for polling. Set only if having timeout issues.



17
18
19
# File 'lib/mindee/input/base_parameters.rb', line 17

def polling_options
  @polling_options
end

#webhook_idsArray<String>? (readonly)

Returns Optional list of Webhooks IDs to propagate the API response to.

Returns:

  • (Array<String>, nil)

    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.

Parameters:

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

    Parameters to provide as a hash.

Returns:

  • (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

  polling_options_input = params.fetch(:page_options, PollingOptions.new)
  if polling_options_input.is_a?(Hash)
    polling_options_input = polling_options_input.transform_keys(&:to_sym)
    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

.slugString

Returns Slug for the endpoint.

Returns:

  • (String)

    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.

Parameters:

  • form_data (Array)

    Array of form fields

Returns:

  • (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

#slugString

Returns Slug for the endpoint.

Returns:

  • (String)

    Slug for the endpoint.



53
54
55
# File 'lib/mindee/input/base_parameters.rb', line 53

def slug
  self.class.slug
end

#validate_async_paramsObject

Validates the parameters for async auto-polling

Raises:

  • (ArgumentError)


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