Class: Appydave::Tools::Dam::S3Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/dam/s3_scanner.rb

Overview

Scan S3 bucket for project files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(brand, brand_info: nil, s3_client: nil) ⇒ S3Scanner

Returns a new instance of S3Scanner.



12
13
14
15
16
# File 'lib/appydave/tools/dam/s3_scanner.rb', line 12

def initialize(brand, brand_info: nil, s3_client: nil)
  @brand_info = brand_info || load_brand_info(brand)
  @brand = @brand_info.key
  @s3_client = s3_client || create_s3_client(@brand_info)
end

Instance Attribute Details

#brandObject (readonly)

Returns the value of attribute brand.



10
11
12
# File 'lib/appydave/tools/dam/s3_scanner.rb', line 10

def brand
  @brand
end

#brand_infoObject (readonly)

Returns the value of attribute brand_info.



10
11
12
# File 'lib/appydave/tools/dam/s3_scanner.rb', line 10

def brand_info
  @brand_info
end

#s3_clientObject (readonly)

Returns the value of attribute s3_client.



10
11
12
# File 'lib/appydave/tools/dam/s3_scanner.rb', line 10

def s3_client
  @s3_client
end

Instance Method Details

#scan_all_projects(show_progress: true, &progress_callback) ⇒ Hash

Scan all projects in brand’s S3 bucket

Parameters:

  • show_progress (Boolean) (defaults to: true)

    Whether to show detailed progress (default: true)

Returns:

  • (Hash)

    Map of project_id => scan result



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/appydave/tools/dam/s3_scanner.rb', line 56

def scan_all_projects(show_progress: true, &progress_callback)
  bucket = @brand_info.aws.s3_bucket
  prefix = @brand_info.aws.s3_prefix

  # List all "directories" (prefixes) under brand prefix
  project_prefixes = list_s3_prefixes(bucket, prefix)

  return {} if project_prefixes.empty?

  results = {}
  project_prefixes.each_with_index do |project_id, index|
    # Call progress callback if provided (for spinner)
    progress_callback&.call(index + 1, project_prefixes.size)

    results[project_id] = scan_project(project_id, show_progress: show_progress)
  end

  results
end

#scan_project(project_id, show_progress: true) ⇒ Hash

Scan S3 for a specific project

Parameters:

  • project_id (String)

    Project ID (e.g., “b65-guy-monroe-marketing-plan”)

  • show_progress (Boolean) (defaults to: true)

    Whether to show progress output (default: true)

Returns:

  • (Hash)

    S3 file data with :file_count, :total_bytes, :last_modified



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/appydave/tools/dam/s3_scanner.rb', line 22

def scan_project(project_id, show_progress: true)
  bucket = @brand_info.aws.s3_bucket
  prefix = File.join(@brand_info.aws.s3_prefix, project_id, '')

  puts "  🔍 Scanning #{project_id}..." if show_progress

  files = list_s3_objects(bucket, prefix)

  if files.empty?
    return {
      exists: false,
      file_count: 0,
      total_bytes: 0,
      last_modified: nil
    }
  end

  total_bytes = files.sum(&:size)
  last_modified = files.map(&:last_modified).max

  {
    exists: true,
    file_count: files.size,
    total_bytes: total_bytes,
    last_modified: last_modified.utc.iso8601
  }
rescue Aws::S3::Errors::ServiceError => e
  puts "  ⚠️  S3 scan failed for #{project_id}: #{e.message}"
  { exists: false, file_count: 0, total_bytes: 0, last_modified: nil, error: e.message }
end