Class: Support::CaptureStorage
- Inherits:
-
Object
- Object
- Support::CaptureStorage
- 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
-
.flush(directory) ⇒ Object
Deletes all files in a specified directory under the capture directory.
-
.load_capture(capture_file) ⇒ Hash
Loads capture data from a JSON file.
-
.save_capture(record, capture_data, name = nil) ⇒ Hash
Saves capture data to a JSON file in a structured directory.
Class Method Details
.flush(directory) ⇒ Object
Deletes all files in a specified directory 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.}" end end end puts "Flushed all files in #{dir_path} successfully." rescue StandardError => e puts "An error occurred during flush operation: #{e.}" end end |
.load_capture(capture_file) ⇒ Hash
Loads capture data from a JSON file.
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.}" end |
.save_capture(record, capture_data, name = nil) ⇒ Hash
Saves capture data to a JSON file in a structured directory.
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. } end |