class PMP::CollectionDocument
Using OpenStruct for now - perhaps use ActiveModel? hmm…
Attributes
private var to save attributes obj, to handle object attributes
the href/url string to retrieve info for this resource
this is a tricky, read-only list of items, same as if loaded from link->item links do not put into json form of this doc
private var to save links obj, to handle link additions
has this resource actually been loaded from remote url or json document?
keep a ref to original doc from which this obj was created should this be private?
keep a ref to response obj if this resulted from one should this be private?
all collection docs have a version default is '1.0'
Public Class Methods
document is the original json derived doc used to create this resource assumption is that doc is a parsed json doc confirming to collection.doc+json TODO: check if this is a json string or hash, for now assume it has been mashified
# File lib/pmp/collection_document.rb, line 44 def initialize(options={}, &block) @mutex = Mutex.new apply_configuration(options) self.root = current_options.delete(:root) self.href = current_options.delete(:href) self.version = current_options.delete(:version) || '1.0' self.attributes = OpenStruct.new # if there is a doc to be had, pull it out self.response = current_options.delete(:response) self.original = current_options.delete(:document) yield(self) if block_given? end
static method to filter out static parts of c.doc+j hash before PUT/POST to PMP
server in the future this should be fixed in PMP
API to no longer be necessary
# File lib/pmp/collection_document.rb, line 194 def self.to_persist_json(body) return body.to_s if body.is_a?(String) || !body.respond_to?(:as_json) result = body.as_json.select { |k,v| %w(version attributes links).include?(k) } result['attributes'].reject! { |k,v| %w(created modified).include?(k) } result['links'].reject! { |k,v| %w(creator query edit auth).include?(k) } result.to_json end
Public Instance Methods
# File lib/pmp/collection_document.rb, line 204 def attributes_map HashWithIndifferentAccess.new(attributes.marshal_dump) end
# File lib/pmp/collection_document.rb, line 72 def attributes_object @attributes || OpenStruct.new end
# File lib/pmp/collection_document.rb, line 117 def delete raise 'No guid specified to delete' if self.guid.blank? url = delete_link.where(guid: self.guid).url request(:delete, url) end
# File lib/pmp/collection_document.rb, line 130 def delete_link link = root.edit['urn:collectiondoc:form:documentdelete'] raise 'Delete link not found' unless link link end
# File lib/pmp/collection_document.rb, line 91 def get @mutex.synchronize { return self if loaded? || !href self.response = request(:get, href) self.loaded = true } self end
# File lib/pmp/collection_document.rb, line 105 def links_object @links ||= PMP::Links.new(self) end
# File lib/pmp/collection_document.rb, line 149 def loaded? !!self.loaded end
# File lib/pmp/collection_document.rb, line 212 def method_missing(method, *args) get if (method.to_s.last != '=') respond_to?(method) ? send(method, *args) : attributes_object.send(method, *args) end
# File lib/pmp/collection_document.rb, line 144 def new_root root_options = current_options.merge(href: endpoint) PMP::CollectionDocument.new(root_options).tap{|r| r.root = r } end
# File lib/pmp/collection_document.rb, line 83 def original=(doc) unless (!doc || loaded?) @original = doc parse(@original) self.loaded = true end end
# File lib/pmp/collection_document.rb, line 76 def response=(resp) unless (!resp || loaded?) @response = resp self.original = resp.body end end
# File lib/pmp/collection_document.rb, line 140 def root @root ||= new_root end
# File lib/pmp/collection_document.rb, line 136 def root=(r) @root = r end
# File lib/pmp/collection_document.rb, line 109 def save set_guid_if_blank url = save_link.where(guid: self.guid).url resp = request(:put, url, self) self.response = resp self.href = resp.body['url'] end
# File lib/pmp/collection_document.rb, line 124 def save_link link = root.edit['urn:collectiondoc:form:documentsave'] raise 'Save link not found' unless link link end
# File lib/pmp/collection_document.rb, line 208 def set_guid_if_blank self.guid = SecureRandom.uuid if guid.blank? end
# File lib/pmp/collection_document.rb, line 153 def setup_oauth_token if !oauth_token && current_options[:client_id] && current_options[:client_secret] token = PMP::Token.new(current_options.merge(root: root)).get_token self.oauth_token = token.token self.token_type = token.params['token_type'] if @root @root.oauth_token = token.token @root.token_type = token.params['token_type'] end end end