Class: Mindee::Image::ExtractedImage
- Inherits:
-
Object
- Object
- Mindee::Image::ExtractedImage
- Defined in:
- lib/mindee/image/extracted_image.rb
Overview
Generic class for image extraction.
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Buffer object of the file’s content.
-
#element_id ⇒ Object
readonly
ID of the element on a given page.
-
#internal_file_name ⇒ Object
readonly
Internal name for the file.
-
#page_id ⇒ Object
readonly
ID of the page the image was extracted from.
Instance Method Summary collapse
-
#as_source ⇒ FileInputSource
Return the file as a Mindee-compatible BufferInput source.
-
#initialize(input_source, page_id, element_id) ⇒ ExtractedImage
constructor
Initializes the ExtractedImage with a buffer and an internal file name.
-
#write_to_file(output_path, file_format = nil) ⇒ Object
Saves the document to a file.
Constructor Details
#initialize(input_source, page_id, element_id) ⇒ ExtractedImage
Initializes the ExtractedImage with a buffer and an internal file name.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mindee/image/extracted_image.rb', line 28 def initialize(input_source, page_id, element_id) @buffer = StringIO.new(input_source.io_stream.read.to_s) @buffer.rewind extension = if input_source.pdf? '.jpg' else File.extname(input_source.filename) end base_name = File.basename(input_source.filename, File.extname(input_source.filename)) @internal_file_name = "#{base_name}_p#{page_id}_#{element_id}#{extension}" @page_id = page_id @element_id = element_id.nil? ? 0 : element_id end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Buffer object of the file’s content.
18 19 20 |
# File 'lib/mindee/image/extracted_image.rb', line 18 def buffer @buffer end |
#element_id ⇒ Object (readonly)
ID of the element on a given page.
15 16 17 |
# File 'lib/mindee/image/extracted_image.rb', line 15 def element_id @element_id end |
#internal_file_name ⇒ Object (readonly)
Internal name for the file.
21 22 23 |
# File 'lib/mindee/image/extracted_image.rb', line 21 def internal_file_name @internal_file_name end |
#page_id ⇒ Object (readonly)
ID of the page the image was extracted from.
12 13 14 |
# File 'lib/mindee/image/extracted_image.rb', line 12 def page_id @page_id end |
Instance Method Details
#as_source ⇒ FileInputSource
Return the file as a Mindee-compatible BufferInput source.
70 71 72 73 |
# File 'lib/mindee/image/extracted_image.rb', line 70 def as_source @buffer.rewind Mindee::Input::Source::BytesInputSource.new(@buffer.read, @internal_file_name) end |
#write_to_file(output_path, file_format = nil) ⇒ Object
Saves the document to a file.
extension if not provided.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mindee/image/extracted_image.rb', line 48 def write_to_file(output_path, file_format = nil) resolved_path = Pathname.new(File.(output_path)) if file_format.nil? raise Errors::MindeeImageError, 'Invalid file format.' if resolved_path.extname.delete('.').empty? file_format = resolved_path.extname.delete('.').upcase end begin @buffer.rewind image = MiniMagick::Image.read(@buffer) image.format file_format.to_s.downcase image.write resolved_path.to_s logger.info("File saved successfully to '#{resolved_path}'") rescue StandardError raise Errors::MindeeImageError, "Could not save file '#{output_path}'. " \ 'Is the provided file path valid?.' end end |