Class: PMP::CollectionDocument

Inherits:
OpenStruct
  • Object
show all
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

Instance Method Summary collapse

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

Yields:

  • (_self)

Yield Parameters:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pmp/collection_document.rb', line 40

def initialize(options={}, &block)
  super()
  apply_configuration(options)

  self.root     = current_options.delete(:root)
  self.href     = current_options.delete(:href)
  self.version  = current_options.delete(:version) || '1.0'

  self.links    = PMP::Links.new(self)

  # 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/pmp/collection_document.rb', line 160

def method_missing(method, *args)
  if (method.to_s.last != '=') && !loaded?
    load
    return self.send(method, *args) if self.respond_to?(method)
  end

  super
end

Instance Attribute Details

#hrefObject

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

#itemsObject

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



35
36
37
# File 'lib/pmp/collection_document.rb', line 35

def items
  @items
end

private var to save links obj, to handle link additions



31
32
33
# File 'lib/pmp/collection_document.rb', line 31

def links
  @links
end

#loadedObject

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

#originalObject

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

#responseObject

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

#versionObject

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

Instance Method Details

#attributesObject



62
63
64
# File 'lib/pmp/collection_document.rb', line 62

def attributes
  HashWithIndifferentAccess.new(marshal_dump.delete_if{|k,v| links.keys.include?(k.to_s)})
end

#deleteObject



96
97
98
99
100
101
# File 'lib/pmp/collection_document.rb', line 96

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


103
104
105
106
107
# File 'lib/pmp/collection_document.rb', line 103

def edit_link(method)
  link = root_document.edit['urn:pmp:form:documentsave']
  raise "Edit link does not specify saving via #{method}" unless (link && link.hints.allow.include?(method))
  link
end

#loadObject Also known as: get



81
82
83
84
85
86
# File 'lib/pmp/collection_document.rb', line 81

def load
  if !loaded? && href
    self.response = request(:get, href)
    self.loaded = true
  end
end

#loaded?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/pmp/collection_document.rb', line 121

def loaded?
  !!self.loaded
end

#request(method, url, body = nil) ⇒ Object

url includes any params - full url



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/pmp/collection_document.rb', line 134

def request(method, url, body=nil) # :nodoc:

  setup_oauth_token

  begin
    raw = connection(current_options.merge({url: url})).send(method) do |request|
      if [:post, :put].include?(method.to_sym) && !body.blank?
        request.body = body.is_a?(String) ? body : body.to_json
      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

#rootObject



109
110
111
# File 'lib/pmp/collection_document.rb', line 109

def root
  @root
end

#root=(r) ⇒ Object



113
114
115
# File 'lib/pmp/collection_document.rb', line 113

def root=(r)
  @root
end

#root_documentObject



117
118
119
# File 'lib/pmp/collection_document.rb', line 117

def root_document
  @root ||= PMP::CollectionDocument.new(current_options.merge(href: endpoint))
end

#saveObject



89
90
91
92
93
94
# File 'lib/pmp/collection_document.rb', line 89

def save
  set_guid_if_blank
  url = edit_link('PUT').where(guid: self.guid).url
  response = request(:put, url, self)
  self.href = response.body['url']
end

#set_guid_if_blankObject



156
157
158
# File 'lib/pmp/collection_document.rb', line 156

def set_guid_if_blank
  self.guid = SecureRandom.uuid if guid.blank?
end

#setup_oauth_tokenObject



125
126
127
128
129
130
131
# File 'lib/pmp/collection_document.rb', line 125

def setup_oauth_token
  if !oauth_token
    token = PMP::Token.new(current_options).get_token
    self.oauth_token = token.token
    self.token_type  = token.params['token_type']
  end
end