Class: UberS3::Bucket::ObjectList
- Inherits:
-
Object
- Object
- UberS3::Bucket::ObjectList
- Includes:
- Enumerable
- Defined in:
- lib/uber-s3/bucket.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#key ⇒ Object
Returns the value of attribute key.
-
#objects ⇒ Object
Returns the value of attribute objects.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #fetch(marker = nil) ⇒ Object
-
#initialize(bucket, key, options = {}) ⇒ ObjectList
constructor
A new instance of ObjectList.
- #parse_contents(xml) ⇒ Object
- #to_a ⇒ Object
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, ={}) self.bucket = bucket self.key = key.gsub(/^\//, '') self. = self.objects = [] end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
48 49 50 |
# File 'lib/uber-s3/bucket.rb', line 48 def bucket @bucket end |
#key ⇒ Object
Returns the value of attribute key.
48 49 50 |
# File 'lib/uber-s3/bucket.rb', line 48 def key @key end |
#objects ⇒ Object
Returns the value of attribute objects.
48 49 50 |
# File 'lib/uber-s3/bucket.rb', line 48 def objects @objects end |
#options ⇒ Object
Returns the value of attribute options.
48 49 50 |
# File 'lib/uber-s3/bucket.rb', line 48 def @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_a ⇒ Object
94 95 96 |
# File 'lib/uber-s3/bucket.rb', line 94 def to_a fetch end |