Class: PMP::CollectionDocument
- Inherits:
-
Object
- Object
- PMP::CollectionDocument
- Includes:
- Configuration, Connection, Parser
- Defined in:
- lib/pmp/collection_document.rb
Overview
Using OpenStruct for now - perhaps use ActiveModel? hmm…
Constant Summary
Constants included from Connection
PMP::Connection::ALLOWED_CONNECTION_OPTIONS
Constants included from Configuration
PMP::Configuration::DEFAULT_ADAPTER, PMP::Configuration::DEFAULT_CLIENT_ID, PMP::Configuration::DEFAULT_CLIENT_SECRET, PMP::Configuration::DEFAULT_ENDPOINT, PMP::Configuration::DEFAULT_TOKEN_TYPE, PMP::Configuration::DEFAULT_USER_AGENT, PMP::Configuration::VALID_OPTIONS_KEYS
Instance Attribute Summary collapse
-
#attributes ⇒ Object
private var to save attributes obj, to handle object attributes.
-
#href ⇒ Object
the href/url string to retrieve info for this resource.
-
#items ⇒ Object
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.
-
#links ⇒ Object
private var to save links obj, to handle link additions.
-
#loaded ⇒ Object
has this resource actually been loaded from remote url or json document?.
-
#original ⇒ Object
keep a ref to original doc from which this obj was created should this be private?.
-
#response ⇒ Object
keep a ref to response obj if this resulted from one should this be private?.
-
#version ⇒ Object
all collection docs have a version default is ‘1.0’.
Class Method Summary collapse
-
.to_persist_json(body) ⇒ Object
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.
Instance Method Summary collapse
- #attributes_map ⇒ Object
- #delete ⇒ Object
- #edit_link(method) ⇒ Object
-
#initialize(options = {}) {|_self| ... } ⇒ CollectionDocument
constructor
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.
- #load ⇒ Object (also: #get)
- #loaded? ⇒ Boolean
- #method_missing(method, *args) ⇒ Object
-
#request(method, url, body = nil) ⇒ Object
url includes any params - full url.
- #root ⇒ Object
- #root=(r) ⇒ Object
- #root_document ⇒ Object
- #save ⇒ Object
- #set_guid_if_blank ⇒ Object
- #setup_oauth_token ⇒ Object
Methods included from Parser
#as_json, #extract_attributes, #extract_links, #parse, #parse_attributes, #parse_items, #parse_link, #parse_links, #parse_links_list, #parse_version
Methods included from Utils
#to_json_key_name, #to_ruby_safe_name
Methods included from Connection
#add_request_auth, #connection, #process_options
Methods included from Configuration
#apply_configuration, #configure, extended, #options, #reset!
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ CollectionDocument
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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pmp/collection_document.rb', line 43 def initialize(={}, &block) apply_configuration() self.root = .delete(:root) self.href = .delete(:href) self.version = .delete(:version) || '1.0' self.attributes = OpenStruct.new self.links = PMP::Links.new(self) # if there is a doc to be had, pull it out self.response = .delete(:response) self.original = .delete(:document) yield(self) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
175 176 177 178 |
# File 'lib/pmp/collection_document.rb', line 175 def method_missing(method, *args) load if (method.to_s.last != '=') respond_to?(method) ? send(method, *args) : attributes.send(method, *args) end |
Instance Attribute Details
#attributes ⇒ Object
private var to save attributes obj, to handle object attributes
31 32 33 |
# File 'lib/pmp/collection_document.rb', line 31 def attributes @attributes end |
#href ⇒ Object
the href/url string to retrieve info for this resource
13 14 15 |
# File 'lib/pmp/collection_document.rb', line 13 def href @href end |
#items ⇒ Object
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
38 39 40 |
# File 'lib/pmp/collection_document.rb', line 38 def items @items end |
#links ⇒ Object
private var to save links obj, to handle link additions
34 35 36 |
# File 'lib/pmp/collection_document.rb', line 34 def links @links end |
#loaded ⇒ Object
has this resource actually been loaded from remote url or json document?
28 29 30 |
# File 'lib/pmp/collection_document.rb', line 28 def loaded @loaded end |
#original ⇒ Object
keep a ref to original doc from which this obj was created should this be private?
21 22 23 |
# File 'lib/pmp/collection_document.rb', line 21 def original @original end |
#response ⇒ Object
keep a ref to response obj if this resulted from one should this be private?
17 18 19 |
# File 'lib/pmp/collection_document.rb', line 17 def response @response end |
#version ⇒ Object
all collection docs have a version default is ‘1.0’
25 26 27 |
# File 'lib/pmp/collection_document.rb', line 25 def version @version end |
Class Method Details
.to_persist_json(body) ⇒ Object
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
157 158 159 160 161 162 163 164 165 |
# File 'lib/pmp/collection_document.rb', line 157 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(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 |
Instance Method Details
#attributes_map ⇒ Object
167 168 169 |
# File 'lib/pmp/collection_document.rb', line 167 def attributes_map HashWithIndifferentAccess.new(attributes.marshal_dump) end |
#delete ⇒ Object
95 96 97 98 99 100 |
# File 'lib/pmp/collection_document.rb', line 95 def delete raise 'No guid specified to delete' if self.guid.blank? url = edit_link('DELETE').where(guid: self.guid).url request(:delete, url) end |
#edit_link(method) ⇒ Object
102 103 104 105 106 |
# File 'lib/pmp/collection_document.rb', line 102 def edit_link(method) link = root_document.edit['urn:collectiondoc:form:documentsave'] raise "Edit link does not specify saving via #{method}" unless (link && link.hints.allow.include?(method)) link end |
#load ⇒ Object Also known as: get
80 81 82 83 84 85 |
# File 'lib/pmp/collection_document.rb', line 80 def load if !loaded? && href self.response = request(:get, href) self.loaded = true end end |
#loaded? ⇒ Boolean
120 121 122 |
# File 'lib/pmp/collection_document.rb', line 120 def loaded? !!self.loaded end |
#request(method, url, body = nil) ⇒ Object
url includes any params - full url
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/pmp/collection_document.rb', line 133 def request(method, url, body=nil) # :nodoc: setup_oauth_token begin raw = connection(.merge({url: url})).send(method) do |request| if [:post, :put].include?(method.to_sym) && !body.blank? request.body = PMP::CollectionDocument.to_persist_json(body) end end rescue Faraday::Error::ResourceNotFound=>not_found_ex if (method.to_sym == :get) raw = OpenStruct.new(body: nil, status: 404) else raise not_found_ex end end # may not need this, but remember how we made this response PMP::Response.new(raw, {method: method, url: url, body: body}) end |
#root ⇒ Object
108 109 110 |
# File 'lib/pmp/collection_document.rb', line 108 def root @root end |
#root=(r) ⇒ Object
112 113 114 |
# File 'lib/pmp/collection_document.rb', line 112 def root=(r) @root end |
#root_document ⇒ Object
116 117 118 |
# File 'lib/pmp/collection_document.rb', line 116 def root_document @root ||= PMP::CollectionDocument.new(.merge(href: endpoint)) end |
#save ⇒ Object
88 89 90 91 92 93 |
# File 'lib/pmp/collection_document.rb', line 88 def save set_guid_if_blank url = edit_link('PUT').where(guid: self.guid).url self.response = request(:put, url, self) self.href = response.body['url'] end |
#set_guid_if_blank ⇒ Object
171 172 173 |
# File 'lib/pmp/collection_document.rb', line 171 def set_guid_if_blank self.guid = SecureRandom.uuid if guid.blank? end |