lib/cloudfiles/container.rb in cloudfiles-1.4.16 vs lib/cloudfiles/container.rb in cloudfiles-1.4.17
- old
+ new
@@ -26,13 +26,13 @@
@cdnmgmtport = self.connection.cdnmgmtport
@cdnmgmtscheme = self.connection.cdnmgmtscheme
end
# Load the metadata now, so we'll get a CloudFiles::Exception::NoSuchContainer exception should the container
# not exist.
- self.metadata
+ self.container_metadata
end
-
+
# Refreshes data about the container and populates class variables. Items are otherwise
# loaded in a lazy loaded fashion.
#
# container.count
# => 2
@@ -47,15 +47,17 @@
true
end
alias :populate :refresh
# Retrieves Metadata for the container
- def metadata
+ def container_metadata
@metadata ||= (
response = self.connection.cfreq("HEAD", @storagehost, @storagepath + "/", @storageport, @storagescheme)
raise CloudFiles::Exception::NoSuchContainer, "Container #{@name} does not exist" unless (response.code =~ /^20/)
- {:bytes => response["x-container-bytes-used"].to_i, :count => response["x-container-object-count"].to_i}
+ resphash = {}
+ response.to_hash.select { |k,v| k.match(/^x-container-meta/) }.each { |x| resphash[x[0]] = x[1].to_s }
+ {:bytes => response["x-container-bytes-used"].to_i, :count => response["x-container-object-count"].to_i, :metadata => resphash}
)
end
# Retrieves CDN-Enabled Meta Data
def cdn_metadata
@@ -76,11 +78,36 @@
)
else
@cdn_metadata = {}
end
end
+
+ # Returns the container's metadata as a nicely formatted hash, stripping off the X-Meta-Object- prefix that the system prepends to the
+ # key name.
+ #
+ # object.metadata
+ # => {"ruby"=>"cool", "foo"=>"bar"}
+ def metadata
+ metahash = {}
+ self.container_metadata[:metadata].each{ |key, value| metahash[key.gsub(/x-container-meta-/, '').gsub(/%20/, ' ')] = URI.decode(value).gsub(/\+\-/, ' ') }
+ metahash
+ end
+ # Sets the metadata for an object. By passing a hash as an argument, you can set the metadata for an object.
+ # New calls to set metadata are additive. To remove metadata, set the value of the key to nil.
+ #
+ # Throws NoSuchObjectException if the container doesn't exist. Throws InvalidResponseException if the request
+ # fails.
+ def set_metadata(metadatahash)
+ headers = {}
+ metadatahash.each{ |key, value| headers['X-Container-Meta-' + CloudFiles.escape(key.to_s.capitalize)] = value.to_s }
+ response = self.connection.cfreq("POST", @storagehost, @storagepath, @storageport, @storagescheme, headers)
+ raise CloudFiles::Exception::NoSuchObject, "Container #{@name} does not exist" if (response.code == "404")
+ raise CloudFiles::Exception::InvalidResponse, "Invalid response code #{response.code}" unless (response.code =~ /^20/)
+ true
+ end
+
# Size of the container (in bytes)
def bytes
self.metadata[:bytes]
end
@@ -198,10 +225,12 @@
# Retrieves a list of all objects in the current container along with their size in bytes, hash, and content_type.
# If no objects exist, an empty hash is returned. Throws an InvalidResponseException if the request fails. Takes a
# parameter hash as an argument, in the same form as the objects method.
#
+ # Accepts the same options as objects to limit the returned set.
+ #
# Returns a hash in the same format as the containers_detail from the CloudFiles class.
#
# container.objects_detail
# => {"test.txt"=>{:content_type=>"application/octet-stream",
# :hash=>"e2a6fcb4771aa3509f6b27b6a97da55b",
@@ -239,10 +268,10 @@
# => true
#
# full_container.empty?
# => false
def empty?
- return (metadata[:count].to_i == 0)? true : false
+ return (container_metadata[:count].to_i == 0)? true : false
end
# Returns true if object exists and returns false otherwise.
#
# container.object_exists?('goodfile.txt')