Class: S3SizeCalc::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/s3sizecalc/calculator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Calculator

Returns a new instance of Calculator.



8
9
10
# File 'lib/s3sizecalc/calculator.rb', line 8

def initialize(options = {})
  @options = options
end

Instance Method Details

#account_totalObject



34
35
36
37
38
39
40
# File 'lib/s3sizecalc/calculator.rb', line 34

def 
  regions = ec2.client.describe_regions[:regions].map {|region| region[:region_name]}

  regions.inject 0 do |total, region|
    total += region_total(region)
  end
end

#bucket_total(bucket_name) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/s3sizecalc/calculator.rb', line 12

def bucket_total(bucket_name)
  puts "Calculating size of bucket `#{bucket_name}`." if @options.verbose

  s3.bucket(bucket_name).objects.inject 0 do |total, object|
    size = object.size
    puts "Size of object `#{object.key}` => #{size} bytes." if @options.very_verbose
    total += size
  end
end

#current_regionObject



42
43
44
# File 'lib/s3sizecalc/calculator.rb', line 42

def current_region
  @current_region || @options.region || own_region
end

#region_total(region = current_region) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/s3sizecalc/calculator.rb', line 22

def region_total(region = current_region)
  @current_region = region

  s3(region).buckets.inject 0 do |total, bucket|
    if s3.client.get_bucket_location(bucket: bucket.name).location_constraint == region
      total += bucket_total(bucket.name)
    else
      total
    end
  end
end