Module: OcrFile::FileHelpers
Instance Method Summary collapse
- #append_file(path, text) ⇒ Object
-
#clear_folder(path) ⇒ Object
Beware this is dangerous!.
- #fetch_temp_image_paths(save_path, temp_filename, filetype) ⇒ Object
- #make_directory(path) ⇒ Object
- #merge_pdfs(file_paths, save_file_path) ⇒ Object
- #open_json(path) ⇒ Object
- #open_text_file(path) ⇒ Object
Instance Method Details
#append_file(path, text) ⇒ Object
25 26 27 |
# File 'lib/ocr-file/file_helpers.rb', line 25 def append_file(path, text) File.open(path, 'a') { |file| file.write(text) } end |
#clear_folder(path) ⇒ Object
Beware this is dangerous!
12 13 14 15 |
# File 'lib/ocr-file/file_helpers.rb', line 12 def clear_folder(path) return unless path.include?('/temp') # Small hacky safeguard `rm -rf #{path}` # Cleanup end |
#fetch_temp_image_paths(save_path, temp_filename, filetype) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/ocr-file/file_helpers.rb', line 33 def fetch_temp_image_paths(save_path, temp_filename, filetype) filenames = `ls #{save_path} | grep .#{filetype}`.split("\n") filenames.map do |filename| "#{save_path}/#{filename}" end end |
#make_directory(path) ⇒ Object
17 18 19 |
# File 'lib/ocr-file/file_helpers.rb', line 17 def make_directory(path) `mkdir -p #{path}` end |
#merge_pdfs(file_paths, save_file_path) ⇒ Object
5 6 7 8 9 |
# File 'lib/ocr-file/file_helpers.rb', line 5 def merge_pdfs(file_paths, save_file_path) documents = file_paths.map { |path| OcrFile::ImageEngines::PdfEngine.open_pdf(path) } merged_document = OcrFile::ImageEngines::PdfEngine.merge(documents) save_pdf(merged_document, save_file_path, optimise: true) end |
#open_json(path) ⇒ Object
21 22 23 |
# File 'lib/ocr-file/file_helpers.rb', line 21 def open_json(path) JSON.parse(File.read(path)) end |
#open_text_file(path) ⇒ Object
29 30 31 |
# File 'lib/ocr-file/file_helpers.rb', line 29 def open_text_file(path) File.read(path) end |