Class: UberS3::Bucket::ObjectList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/uber-s3/bucket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, key, options = {}) ⇒ ObjectList

Returns a new instance of ObjectList.



50
51
52
53
54
55
# File 'lib/uber-s3/bucket.rb', line 50

def initialize(bucket, key, options={})
  self.bucket   = bucket
  self.key      = key.gsub(/^\//, '')
  self.options  = options
  self.objects  = []
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



48
49
50
# File 'lib/uber-s3/bucket.rb', line 48

def bucket
  @bucket
end

#keyObject

Returns the value of attribute key.



48
49
50
# File 'lib/uber-s3/bucket.rb', line 48

def key
  @key
end

#objectsObject

Returns the value of attribute objects.



48
49
50
# File 'lib/uber-s3/bucket.rb', line 48

def objects
  @objects
end

#optionsObject

Returns the value of attribute options.



48
49
50
# File 'lib/uber-s3/bucket.rb', line 48

def options
  @options
end

Instance Method Details

#each(&block) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/uber-s3/bucket.rb', line 84

def each(&block)
  loop do
    marker = objects.last.key rescue nil
    fetch(marker)
    
    objects.each {|obj| block.call(obj) }
    break if @is_truncated == false
  end
end

#fetch(marker = nil) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/uber-s3/bucket.rb', line 57

def fetch(marker=nil)
  @objects = []

  default_max_keys = 500
  response = bucket.connection.get("/?prefix=#{CGI.escape(key)}&marker=#{marker}&max-keys=#{default_max_keys}")        

  @objects = parse_contents(response.body)
end

#parse_contents(xml) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/uber-s3/bucket.rb', line 66

def parse_contents(xml)
  objects = []
  doc = Util::XmlDocument.new(xml)
  
  # TODO: can use more error checking on the xml stuff
  
  @is_truncated = doc.xpath('//ListBucketResult/IsTruncated').first.text == "true"
  contents = doc.xpath('//ListBucketResult/Contents')
  
  contents.each do |content|
    h = {}
    content.elements.each {|el| h[el.name] = el.text }
    objects << ::UberS3::Object.new(bucket, h['Key'], nil, { :size => h['Size'].to_i })
  end if contents.any?

  objects
end

#to_aObject



94
95
96
# File 'lib/uber-s3/bucket.rb', line 94

def to_a
  fetch
end