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 lib/mindee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module Mindee
# Dependency management
module Dependency
module Dependencies
end

# Mindee internal error module.
Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/dependency.rb → lib/mindee/dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Mindee
# Centralized check for optional heavy dependencies
module Dependency
module Dependencies
def self.check_all_dependencies
require 'origami'
require 'mini_magick'
Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/image/image_extractor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Mindee::Dependency.require_all_deps!
Mindee::Dependencies.require_all_deps!
require 'mini_magick'
require 'origami'
require 'stringio'
Expand Down
18 changes: 9 additions & 9 deletions lib/mindee/input/sources/local_input_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
require 'marcel'
require 'fileutils'

require_relative '../../dependency'
require_relative '../../pdf' if Mindee::Dependency.all_deps_available?
require_relative '../../image' if Mindee::Dependency.all_deps_available?
require_relative '../../dependencies'
require_relative '../../pdf' if Mindee::Dependencies.all_deps_available?
require_relative '../../image' if Mindee::Dependencies.all_deps_available?

module Mindee
module Input
Expand Down Expand Up @@ -143,8 +143,8 @@ def write_to_file(path)
# Defaults to one for images.
# @return [Integer]
def page_count
unless Mindee::Dependency.all_deps_available?
raise NotImplementedError, Mindee::Dependency::MINDEE_DEPENDENCIES_LOAD_ERROR
unless Mindee::Dependencies.all_deps_available?
raise NotImplementedError, Mindee::Dependencies::MINDEE_DEPENDENCIES_LOAD_ERROR
end
return 1 unless pdf?

Expand All @@ -163,8 +163,8 @@ def page_count
# @param [bool] disable_source_text If the PDF has source text, whether to re-apply it to the original or
# not. Needs force_source_text to work.
def compress!(quality: 85, max_width: nil, max_height: nil, force_source_text: false, disable_source_text: true)
unless Mindee::Dependency.all_deps_available?
raise NotImplementedError, Mindee::Dependency::MINDEE_DEPENDENCIES_LOAD_ERROR
unless Mindee::Dependencies.all_deps_available?
raise NotImplementedError, Mindee::Dependencies::MINDEE_DEPENDENCIES_LOAD_ERROR
end

buffer = if pdf?
Expand All @@ -189,8 +189,8 @@ def compress!(quality: 85, max_width: nil, max_height: nil, force_source_text: f
# Checks whether the file has source text if it is a pdf. `false` otherwise
# @return [bool] `true` if the file is a PDF and has source text.
def source_text?
unless Mindee::Dependency.all_deps_available?
raise NotImplementedError, Mindee::Dependency::MINDEE_DEPENDENCIES_LOAD_ERROR
unless Mindee::Dependencies.all_deps_available?
raise NotImplementedError, Mindee::Dependencies::MINDEE_DEPENDENCIES_LOAD_ERROR
end

Mindee::PDF::PDFTools.source_text?(@io_stream)
Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/pdf/pdf_compressor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Mindee::Dependency.require_all_deps!
Mindee::Dependencies.require_all_deps!
require 'pdf-reader'

# Shorthand for pdf-reader's PDF namespace, to avoid mixups with the local Origami fork.
Expand Down
8 changes: 4 additions & 4 deletions lib/mindee/pdf/pdf_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module PDF
class PDFExtractor
# @param local_input [Mindee::Input::Source::LocalInputSource]
def initialize(local_input)
unless Mindee::Dependency.all_deps_available?
raise NotImplementedError, Mindee::Dependency::MINDEE_DEPENDENCIES_LOAD_ERROR
unless Mindee::Dependencies.all_deps_available?
raise NotImplementedError, Mindee::Dependencies::MINDEE_DEPENDENCIES_LOAD_ERROR
end

@filename = local_input.filename
Expand Down Expand Up @@ -82,14 +82,14 @@ def extract_invoices(page_indexes, strict: false)
page_indexes_as_array = page_indexes # @type var page_indexes : Array[Array[Integer]]
return extract_sub_documents(page_indexes_as_array)
end
p_ids = page_indexes # @type var page_indexes: Product::InvoiceSplitter::InvoiceSplitterV1InvoicePageGroups
p_ids = page_indexes # @type var page_indexes: Mindee::V1::Product::InvoiceSplitter::InvoiceSplitterV1InvoicePageGroups
return extract_sub_documents(p_ids.map(&:page_indexes)) unless strict

correct_page_indexes = [] # @type var correct_page_indexes: Array[Array[Integer]]
current_list = [] # @type var current_list: Array[Integer]
previous_confidence = nil
p_ids.each_with_index do |p_i, i|
page_index = p_i # @type var page_index: Product::InvoiceSplitter::InvoiceSplitterV1InvoicePageGroup
page_index = p_i # @type var page_index: Mindee::V1::Product::InvoiceSplitter::InvoiceSplitterV1InvoicePageGroup
confidence = page_index.confidence.to_f
page_list = page_index.page_indexes

Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/pdf/pdf_processor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Mindee::Dependency.require_all_deps!
Mindee::Dependencies.require_all_deps!
require 'origami'
require_relative 'pdf_tools'

Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/pdf/pdf_tools.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Mindee::Dependency.require_all_deps!
Mindee::Dependencies.require_all_deps!
require 'origami'

module Mindee
Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/v1.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative 'v1/client'
require_relative 'v1/extraction' if Mindee::Dependency.all_deps_available?
require_relative 'v1/extraction' if Mindee::Dependencies.all_deps_available?
require_relative 'v1/http'
require_relative 'v1/parsing'
require_relative 'v1/product'
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class InvoiceSplitterV1Document < Mindee::V1::Parsing::Common::Prediction
# @param page_id [Integer, nil]
def initialize(prediction, page_id)
super
@invoice_page_groups = Product::InvoiceSplitter::InvoiceSplitterV1InvoicePageGroups.new(
@invoice_page_groups = InvoiceSplitterV1InvoicePageGroups.new(
prediction['invoice_page_groups'], page_id
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require_relative 'v2/client'
require_relative 'v2/http'
require_relative 'v2/file_operation' if Mindee::Dependency.all_deps_available?
require_relative 'v2/file_operations' if Mindee::Dependencies.all_deps_available?
require_relative 'v2/parsing'
require_relative 'v2/product'
6 changes: 0 additions & 6 deletions lib/mindee/v2/file_operation.rb

This file was deleted.

6 changes: 6 additions & 0 deletions lib/mindee/v2/file_operations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require_relative 'file_operations/crop'
require_relative 'file_operations/crop_files'
require_relative 'file_operations/split'
require_relative 'file_operations/split_files'
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def self.extract_single_crop(input_source, crop)
).first
end

# Extracts individual receipts from multi-receipts documents.
# Extracts multiple crop zones from an image.
#
# @param input_source [LocalInputSource] Local Input Source to extract sub-receipts from.
# @param crops [Array<CropItem>] List of crops.
# @return [CropFiles] Individual extracted receipts as an array of ExtractedImage.
# @return [CropFiles] Individual extracted zones as an array of ExtractedImage.
# @raise [MindeeError] if the crops array is empty.
def self.extract_crops(input_source, crops)
if crops.nil? || crops.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/v2/product/split/split_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def to_s
@inference.to_s
end

# Extracts the crops from the input source.
# Splits the input PDF.
# @param input_source [Mindee::Input::Source::LocalInputSource] Path to the file or a File object.
# @return [FileOperation::SplitFiles]
def extract_from_file(input_source)
Expand Down
2 changes: 1 addition & 1 deletion sig/mindee/dependency.rbs → sig/mindee/dependencies.rbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mindee
module Dependency
module Dependencies
MINDEE_DEPENDENCIES_LOAD_ERROR: String

self.@all_deps_available: bool
Expand Down
2 changes: 1 addition & 1 deletion sig/mindee/input/base_parameters.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Mindee
class BaseParameters
attr_reader self.slug: String

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

def slug: -> String
Expand Down
2 changes: 1 addition & 1 deletion sig/mindee/pdf/pdf_extractor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Mindee

def extract_sub_documents: (Array[Array[Integer]]) -> Array[ExtractedPDF]

def extract_invoices: (Array[V1::Product::InvoiceSplitter::InvoiceSplitterV1InvoicePageGroup] | Array[Array[Integer]], ?strict: bool) -> Array[ExtractedPDF]
def extract_invoices: (Array[Mindee::V1::Product::InvoiceSplitter::InvoiceSplitterV1InvoicePageGroup] | Array[Array[Integer]], ?strict: bool) -> Array[ExtractedPDF]
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mindee
module InvoiceSplitter
class InvoiceSplitterV1Document < Parsing::Common::Prediction
def initialize: (Hash[String | Symbol, untyped], Integer?) -> void
def invoice_page_groups: -> (Product::InvoiceSplitter::InvoiceSplitterV1InvoicePageGroups)
def invoice_page_groups: -> InvoiceSplitterV1InvoicePageGroups
def invoice_page_groups_separator: (String) -> String
def invoice_page_groups_to_s: -> String
def to_s: -> String
Expand Down
24 changes: 12 additions & 12 deletions spec/dependency_spec.rb → spec/dependencies_spec.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
# frozen_string_literal: true

require 'mindee'
describe Mindee::Dependency do
describe Mindee::Dependencies do
before(:each) do
if Mindee::Dependency.instance_variable_defined?(:@all_deps_available)
Mindee::Dependency.remove_instance_variable(:@all_deps_available)
if Mindee::Dependencies.instance_variable_defined?(:@all_deps_available)
Mindee::Dependencies.remove_instance_variable(:@all_deps_available)
end
end

describe '.all_deps_available?' do
context 'when evaluating the full mindee gem' do
before do
allow(Mindee::Dependency).to receive(:require).and_return(true)
allow(Mindee::Dependencies).to receive(:require).and_return(true)

Mindee::Dependency.instance_variable_set(:@all_deps_available, Mindee::Dependency.check_all_dependencies)
Mindee::Dependencies.instance_variable_set(:@all_deps_available, Mindee::Dependencies.check_all_dependencies)
end

it 'returns true' do
expect(Mindee::Dependency.all_deps_available?).to be true
expect(Mindee::Dependencies.all_deps_available?).to be true
end
end

context 'when evaluating the mindee-lite gem' do
before do
allow(Mindee::Dependency).to receive(:require).and_raise(LoadError)
allow(Mindee::Dependencies).to receive(:require).and_raise(LoadError)

Mindee::Dependency.instance_variable_set(:@all_deps_available, Mindee::Dependency.check_all_dependencies)
Mindee::Dependencies.instance_variable_set(:@all_deps_available, Mindee::Dependencies.check_all_dependencies)
end

it 'returns false' do
expect(Mindee::Dependency.all_deps_available?).to be false
expect(Mindee::Dependencies.all_deps_available?).to be false
end
end
end
Expand All @@ -40,13 +40,13 @@

context 'when initialized in a mindee-lite environment' do
before do
allow(Mindee::Dependency).to receive(:all_deps_available?).and_return(false)
allow(Mindee::Dependencies).to receive(:all_deps_available?).and_return(false)
end

it 'raises a LoadError with the lite exception message' do
expect do
load pdf_tools_module_path
end.to raise_error(LoadError, Mindee::Dependency::MINDEE_DEPENDENCIES_LOAD_ERROR)
end.to raise_error(LoadError, Mindee::Dependencies::MINDEE_DEPENDENCIES_LOAD_ERROR)
end
end

Expand All @@ -66,7 +66,7 @@
end

before do
allow(Mindee::Dependency).to receive(:all_deps_available?).and_return(true)
allow(Mindee::Dependencies).to receive(:all_deps_available?).and_return(true)
end

it 'loads the module successfully without raising errors' do
Expand Down
4 changes: 2 additions & 2 deletions spec/image/extracted_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
require_relative '../data'

describe 'Mindee::Image::ExtractedImage', :all_deps do
require 'mini_magick' if Mindee::Dependency.all_deps_available?
require 'mini_magick' if Mindee::Dependencies.all_deps_available?
# Workaround for mindee-lite
if Mindee::Dependency.all_deps_available?
if Mindee::Dependencies.all_deps_available?
let(:described_class) do
Mindee::Image::ExtractedImage
end
Expand Down
2 changes: 1 addition & 1 deletion spec/image/image_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'mindee'

describe 'Mindee::Image::ImageUtils', :all_deps do
require 'mini_magick' if Mindee::Dependency.all_deps_available?
require 'mini_magick' if Mindee::Dependencies.all_deps_available?
let(:sample_image_path) { "#{FILE_TYPES_DIR}/receipt.jpg" }
let(:sample_image) { MiniMagick::Image.open(sample_image_path) }

Expand Down
10 changes: 5 additions & 5 deletions spec/input/sources/sources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'mindee'
require 'mindee/input/sources'
require 'mindee/error'
require 'pdf-reader' if Mindee::Dependency.all_deps_available?
require 'pdf-reader' if Mindee::Dependencies.all_deps_available?

require_relative '../../data'

Expand All @@ -15,15 +15,15 @@
)
expect(input_source.file_mimetype).to eq('image/jpeg')
expect(input_source.filename).to eq('receipt.jpg')
expect(input_source.page_count).to eq(1) if Mindee::Dependency.all_deps_available?
expect(input_source.page_count).to eq(1) if Mindee::Dependencies.all_deps_available?
expect(input_source.pdf?).to eq(false)

input_source = Mindee::Input::Source::PathInputSource.new(
File.join(FILE_TYPES_DIR, 'receipt.jpga')
)
expect(input_source.file_mimetype).to eq('image/jpeg')
expect(input_source.filename).to eq('receipt.jpga')
expect(input_source.page_count).to eq(1) if Mindee::Dependency.all_deps_available?
expect(input_source.page_count).to eq(1) if Mindee::Dependencies.all_deps_available?
expect(input_source.pdf?).to eq(false)
end

Expand All @@ -33,15 +33,15 @@
)
expect(input_source.file_mimetype).to eq('image/tiff')
expect(input_source.filename).to eq('receipt.tif')
expect(input_source.page_count).to eq(1) if Mindee::Dependency.all_deps_available?
expect(input_source.page_count).to eq(1) if Mindee::Dependencies.all_deps_available?
expect(input_source.pdf?).to eq(false)

input_source = Mindee::Input::Source::PathInputSource.new(
File.join(FILE_TYPES_DIR, 'receipt.tiff')
)
expect(input_source.file_mimetype).to eq('image/tiff')
expect(input_source.filename).to eq('receipt.tiff')
expect(input_source.page_count).to eq(1) if Mindee::Dependency.all_deps_available?
expect(input_source.page_count).to eq(1) if Mindee::Dependencies.all_deps_available?
expect(input_source.pdf?).to eq(false)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/pdf/extracted_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe 'Mindee::PDF::ExtractedPDF', :all_deps do
# Workaround for mindee-lite
if Mindee::Dependency.all_deps_available?
if Mindee::Dependencies.all_deps_available?
let(:described_class) do
Mindee::PDF::ExtractedPDF
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require_relative '../../data'
require_relative '../../test_utilities'

if Mindee::Dependency.all_deps_available? # Can't be bypassed by tag as otherwise it will try to load the PDF module.
if Mindee::Dependencies.all_deps_available? # Can't be bypassed by tag as otherwise it will try to load the PDF module.
describe 'PDF Invoice Extraction (Strict Mode)', :all_deps do
let(:invoice_splitter_5p_path) { File.join(V1_PRODUCT_DATA_DIR, 'invoice_splitter', 'invoice_5p.pdf') }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'mindee'
require 'mindee/v2/file_operation'
require 'mindee/v2/file_operations'
require 'mindee/v2/product'

describe Mindee::V2::FileOperation::Crop, :integration, :v2, :all_deps do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require 'json'
require 'mini_magick' if Mindee::Dependency.all_deps_available?
require 'mini_magick' if Mindee::Dependencies.all_deps_available?
require 'mindee'
require 'mindee/v2/file_operation'
require 'mindee/v2/file_operations'
require 'mindee/v2/product'

describe Mindee::V2::FileOperation::Crop, :v2, :all_deps do
Expand Down
Loading
Loading