class S3Tree::Directory

Attributes

path[R]

Public Class Methods

new(bucket, path) click to toggle source
# File lib/s3_tree/directory.rb, line 6
def initialize(bucket, path)
  @bucket = bucket
  @path = path
end

Public Instance Methods

children() click to toggle source
# File lib/s3_tree/directory.rb, line 20
def children
  subdirectories + files
end
files() click to toggle source
# File lib/s3_tree/directory.rb, line 30
def files
  @files ||= list_objects['contents'].collect do |object|
    S3Tree::Files.new(@bucket, s3_object: object) unless object.key.ends_with?('/')
  end.compact
  (@files.sort_by &:last_modified).reverse!
end
last_modified() click to toggle source
# File lib/s3_tree/directory.rb, line 41
def last_modified
  (@bucket.objects(prefix: @path.gsub('+', ' ')).inject([]) { |date, s3_object| date.push(s3_object.last_modified) }).max
end
name() click to toggle source
# File lib/s3_tree/directory.rb, line 11
def name
  URI.decode(path_pieces.last).gsub('+', ' ')
end
parent() click to toggle source
# File lib/s3_tree/directory.rb, line 15
def parent
  parent_path = path_pieces[0..-2].join('/')
  S3Tree::Directory.new(@bucket, parent_path) unless parent_path.blank?
end
size() click to toggle source
# File lib/s3_tree/directory.rb, line 37
def size
  number_to_human_size(@bucket.objects(prefix: @path.gsub('+', ' ')).inject(0) { |sum, s3_object| sum + s3_object.content_length })
end
subdirectories() click to toggle source
# File lib/s3_tree/directory.rb, line 24
def subdirectories
  @subdirectories ||= list_objects['common_prefixes'].collect do |prefix|
    S3Tree::Directory.new(@bucket, prefix.prefix)
  end
end

Private Instance Methods

list_objects() click to toggle source
# File lib/s3_tree/directory.rb, line 50
def list_objects
  @list_objects ||= @bucket.client.list_objects({
                                                    prefix: @path.blank? ? '' : @path,
                                                    delimiter: '/',
                                                    bucket: @bucket.name,
                                                    encoding_type: 'url'
                                                })
end
path_pieces() click to toggle source
# File lib/s3_tree/directory.rb, line 46
def path_pieces
  @path_pieces ||= path ? path.split('/') : []
end