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
- #attributes_object ⇒ Object
- #delete ⇒ Object
- #delete_link ⇒ Object
- #get ⇒ 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.
- #links_object ⇒ Object
- #loaded? ⇒ Boolean
- #method_missing(method, *args) ⇒ Object
- #new_root ⇒ Object
-
#request(method, url, body = nil) ⇒ Object
url includes any params - full url.
- #root ⇒ Object
- #root=(r) ⇒ Object
- #save ⇒ Object
- #save_link ⇒ Object
- #set_guid_if_blank ⇒ Object
- #setup_oauth_token ⇒ Object
Methods included from Parser
#as_json, #extract_attributes, #extract_links, #parse, #parse_attributes, #parse_href, #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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pmp/collection_document.rb', line 44 def initialize(={}, &block) @mutex = Mutex.new apply_configuration() self.root = .delete(:root) self.href = .delete(:href) self.version = .delete(:version) || '1.0' self.attributes = OpenStruct.new # 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
212 213 214 215 |
# 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 |
Instance Attribute Details
#attributes ⇒ Object
private var to save attributes obj, to handle object attributes
32 33 34 |
# File 'lib/pmp/collection_document.rb', line 32 def attributes @attributes end |
#href ⇒ Object
the href/url string to retrieve info for this resource
14 15 16 |
# File 'lib/pmp/collection_document.rb', line 14 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
39 40 41 |
# File 'lib/pmp/collection_document.rb', line 39 def items @items end |
#links ⇒ Object
private var to save links obj, to handle link additions
35 36 37 |
# File 'lib/pmp/collection_document.rb', line 35 def links @links end |
#loaded ⇒ Object
has this resource actually been loaded from remote url or json document?
29 30 31 |
# File 'lib/pmp/collection_document.rb', line 29 def loaded @loaded end |
#original ⇒ Object
keep a ref to original doc from which this obj was created should this be private?
22 23 24 |
# File 'lib/pmp/collection_document.rb', line 22 def original @original end |
#response ⇒ Object
keep a ref to response obj if this resulted from one should this be private?
18 19 20 |
# File 'lib/pmp/collection_document.rb', line 18 def response @response end |
#version ⇒ Object
all collection docs have a version default is ‘1.0’
26 27 28 |
# File 'lib/pmp/collection_document.rb', line 26 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
194 195 196 197 198 199 200 201 202 |
# 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 |
Instance Method Details
#attributes_map ⇒ Object
204 205 206 |
# File 'lib/pmp/collection_document.rb', line 204 def attributes_map HashWithIndifferentAccess.new(attributes.marshal_dump) end |
#attributes_object ⇒ Object
72 73 74 |
# File 'lib/pmp/collection_document.rb', line 72 def attributes_object @attributes || OpenStruct.new end |
#delete ⇒ Object
117 118 119 120 121 122 |
# 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 |
#delete_link ⇒ Object
130 131 132 133 134 |
# 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 |
#get ⇒ Object
91 92 93 94 95 96 97 98 |
# 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 |
#links_object ⇒ Object
105 106 107 |
# File 'lib/pmp/collection_document.rb', line 105 def links_object @links ||= PMP::Links.new(self) end |
#loaded? ⇒ Boolean
149 150 151 |
# File 'lib/pmp/collection_document.rb', line 149 def loaded? !!self.loaded end |
#new_root ⇒ Object
144 145 146 147 |
# File 'lib/pmp/collection_document.rb', line 144 def new_root = .merge(href: endpoint) PMP::CollectionDocument.new().tap{|r| r.root = r } end |
#request(method, url, body = nil) ⇒ Object
url includes any params - full url
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/pmp/collection_document.rb', line 168 def request(method, url, body=nil) # :nodoc: unless ['/', ''].include?(URI::parse(url).path) setup_oauth_token end begin raw = connection(.merge({url: url})).send(method) do |req| if [:post, :put].include?(method.to_sym) && !body.blank? req.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
140 141 142 |
# File 'lib/pmp/collection_document.rb', line 140 def root @root ||= new_root end |
#root=(r) ⇒ Object
136 137 138 |
# File 'lib/pmp/collection_document.rb', line 136 def root=(r) @root = r end |
#save ⇒ Object
109 110 111 112 113 114 115 |
# 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 |
#save_link ⇒ Object
124 125 126 127 128 |
# 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 |
#set_guid_if_blank ⇒ Object
208 209 210 |
# File 'lib/pmp/collection_document.rb', line 208 def set_guid_if_blank self.guid = SecureRandom.uuid if guid.blank? end |
#setup_oauth_token ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/pmp/collection_document.rb', line 153 def setup_oauth_token if !oauth_token && [:client_id] && [:client_secret] token = PMP::Token.new(.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 |