class PMP::CollectionDocument

Using OpenStruct for now - perhaps use ActiveModel? hmm…

Attributes

attributes[RW]

private var to save attributes obj, to handle object attributes

href[RW]

the href/url string to retrieve info for this resource

items[RW]

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

loaded[RW]

has this resource actually been loaded from remote url or json document?

original[RW]

keep a ref to original doc from which this obj was created should this be private?

response[RW]

keep a ref to response obj if this resulted from one should this be private?

version[RW]

all collection docs have a version default is '1.0'

Public Class Methods

new(options={}) { |self| ... } click to toggle source

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
to_persist_json(body) click to toggle source

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

attributes_map() click to toggle source
# File lib/pmp/collection_document.rb, line 204
def attributes_map
  HashWithIndifferentAccess.new(attributes.marshal_dump)
end
attributes_object() click to toggle source
# File lib/pmp/collection_document.rb, line 72
def attributes_object
  @attributes || OpenStruct.new
end
delete() click to toggle source
# 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
get() click to toggle source
# 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
loaded?() click to toggle source
# File lib/pmp/collection_document.rb, line 149
def loaded?
  !!self.loaded
end
method_missing(method, *args) click to toggle source
# 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
new_root() click to toggle source
# 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
original=(doc) click to toggle source
# File lib/pmp/collection_document.rb, line 83
def original=(doc)
  unless (!doc || loaded?)
    @original = doc
    parse(@original)
    self.loaded = true
  end
end
response=(resp) click to toggle source
# File lib/pmp/collection_document.rb, line 76
def response=(resp)
  unless (!resp || loaded?)
    @response = resp
    self.original = resp.body
  end
end
root() click to toggle source
# File lib/pmp/collection_document.rb, line 140
def root
  @root ||= new_root
end
root=(r) click to toggle source
# File lib/pmp/collection_document.rb, line 136
def root=(r)
  @root = r
end
save() click to toggle source
# 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
set_guid_if_blank() click to toggle source
# File lib/pmp/collection_document.rb, line 208
def set_guid_if_blank
  self.guid = SecureRandom.uuid if guid.blank?
end
setup_oauth_token() click to toggle source
# 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