Class: Support::CaptureStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/support/capture_storage.rb

Defined Under Namespace

Classes: StorageError

Constant Summary collapse

CAPTURE_DIR =

Constants for directory and filename constraints

'captures'.freeze
MAX_FILENAME_LENGTH =
100
VALID_FILENAME_REGEX =
/\A[a-zA-Z0-9\-_\.]+\z/

Class Method Summary collapse

Class Method Details

.flush(directory) ⇒ Object

Deletes all files in a specified directory under the capture directory.

Parameters:

  • directory (String)

    Subdirectory name under the capture directory.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/support/capture_storage.rb', line 56

def flush(directory)
  begin
    dir_path = File.join(CAPTURE_DIR, directory) # Full path to the directory

    unless Dir.exist?(dir_path)
      raise StandardError, "Directory '#{dir_path}' does not exist."
    end

    files = Dir.glob("#{dir_path}/*") # Get all files in the directory
    
    if files.empty?
      puts "No files found in #{dir_path} to delete."
      return
    end

    files.each do |file|
      if File.file?(file)
        begin
          File.delete(file) # Delete each file
        rescue Errno::EACCES
          puts "Permission denied when deleting file: #{file}"
        rescue StandardError => e
          puts "Failed to delete file: #{file}. Error: #{e.message}"
        end
      end
    end

    puts "Flushed all files in #{dir_path} successfully."

  rescue StandardError => e
    puts "An error occurred during flush operation: #{e.message}"
  end
end

.load_capture(capture_file) ⇒ Hash

Loads capture data from a JSON file.

Parameters:

  • capture_file (String)

    Path to the capture file.

Returns:

  • (Hash)

    Parsed JSON data from the file.

Raises:

  • (StorageError)

    If the file cannot be loaded or parsed.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/support/capture_storage.rb', line 41

def load_capture(capture_file)
  validate_file_path(capture_file) # Ensure the file path is valid
  
  file_content = read_file_safely(capture_file) # Read the file content
  parsed_data = JSON.parse(file_content) # Parse the JSON content
  
  validate_parsed_data(parsed_data) # Ensure the parsed data is valid
  
  parsed_data
rescue => e
  raise StorageError, "Failed to load capture: #{e.message}"
end

.save_capture(record, capture_data, name = nil) ⇒ Hash

Saves capture data to a JSON file in a structured directory.

Parameters:

  • record (Object)

    The record object (must respond to :class and :id).

  • capture_data (Hash)

    The data to be saved.

  • name (String, nil) (defaults to: nil)

    Optional custom filename.

Returns:

  • (Hash)

    Result of the operation with success status and file path or error message.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/support/capture_storage.rb', line 22

def save_capture(record, capture_data, name = nil)
  validate_record(record) # Ensure the record is valid
  validate_capture_data(capture_data) # Ensure the capture data is valid
  
  file_name = generate_filename(record, name) # Generate a valid filename
  dir_path = create_capture_directory(record) # Create the directory for the capture
  file_path = File.join(dir_path, file_name) # Full path to the capture file

  write_capture_file(file_path, capture_data) # Write the capture data to the file
  
  { success: true, file_path: file_path }
rescue => e
  { success: false, error: e.message }
end