class Duracloud::Space
A “space” within a DuraCloud account.
Constants
- MAX_RESULTS
Max size of content item list for one request.
This limit is imposed by Duracloud.
Public Class Methods
List all spaces @param store_id [String] the store ID (optional) @return [Array<Duracloud::Space>] the list of spaces @raise [Duracloud::Error] the store was not found
# File lib/duracloud/space.rb, line 24 def all(store_id = nil) ids(store_id).map { |id| new(id, store_id) } end
Return the audit log for the space @return [Duracloud::AuditLog] the audit log @raise [Duracloud::NotFoundError] the space or store was not found
# File lib/duracloud/space.rb, line 104 def audit_log(*args) find(*args).audit_log end
Return the bit integrity report for the space @return [Duracloud::BitIntegrityReport] the report @raise [Duracloud::NotFoundError] the space or store was not found
# File lib/duracloud/space.rb, line 111 def bit_integrity_report(*args) find(*args).bit_integrity_report end
Enumerates the content IDs in the space. @param space_id [String] the space ID @param store_id [String] the store ID (optional) @param prefix [String] the content ID prefix for filtering (optional) @param start_after [String] the content ID to be used as a “marker”.
Listing starts after this ID. (optional)
@return [Enumerator] an enumerator. @raise [Duracloud::NotFoundError] the space or store does not exist.
# File lib/duracloud/space.rb, line 46 def content_ids(space_id, store_id: nil, prefix: nil, start_after: nil) space = find(space_id, store_id) space.content_ids(prefix: prefix, start_after: start_after) end
Return the number of items in the space @return [Fixnum] the number of items @raise [Duracloud::NotFoundError] the space or store was not found
# File lib/duracloud/space.rb, line 97 def count(*args) find(*args).count end
Create a new space @see .new for arguments @return [Duracloud::Space] the space @raise [Duracloud::BadRequestError] the space ID is invalid.
# File lib/duracloud/space.rb, line 68 def create(*args) new(*args) do |space| yield space if block_given? space.save end end
Does the space exist? @see .new for arguments @return [Boolean] whether the space exists.
# File lib/duracloud/space.rb, line 78 def exist?(*args) find(*args) && true rescue NotFoundError false end
Find a space @see .new for arguments @return [Duracloud::Space] the space @raise [Duracloud::NotFoundError] the space or store was not found
# File lib/duracloud/space.rb, line 88 def find(*args) new(*args) do |space| space.load_properties end end
List all space IDs @param store_id [String] the store ID (optional) @return [Array<String>] the list of space IDs @raise [Duracloud::Error] the store was not found
# File lib/duracloud/space.rb, line 32 def ids(store_id = nil) response = Client.get_spaces(storeID: store_id) doc = Nokogiri::XML(response.body) doc.css('space').map { |s| s['id'] } end
Enumerates the content items in the space. @param space_id [String] the space ID @param store_id [String] the store ID (optional) @param prefix [String] the content ID prefix for filtering (optional) @param start_after [String] the content ID to be used as a “marker”.
Listing starts after this ID. (optional)
@return [Enumerator] an enumerator. @raise [Duracloud::NotFoundError] the space does not exist in Duracloud
.
# File lib/duracloud/space.rb, line 59 def items(space_id, store_id: nil, prefix: nil, start_after: nil) space = find(space_id, store_id) space.items(prefix: prefix, start_after: start_after) end
Return the manifest for the space @return [Duracloud::Manifest] the manifest @raise [Duracloud::NotFoundError] the space or store was not found
# File lib/duracloud/space.rb, line 118 def manifest(*args) find(*args).manifest end
@param space_id [String] the space ID @param store_id [String] the store ID (optional)
# File lib/duracloud/space.rb, line 125 def initialize(space_id, store_id = nil) super(space_id: space_id, store_id: store_id) yield self if block_given? end
Public Instance Methods
Return the ACLs for the space @return [Duracloud::SpaceAcls] the ACLs
# File lib/duracloud/space.rb, line 182 def acls @acls ||= SpaceAcls.new(self) end
Return the audit log for the space @return [Duracloud::AuditLog] the audit log
# File lib/duracloud/space.rb, line 164 def audit_log AuditLog.new(space_id, store_id) end
Return the bit integrity report for the space @return [Duracloud::BitIntegrityReport] the report
# File lib/duracloud/space.rb, line 170 def bit_integrity_report BitIntegrityReport.new(space_id, store_id) end
Enumerates the content IDs in the space. @param prefix [String] the content ID prefix for filtering (optional) @param start_after [String] the content ID to be used as a “marker”.
Listing starts after this ID. (optional)
@return [Enumerator] an enumerator. @raise [Duracloud::NotFoundError] the space does not exist in Duracloud
.
# File lib/duracloud/space.rb, line 192 def content_ids(prefix: nil, start_after: nil) Enumerator.new do |yielder| num = 0 marker = start_after while num < count || count == 1000 q = query.merge(prefix: prefix, maxResults: MAX_RESULTS, marker: marker) response = Client.get_space(space_id, **q) xml = Nokogiri::XML(response.body) ids = xml.css('item').map(&:text) break if ids.empty? ids.each do |content_id| yielder << content_id end num += ids.length marker = ids.last end end end
Return the number of items in the space @return [Fixnum] the number of items @note If the count is over 1000, DuraCloud sets the
x-dura-meta-space-count header to "1000+". This method will in that case return 1000, indicating that the exact count must be retrieved by other means.
# File lib/duracloud/space.rb, line 145 def count properties["x-dura-meta-space-count"].to_i end
Return the creation date of the space, if persisted, or nil. @return [DateTime] the date
# File lib/duracloud/space.rb, line 151 def created DateTime.parse(properties["x-dura-meta-space-created"]) rescue nil end
Find a content item in the space @return [Duracloud::Content] the content item. @raise [Duracloud::NotFoundError] if the content item does not exist.
# File lib/duracloud/space.rb, line 158 def find_content(content_id) Content.find(space_id: space_id, content_id: content_id, store_id: store_id) end
# File lib/duracloud/space.rb, line 130 def inspect "#<#{self.class} space_id=#{space_id.inspect}," \ " store_id=#{(store_id || '(default)').inspect}>" end
Enumerates the content items in the space. @see content_ids
@return [Enumerator] an enumerator. @raise [Duracloud::NotFoundError] the space does not exist in Duracloud
.
# File lib/duracloud/space.rb, line 215 def items(*args) Enumerator.new do |yielder| content_ids(*args).each do |content_id| yielder << find_content(content_id) end end end
Return the manifest for the space @return [Duracloud::Manifest] the manifest
# File lib/duracloud/space.rb, line 176 def manifest Manifest.new(space_id, store_id) end
# File lib/duracloud/space.rb, line 135 def to_s space_id end
Private Instance Methods
# File lib/duracloud/space.rb, line 234 def create Client.create_space(id, **query) update unless acls.empty? end
# File lib/duracloud/space.rb, line 248 def do_delete reset_acls Client.delete_space(id, **query) end
# File lib/duracloud/space.rb, line 225 def do_load_properties response = Client.get_space_properties(id, **query) self.properties = response.headers end
# File lib/duracloud/space.rb, line 253 def do_save persisted? ? update : create reset_acls end
# File lib/duracloud/space.rb, line 244 def properties_class SpaceProperties end
# File lib/duracloud/space.rb, line 258 def query { storeID: store_id } end
# File lib/duracloud/space.rb, line 230 def reset_acls @acls = nil end
# File lib/duracloud/space.rb, line 239 def update options = { headers: acls.to_h, query: query } Client.set_space_acls(id, **options) end