Class: Mindee::Parsing::Common::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/parsing/common/document.rb

Overview

Stores all response attributes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product_class, http_response) ⇒ Document

Returns a new instance of Document.

Parameters:

  • product_class (Mindee::Inference)
  • http_response (Hash)


46
47
48
49
50
51
52
53
54
# File 'lib/mindee/parsing/common/document.rb', line 46

def initialize(product_class, http_response)
  @id = http_response['id']
  @name = http_response['name']
  @inference = product_class.new(http_response['inference'])
  @ocr = self.class.load_ocr(http_response)
  @extras = self.class.extract_extras(http_response)
  inject_full_text_ocr(http_response)
  @n_pages = http_response['n_pages']
end

Instance Attribute Details

#extrasMindee::Parsing::Common::Extras::Extras (readonly)

Returns Potential Extras fields sent back along the prediction.

Returns:



18
19
20
# File 'lib/mindee/parsing/common/document.rb', line 18

def extras
  @extras
end

#idString (readonly)

Returns Mindee ID of the document.

Returns:

  • (String)

    Mindee ID of the document



16
17
18
# File 'lib/mindee/parsing/common/document.rb', line 16

def id
  @id
end

#inferenceMindee::Inference (readonly)

Returns:

  • (Mindee::Inference)


12
13
14
# File 'lib/mindee/parsing/common/document.rb', line 12

def inference
  @inference
end

#n_pagesInteger (readonly)

Returns Amount of pages of the document.

Returns:

  • (Integer)

    Amount of pages of the document



22
23
24
# File 'lib/mindee/parsing/common/document.rb', line 22

def n_pages
  @n_pages
end

#nameString (readonly)

Returns Filename sent to the API.

Returns:

  • (String)

    Filename sent to the API



14
15
16
# File 'lib/mindee/parsing/common/document.rb', line 14

def name
  @name
end

#ocrMindee::Parsing::Common::OCR::OCR? (readonly)

Returns OCR text results (limited availability).

Returns:



20
21
22
# File 'lib/mindee/parsing/common/document.rb', line 20

def ocr
  @ocr
end

Class Method Details

.extract_extras(http_response) ⇒ Mindee::Parsing::Common::OCR::OCR

Loads extras into the document prediction.

Parameters:

  • http_response (Hash)

    Full HTTP contents of the response.

Returns:



37
38
39
40
41
42
# File 'lib/mindee/parsing/common/document.rb', line 37

def self.extract_extras(http_response)
  extras_prediction = http_response['inference'].fetch('extras', nil)
  return nil if extras_prediction.nil? || extras_prediction.fetch('mvision-v1', nil).nil?

  Mindee::Parsing::Common::Extras::Extras.new(extras_prediction)
end

.load_ocr(http_response) ⇒ Mindee::Parsing::Common::OCR::OCR

Loads the MVision OCR response.

Parameters:

  • http_response (Hash)

    Full HTTP contents of the response.

Returns:



27
28
29
30
31
32
# File 'lib/mindee/parsing/common/document.rb', line 27

def self.load_ocr(http_response)
  ocr_prediction = http_response.fetch('ocr', nil)
  return nil if ocr_prediction.nil? || ocr_prediction.fetch('mvision-v1', nil).nil?

  OCR::OCR.new(ocr_prediction)
end

Instance Method Details

#to_sString

Returns:

  • (String)


57
58
59
60
61
62
63
# File 'lib/mindee/parsing/common/document.rb', line 57

def to_s
  out_str = String.new
  out_str << "########\nDocument\n########"
  out_str << "\n:Mindee ID: #{@id}"
  out_str << "\n:Filename: #{@name}"
  out_str << "\n\n#{@inference}"
end