Class: ToiletTracker::Utils::ZipHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/toilet_tracker/utils/zip_handler.rb

Defined Under Namespace

Classes: ChatFileNotFoundError, ZipError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_path) ⇒ ZipHandler

Returns a new instance of ZipHandler.



21
22
23
24
# File 'lib/toilet_tracker/utils/zip_handler.rb', line 21

def initialize(zip_path)
  @zip_path = zip_path
  validate_zip_file!
end

Class Method Details

.extract_chat_file(zip_path) ⇒ Object



13
14
15
# File 'lib/toilet_tracker/utils/zip_handler.rb', line 13

def self.extract_chat_file(zip_path)
  new(zip_path).extract_chat_file
end

.zip_file?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/toilet_tracker/utils/zip_handler.rb', line 17

def self.zip_file?(file_path)
  File.extname(file_path).downcase == ".zip"
end

Instance Method Details

#extract_chat_fileObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/toilet_tracker/utils/zip_handler.rb', line 26

def extract_chat_file
  chat_entries = find_chat_entries

  raise ChatFileNotFoundError, "No _chat.txt file found in zip archive" if chat_entries.empty?

  chat_entry = if chat_entries.size > 1
                 # If multiple _chat.txt files, prefer the one in root or with shortest path
                 chat_entries.min_by { |entry| entry.name.count("/") }
  else
                 chat_entries.first
  end

  extract_to_tempfile(chat_entry)
end