diff --git a/.github/workflows/_test-cli.yml b/.github/workflows/_test-cli.yml index d8c2581b..2743f279 100644 --- a/.github/workflows/_test-cli.yml +++ b/.github/workflows/_test-cli.yml @@ -14,7 +14,7 @@ jobs: name: Run Tests timeout-minutes: 30 strategy: - max-parallel: 4 + max-parallel: 6 matrix: os_config: - os: "ubuntu-24.04" diff --git a/.github/workflows/_test-integration-lite.yml b/.github/workflows/_test-integration-lite.yml index f283269a..aaef5aac 100644 --- a/.github/workflows/_test-integration-lite.yml +++ b/.github/workflows/_test-integration-lite.yml @@ -27,7 +27,7 @@ jobs: timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: - max-parallel: 3 + max-parallel: 4 matrix: os: - "ubuntu-24.04" diff --git a/.github/workflows/_test-integration.yml b/.github/workflows/_test-integration.yml index 32e06f69..9ac72cf2 100644 --- a/.github/workflows/_test-integration.yml +++ b/.github/workflows/_test-integration.yml @@ -26,7 +26,7 @@ jobs: timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: - max-parallel: 3 + max-parallel: 4 matrix: os: - "ubuntu-24.04" diff --git a/lib/mindee/v2/parsing/field/simple_field.rb b/lib/mindee/v2/parsing/field/simple_field.rb index 2bc8f675..9913ba65 100644 --- a/lib/mindee/v2/parsing/field/simple_field.rb +++ b/lib/mindee/v2/parsing/field/simple_field.rb @@ -38,6 +38,38 @@ def to_s end end + # Retrieves the field value as a Float. + # @return [Float, nil] + # @raise [RuntimeError] If the value is not a Float. + def float_value + raise "Value is not a float: #{@value.class}" unless @value.nil? || @value.is_a?(Float) + + val = @value + val.is_a?(Float) ? val : nil # @type var val: Float | nil + end + + # Retrieves the field value as a String. + # @return [String, nil] + # @raise [RuntimeError] If the value is not a String. + def string_value + raise "Value is not a string: #{@value.class}" unless @value.nil? || @value.is_a?(String) + + val = @value + val.is_a?(String) ? val : nil # @type var val: String | nil + end + + # Retrieves the field value as a Boolean. + # @return [Boolean, nil] + # @raise [RuntimeError] If the value is not a Boolean. + def boolean_value + unless @value.nil? || @value.is_a?(TrueClass) || @value.is_a?(FalseClass) + raise "Value is not a boolean: #{@value.class}" + end + + val = @value + @value.is_a?(TrueClass) || @value.is_a?(FalseClass) ? val : nil # @type var val: bool | nil + end + private # Format numeric values to display '.0' in string reps. diff --git a/sig/mindee/v2/parsing/field/simple_field.rbs b/sig/mindee/v2/parsing/field/simple_field.rbs index b0a35b29..299d7873 100644 --- a/sig/mindee/v2/parsing/field/simple_field.rbs +++ b/sig/mindee/v2/parsing/field/simple_field.rbs @@ -4,9 +4,13 @@ module Mindee module Parsing module Field class SimpleField < BaseField - attr_reader value: String | Integer | Float | bool | nil + attr_reader value: String | Float | bool | nil def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void + def boolean_value: -> (bool | nil) + def float_value: -> (Float | nil) + def string_value: -> (String | nil) + def to_s: -> String def format_numeric_value: (Integer | Float) -> String end diff --git a/spec/v2/client_v2_integration.rb b/spec/v2/client_v2_integration.rb index 8cda5f3c..062ff853 100644 --- a/spec/v2/client_v2_integration.rb +++ b/spec/v2/client_v2_integration.rb @@ -112,7 +112,7 @@ fields = result.fields expect(fields).not_to be_nil expect(fields['supplier_name']).not_to be_nil - expect(fields['supplier_name'].value).to eq('John Smith') + expect(fields['supplier_name'].string_value).to eq('John Smith') end end context 'An error' do @@ -266,7 +266,7 @@ fields = result.fields expect(fields).not_to be_nil expect(fields['test_replace']).not_to be_nil - expect(fields['test_replace'].value).to eq('a test value') + expect(fields['test_replace'].string_value).to eq('a test value') end end end diff --git a/spec/v2/product/classification/classification_spec.rb b/spec/v2/product/classification/classification_spec.rb index a2bd5673..0d02015f 100644 --- a/spec/v2/product/classification/classification_spec.rb +++ b/spec/v2/product/classification/classification_spec.rb @@ -30,7 +30,7 @@ expect(classification.extraction_response).to be_a(Mindee::V2::Product::Extraction::ExtractionResponse) expect( - classification.extraction_response.inference.result.fields.get_simple_field('customer_name').value + classification.extraction_response.inference.result.fields.get_simple_field('customer_name').string_value ).to eq('Jiro Doi') end end diff --git a/spec/v2/product/crop/crop_spec.rb b/spec/v2/product/crop/crop_spec.rb index 0c828834..4c10da76 100644 --- a/spec/v2/product/crop/crop_spec.rb +++ b/spec/v2/product/crop/crop_spec.rb @@ -100,7 +100,7 @@ extraction_response0 = crop0.extraction_response expect(extraction_response0).not_to be_nil expect( - extraction_response0.inference.result.fields.get_simple_field('supplier_name').value + extraction_response0.inference.result.fields.get_simple_field('supplier_name').string_value ).to eq('CHEZ ALAIN MIAM MIAM') crop1 = crops[1] @@ -110,7 +110,7 @@ extraction_response1 = crop1.extraction_response expect(extraction_response1).not_to be_nil expect( - extraction_response1.inference.result.fields.get_simple_field('supplier_name').value + extraction_response1.inference.result.fields.get_simple_field('supplier_name').string_value ).to eq('La cerise sur la pizza') end end diff --git a/spec/v2/product/extraction/extraction_spec.rb b/spec/v2/product/extraction/extraction_spec.rb index 4a676f19..a72f7bfa 100644 --- a/spec/v2/product/extraction/extraction_spec.rb +++ b/spec/v2/product/extraction/extraction_spec.rb @@ -124,11 +124,11 @@ def load_v2_extraction_inference(resource_path) expect(line_items).to be_a(list_field) first_line_item = line_items.object_items[0] expect(first_line_item).to be_a(object_field) - expect(first_line_item.get_simple_field('quantity').value).to eq(1.0) + expect(first_line_item.get_simple_field('quantity').float_value).to eq(1.0) base_field = tax_item_obj.fields.get_simple_field('base') expect(base_field).to be_a(simple_field) - expect(base_field.value).to eq(31.5) + expect(base_field.float_value).to eq(31.5) expect(fields).to have_key('supplier_address') supplier_address = fields.get_object_field('supplier_address') @@ -138,14 +138,14 @@ def load_v2_extraction_inference(resource_path) country_field = supplier_address.fields.get_simple_field('country') expect(country_field).to be_a(simple_field) - expect(country_field.value).to eq('USA') + expect(country_field.string_value).to eq('USA') expect(country_field.to_s).to eq('USA') customer_addr = fields.get_object_field('customer_address') expect(customer_addr).to be_a(object_field) city_field = customer_addr.fields.get_simple_field('city') expect(city_field).to be_a(simple_field) - expect(city_field.value).to eq('New York') + expect(city_field.string_value).to eq('New York') expect(inference.result.raw_text).to be_nil end @@ -213,25 +213,32 @@ def load_standard_fields field_simple_string = fields.get_simple_field('field_simple_string') expect(field_simple_string).to be_a(simple_field) expect(field_simple_string.value).to eq('field_simple_string-value') + expect(field_simple_string.string_value).to eq('field_simple_string-value') expect(field_simple_string.confidence).to eq(field_confidence::CERTAIN) expect(field_simple_string.to_s).to eq('field_simple_string-value') field_simple_int = fields.get_simple_field('field_simple_int') expect(field_simple_int).to be_a(simple_field) expect(field_simple_int.value).to be_a(Float) + expect(field_simple_int.float_value).to eq(12.0) field_simple_float = fields.get_simple_field('field_simple_float') expect(field_simple_float).to be_a(simple_field) expect(field_simple_float.value).to be_a(Float) + expect(field_simple_float.value).to eq(1.1) field_simple_bool = fields.get_simple_field('field_simple_bool') expect(field_simple_bool).to be_a(simple_field) expect(field_simple_bool.value).to eq(true) + expect(field_simple_bool.boolean_value).to eq(true) expect(field_simple_bool.to_s).to eq('True') field_simple_null = fields.get_simple_field('field_simple_null') expect(field_simple_null).to be_a(simple_field) expect(field_simple_null.value).to be_nil + expect(field_simple_null.string_value).to be_nil + expect(field_simple_null.float_value).to be_nil + expect(field_simple_null.boolean_value).to be_nil expect(field_simple_null.to_s).to eq('') end diff --git a/spec/v2/product/split/split_spec.rb b/spec/v2/product/split/split_spec.rb index a423738e..93722ca4 100644 --- a/spec/v2/product/split/split_spec.rb +++ b/spec/v2/product/split/split_spec.rb @@ -66,7 +66,7 @@ extraction_response0 = split0.extraction_response expect(extraction_response0).not_to be_nil expect( - extraction_response0.inference.result.fields.get_simple_field('supplier_phone_number').value + extraction_response0.inference.result.fields.get_simple_field('supplier_phone_number').string_value ).to eq('05 05 44 44 90') split1 = splits[1] @@ -75,7 +75,7 @@ extraction_response1 = split1.extraction_response expect(extraction_response1).not_to be_nil expect( - extraction_response1.inference.result.fields.get_simple_field('supplier_phone_number').value + extraction_response1.inference.result.fields.get_simple_field('supplier_phone_number').string_value ).to eq('416-555-1212') end end