Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Code Converting
- When given code in another language for conversion, keep the original's overall naming for class, variable, and function names.
Only adjust as needed to conform to Ruby conventions, ex `myMethod` becomes `my_method`.
Only adjust as needed to conform to Ruby conventions, ex `myMethod` becomes `my_method`, `MyMethodAsync` (C#) becomes `my_method`.

## Code Linting
- Run `prek run -a` to lint and format the code.
Expand Down
1 change: 1 addition & 0 deletions lib/mindee/v2/client_options.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require_relative 'client_options/base_product_parameters'
require_relative 'client_options/base_classifier_product_parameters'
require_relative 'client_options/base_search_parameters'
73 changes: 73 additions & 0 deletions lib/mindee/v2/client_options/base_classifier_product_parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

module Mindee
module V2
module ClientOptions
# Base parameters for classification-based products.
class BaseClassifierProductParameters < BaseProductParameters
# @return [String, nil] Pass additional context to the extraction model chained to this utility inference.
attr_reader :extraction_text_context

# @param [String] model_id ID of the model
# @param [String, nil] file_alias File alias, if applicable.
# @param [Array<String>, nil] webhook_ids List of webhook IDs to propagate the API response to.
# @param [Boolean, nil] close_file Whether to close the file after parsing.
# @param [String, nil] extraction_text_context Pass additional context to the extraction model
# chained to this utility inference.
def initialize(
model_id,
file_alias: nil,
webhook_ids: nil,
close_file: true,
extraction_text_context: nil
)
super(
model_id,
file_alias: file_alias,
webhook_ids: webhook_ids,
close_file: close_file
)

if !extraction_text_context.nil? && !extraction_text_context.is_a?(String)
raise Error::MindeeInputError, 'Extraction text context must be a string.'
end

@extraction_text_context = extraction_text_context
end

# Load from a hash
# @param [Hash] params Parameters to provide as a hash.
# @return [BaseClassifierProductParameters]
def self.from_hash(params: {})
cleaned_params = load_from_hash(params: params)
new(
cleaned_params[:model_id],
file_alias: cleaned_params[:file_alias],
webhook_ids: cleaned_params[:webhook_ids],
close_file: cleaned_params[:close_file],
extraction_text_context: cleaned_params[:extraction_text_context]
)
end

# Loads parameters from a Hash.
# @param [Hash] params Parameters to provide as a hash.
# @return [Hash]
def self.load_from_hash(params: {})
cleaned_params = super
cleaned_params[:extraction_text_context] =
params[:extraction_text_context] || params['extraction_text_context']
cleaned_params
Comment thread
ianardee marked this conversation as resolved.
Resolved
end

# Gets the request parameters for the endpoint.
# @return [Hash{String => String, Array<String>}]
def request_parameters
params = super
text_context = @extraction_text_context
params['extraction_text_context'] = text_context if text_context
params
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Classification
# Classification parameters.
module Params
# Parameters accepted by the classification utility v2 endpoint.
class ClassificationParameters < Mindee::V2::ClientOptions::BaseProductParameters
class ClassificationParameters < Mindee::V2::ClientOptions::BaseClassifierProductParameters
# @return [String] Slug for the endpoint.
def self.slug
'classification'
Expand All @@ -17,11 +17,14 @@ def self.slug
# @param [String, nil] file_alias File alias, if applicable.
# @param [Array<String>, nil] webhook_ids List of webhook IDs to propagate the API response to.
# @param [Boolean, nil] close_file Whether to close the file after parsing.
# @param [String, nil] extraction_text_context Pass additional context to the extraction model chained
# to this utility inference.
def initialize(
model_id,
file_alias: nil,
webhook_ids: nil,
close_file: true
close_file: true,
extraction_text_context: nil
)
super
end
Expand All @@ -34,7 +37,8 @@ def self.from_hash(params: {})
params.fetch(:model_id),
file_alias: params.fetch(:file_alias, nil),
webhook_ids: params.fetch(:webhook_ids, nil),
close_file: params.fetch(:close_file, true)
close_file: params.fetch(:close_file, true),
extraction_text_context: params.fetch(:extraction_text_context, nil)
Comment thread
ianardee marked this conversation as resolved.
Resolved
)
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/mindee/v2/product/crop/params/crop_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Crop
# Crop parameters.
module Params
# Parameters accepted by the crop utility v2 endpoint.
class CropParameters < Mindee::V2::ClientOptions::BaseProductParameters
class CropParameters < Mindee::V2::ClientOptions::BaseClassifierProductParameters
# @return [String] Slug for the endpoint.
def self.slug
'crop'
Expand All @@ -17,11 +17,14 @@ def self.slug
# @param [String, nil] file_alias File alias, if applicable.
# @param [Array<String>, nil] webhook_ids List of webhook IDs to propagate the API response to.
# @param [Boolean, nil] close_file Whether to close the file after parsing.
# @param [String, nil] extraction_text_context Pass additional context to the extraction model chained
# to this utility inference.
def initialize(
model_id,
file_alias: nil,
webhook_ids: nil,
close_file: true
close_file: true,
extraction_text_context: nil
)
super
end
Expand All @@ -34,7 +37,8 @@ def self.from_hash(params: {})
params.fetch(:model_id),
file_alias: params.fetch(:file_alias, nil),
webhook_ids: params.fetch(:webhook_ids, nil),
close_file: params.fetch(:close_file, true)
close_file: params.fetch(:close_file, true),
extraction_text_context: params.fetch(:extraction_text_context, nil)
)
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/mindee/v2/product/split/params/split_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Split
# Split parameters.
module Params
# Parameters accepted by the split utility v2 endpoint.
class SplitParameters < Mindee::V2::ClientOptions::BaseProductParameters
class SplitParameters < Mindee::V2::ClientOptions::BaseClassifierProductParameters
# @return [String] Slug for the endpoint.
def self.slug
'split'
Expand All @@ -18,11 +18,14 @@ def self.slug
# @param [String, nil] file_alias File alias, if applicable.
# @param [Array<String>, nil] webhook_ids List of webhook IDs to propagate the API response to.
# @param [Boolean, nil] close_file Whether to close the file after parsing.
# @param [String, nil] extraction_text_context Pass additional context to the extraction model chained
# to this utility inference.
def initialize(
model_id,
file_alias: nil,
webhook_ids: nil,
close_file: true
close_file: true,
extraction_text_context: nil
)
super
end
Expand All @@ -35,7 +38,8 @@ def self.from_hash(params: {})
params.fetch(:model_id),
file_alias: params.fetch(:file_alias, nil),
webhook_ids: params.fetch(:webhook_ids, nil),
close_file: params.fetch(:close_file, true)
close_file: params.fetch(:close_file, true),
extraction_text_context: params.fetch(:extraction_text_context, nil)
)
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# lib/mindee/v2/client_options/base_classifier_product_parameters.rb

module Mindee
module V2
module ClientOptions
class BaseClassifierProductParameters < BaseProductParameters
def self.from_hash: (params: Hash[String | Symbol, untyped]) -> BaseClassifierProductParameters

def self.load_from_hash: (params: Hash[String | Symbol, untyped]) -> Hash[String | Symbol, untyped]

attr_reader extraction_text_context: String?

def initialize: (
String,
?file_alias: String?,
?webhook_ids: Array[String]?,
?close_file: bool?,
?extraction_text_context: String?
) -> void

def request_parameters: -> Hash[String, String | Array[String]]
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Mindee
module Product
module Classification
module Params
class ClassificationParameters < ClientOptions::BaseProductParameters
class ClassificationParameters < ClientOptions::BaseClassifierProductParameters
def self.slug: -> String

def self.from_hash: (params: Hash[String | Symbol, untyped]) -> ClassificationParameters
Expand All @@ -12,7 +12,8 @@ module Mindee
String,
?file_alias: String?,
?webhook_ids: Array[String]?,
?close_file: bool?
?close_file: bool?,
?extraction_text_context: String?
) -> void
end
end
Expand Down
5 changes: 3 additions & 2 deletions sig/mindee/v2/product/crop/params/crop_parameters.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Mindee
module Product
module Crop
module Params
class CropParameters < ClientOptions::BaseProductParameters
class CropParameters < ClientOptions::BaseClassifierProductParameters
def self.slug: -> String

def self.from_hash: (params: Hash[String | Symbol, untyped]) -> CropParameters
Expand All @@ -12,7 +12,8 @@ module Mindee
String,
?file_alias: String?,
?webhook_ids: Array[String]?,
?close_file: bool?
?close_file: bool?,
?extraction_text_context: String?
) -> void
end
end
Expand Down
5 changes: 3 additions & 2 deletions sig/mindee/v2/product/split/params/split_parameters.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Mindee
module Product
module Split
module Params
class SplitParameters < ClientOptions::BaseProductParameters
class SplitParameters < ClientOptions::BaseClassifierProductParameters
def self.slug: -> String

def self.from_hash: (params: Hash[String | Symbol, untyped]) -> SplitParameters
Expand All @@ -12,7 +12,8 @@ module Mindee
String,
?file_alias: String?,
?webhook_ids: Array[String]?,
?close_file: bool?
?close_file: bool?,
?extraction_text_context: String?
) -> void
end
end
Expand Down
61 changes: 61 additions & 0 deletions spec/v2/product/classification/classification_parameter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require 'mindee'
require 'mindee/v2/product/classification/params/classification_parameters'

describe Mindee::V2::Product::Classification::Params::ClassificationParameters do
let(:model_id) { 'test-model-id' }

it 'should init with only required parameters' do
product_params = described_class.new(model_id)

request_params = product_params.request_parameters
expect(request_params['model_id']).to eq(model_id)
expect(request_params).not_to have_key('alias')
expect(request_params).not_to have_key('webhook_ids')
expect(request_params).not_to have_key('extraction_text_context')
end

it 'should init with all parameters' do
product_params = described_class.new(
model_id,
file_alias: 'test-alias',
webhook_ids: ['webhook-1', 'webhook-2'],
extraction_text_context: 'context'
)

request_params = product_params.request_parameters
expect(request_params['model_id']).to eq(model_id)
expect(request_params['alias']).to eq('test-alias')
expect(request_params['webhook_ids']).to eq('webhook-1,webhook-2')
expect(request_params['extraction_text_context']).to eq('context')
end

it 'should load from a hash' do
product_params = described_class.from_hash(
params: {
model_id: model_id,
file_alias: 'test-alias',
webhook_ids: ['webhook-1', 'webhook-2'],
extraction_text_context: 'context',
}
)

expect(product_params.model_id).to eq(model_id)
expect(product_params.file_alias).to eq('test-alias')
expect(product_params.webhook_ids).to eq(['webhook-1', 'webhook-2'])
expect(product_params.extraction_text_context).to eq('context')
end

it 'should raise an error if extraction_text_context is not a string' do
expect do
described_class.new(model_id, extraction_text_context: 123)
end.to raise_error(Mindee::Error::MindeeInputError, 'Extraction text context must be a string.')
end

it 'should raise an error if model_id is empty' do
expect do
described_class.new('')
end.to raise_error(Mindee::Error::MindeeInputError, 'Model ID is required.')
end
end
Loading
Loading