pmp 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d96e1bc799b9ca048a31b6bee2e54a1b4c3af2b
4
- data.tar.gz: 8702b4b792012a2ccc7aa7a093a179eaaedd8b1b
3
+ metadata.gz: 1859817bdd4a4e10673682a59cd88f8db50f025c
4
+ data.tar.gz: f63e8e75ee4a63bb22ed0647cae4fb2ad1fc5e44
5
5
  SHA512:
6
- metadata.gz: d9c4f583839c50699c93a04605d3f16ef43ae767de3baee9203cbae1c3377eaf235aa46e8e181efbfa426b99073f169a819fc05cbfaa5dc118615f78ec374dd2
7
- data.tar.gz: 3d1d6850858d0d3aebe96ae55e5c8e156680347190b400ddbb12837c099002c537623f2f8fd1d66147cb3402ea953fbd263d82cc020bf702e4d4dd52edf73371
6
+ metadata.gz: 4039a640c7f0144681d5d57002e2ede8b296a25aef9e98c04ab6382ebe8eaa79f84cdebc73e608de248863fc4ff1ef41fd7ce0333d10e347278c4398e2949652
7
+ data.tar.gz: b5240080294c1f6ca77bb05acf4b6352430ee0b4a74c475bad440f5986edf343d6871ef67ae34af7a1761d22d3568d8f51736ccb4cd0f1eefedeb830db6b2230
data/lib/pmp/client.rb CHANGED
@@ -22,8 +22,12 @@ module PMP
22
22
 
23
23
  def root(opts={})
24
24
  @root = nil if (opts != {})
25
- opts = options.merge(href: endpoint).merge(opts)
26
- @root ||= PMP::CollectionDocument.new(opts)
25
+ @root ||= new_root(opts)
26
+ end
27
+
28
+ def new_root(opts={})
29
+ root_options = options.merge(opts).merge(href: endpoint)
30
+ PMP::CollectionDocument.new(root_options).tap{|r| r.root = r }
27
31
  end
28
32
 
29
33
  def doc_of_type(type, opts={})
@@ -42,6 +42,11 @@ module PMP
42
42
  # assumption is that doc is a parsed json doc confirming to collection.doc+json
43
43
  # TODO: check if this is a json string or hash, for now assume it has been mashified
44
44
  def initialize(options={}, &block)
45
+ @mutex = Mutex.new
46
+
47
+ # puts "CollectionDocument.initialize options: #{options.inspect}\n"
48
+ # puts "\t#{RuntimeException.new.backtrace.join("\n\t")}"
49
+
45
50
  apply_configuration(options)
46
51
 
47
52
  self.root = current_options.delete(:root)
@@ -49,7 +54,6 @@ module PMP
49
54
  self.version = current_options.delete(:version) || '1.0'
50
55
 
51
56
  self.attributes = OpenStruct.new
52
- self.links = PMP::Links.new(self)
53
57
 
54
58
  # if there is a doc to be had, pull it out
55
59
  self.response = current_options.delete(:response)
@@ -63,6 +67,15 @@ module PMP
63
67
  @items ||= []
64
68
  end
65
69
 
70
+ def attributes
71
+ get
72
+ attributes_object
73
+ end
74
+
75
+ def attributes_object
76
+ @attributes || OpenStruct.new
77
+ end
78
+
66
79
  def response=(resp)
67
80
  unless (!resp || loaded?)
68
81
  @response = resp
@@ -79,13 +92,23 @@ module PMP
79
92
  end
80
93
 
81
94
  def get
82
- if !loaded? && href
95
+ return if loaded? || !href
96
+ @mutex.synchronize {
83
97
  self.response = request(:get, href)
84
98
  self.loaded = true
85
- end
99
+ }
86
100
  self
87
101
  end
88
102
 
103
+ def links
104
+ get
105
+ links_object
106
+ end
107
+
108
+ def links_object
109
+ @links ||= PMP::Links.new(self)
110
+ end
111
+
89
112
  def save
90
113
  set_guid_if_blank
91
114
  url = save_link.where(guid: self.guid).url
@@ -101,27 +124,28 @@ module PMP
101
124
  end
102
125
 
103
126
  def save_link
104
- link = root_document.edit['urn:collectiondoc:form:documentsave']
127
+ link = root.edit['urn:collectiondoc:form:documentsave']
105
128
  raise 'Save link not found' unless link
106
129
  link
107
130
  end
108
131
 
109
132
  def delete_link
110
- link = root_document.edit['urn:collectiondoc:form:documentdelete']
133
+ link = root.edit['urn:collectiondoc:form:documentdelete']
111
134
  raise 'Delete link not found' unless link
112
135
  link
113
136
  end
114
137
 
115
- def root
116
- @root
138
+ def root=(r)
139
+ @root = r
117
140
  end
118
141
 
119
- def root=(r)
120
- @root
142
+ def root
143
+ @root ||= new_root
121
144
  end
122
145
 
123
- def root_document
124
- @root ||= PMP::CollectionDocument.new(current_options.merge(href: endpoint))
146
+ def new_root
147
+ root_options = current_options.merge(href: endpoint)
148
+ PMP::CollectionDocument.new(root_options).tap{|r| r.root = r }
125
149
  end
126
150
 
127
151
  def loaded?
@@ -130,23 +154,31 @@ module PMP
130
154
 
131
155
  def setup_oauth_token
132
156
  if !oauth_token && current_options[:client_id] && current_options[:client_secret]
133
- token = PMP::Token.new(current_options).get_token
157
+ token = PMP::Token.new(current_options.merge(root: root)).get_token
134
158
  self.oauth_token = token.token
135
159
  self.token_type = token.params['token_type']
160
+
161
+ if @root
162
+ @root.oauth_token = token.token
163
+ @root.token_type = token.params['token_type']
164
+ end
165
+
136
166
  end
137
167
  end
138
168
 
139
169
  # url includes any params - full url
140
170
  def request(method, url, body=nil) # :nodoc:
141
171
 
172
+ # puts "CD::request: obj: #{self.object_id} #{method}, #{url}, #{body.inspect}"
173
+
142
174
  unless ['/', ''].include?(URI::parse(url).path)
143
175
  setup_oauth_token
144
176
  end
145
177
 
146
178
  begin
147
- raw = connection(current_options.merge({url: url})).send(method) do |request|
179
+ raw = connection(current_options.merge({url: url})).send(method) do |req|
148
180
  if [:post, :put].include?(method.to_sym) && !body.blank?
149
- request.body = PMP::CollectionDocument.to_persist_json(body)
181
+ req.body = PMP::CollectionDocument.to_persist_json(body)
150
182
  end
151
183
  end
152
184
  rescue Faraday::Error::ResourceNotFound => not_found_ex
@@ -183,7 +215,7 @@ module PMP
183
215
 
184
216
  def method_missing(method, *args)
185
217
  get if (method.to_s.last != '=')
186
- respond_to?(method) ? send(method, *args) : attributes.send(method, *args)
218
+ respond_to?(method) ? send(method, *args) : attributes_object.send(method, *args)
187
219
  end
188
220
 
189
221
  end
@@ -10,9 +10,13 @@ module PMP
10
10
 
11
11
  CREDENTIAL_PARAMS = [:scope, :token_expires_in, :label]
12
12
 
13
+ attr_accessor :root
14
+
13
15
  def initialize(options={}, &block)
14
16
  apply_configuration(options)
15
17
 
18
+ self.root = current_options.delete(:root)
19
+
16
20
  yield(self) if block_given?
17
21
  end
18
22
 
@@ -53,19 +57,19 @@ module PMP
53
57
  end
54
58
 
55
59
  def credentials_url
56
- root_document.auth['urn:collectiondoc:form:listcredentials'].url
60
+ root.auth['urn:collectiondoc:form:listcredentials'].url
57
61
  end
58
62
 
59
63
  def create_credentials_url
60
- root_document.auth['urn:collectiondoc:form:createcredentials'].url
64
+ root.auth['urn:collectiondoc:form:createcredentials'].url
61
65
  end
62
66
 
63
67
  def remove_credentials_url(client_id)
64
- link = root_document.auth['urn:collectiondoc:form:removecredentials']
68
+ link = root.auth['urn:collectiondoc:form:removecredentials']
65
69
  link.where(client_id: client_id).url
66
70
  end
67
71
 
68
- def root_document
72
+ def root
69
73
  @root ||= PMP::CollectionDocument.new(current_options.merge(href: endpoint))
70
74
  end
71
75
 
data/lib/pmp/link.rb CHANGED
@@ -27,13 +27,17 @@ module PMP
27
27
 
28
28
  def initialize(link={}, parent=nil)
29
29
  super()
30
- self.parent = parent || link.delete('parent') || PMP::CollectionDocument.new
30
+ self.parent = parent || link.delete('parent')
31
31
  self.params = link.delete('params') || {}
32
32
  # puts "params: #{params.inspect}"
33
33
  parse_attributes(link)
34
34
  [:href, :href_template, :method].each{|m| self.send("#{m}=", nil) unless respond_to?(m)}
35
35
  end
36
36
 
37
+ def parent
38
+ @parent ||= PMP::CollectionDocument.new
39
+ end
40
+
37
41
  def where(params={})
38
42
  self.class.new(attributes_map.merge({'params'=>params}), parent)
39
43
  end
data/lib/pmp/parser.rb CHANGED
@@ -44,6 +44,7 @@ module PMP
44
44
 
45
45
  def parse(doc)
46
46
  return if (!doc)
47
+ parse_href(doc['href'])
47
48
  parse_version(doc['version'])
48
49
  parse_attributes(doc['attributes'])
49
50
  parse_links(doc['links'])
@@ -51,6 +52,10 @@ module PMP
51
52
  # parse_error(doc)
52
53
  end
53
54
 
55
+ def parse_href(document)
56
+ self.href = document
57
+ end
58
+
54
59
  def parse_version(document)
55
60
  self.version = document || '1.0'
56
61
  end
@@ -65,7 +70,7 @@ module PMP
65
70
  Array(document).each do |k,v|
66
71
  link = parse_link(k,v)
67
72
  if link
68
- self.links[k] = link
73
+ self.links_object[k] = link
69
74
  end
70
75
  end
71
76
  end
data/lib/pmp/token.rb CHANGED
@@ -7,14 +7,18 @@ module PMP
7
7
 
8
8
  include Configuration
9
9
 
10
+ attr_accessor :root
11
+
10
12
  def initialize(options={}, &block)
11
13
  apply_configuration(options)
12
14
 
15
+ self.root = current_options.delete(:root)
16
+
13
17
  yield(self) if block_given?
14
18
  end
15
19
 
16
20
  def token_url
17
- root_document.auth['urn:collectiondoc:form:issuetoken'].url
21
+ root.auth['urn:collectiondoc:form:issuetoken'].url
18
22
  end
19
23
 
20
24
  def get_token
@@ -50,7 +54,7 @@ module PMP
50
54
  options.select{|k,v| PMP::Connection::ALLOWED_CONNECTION_OPTIONS.include?(k.to_sym)}
51
55
  end
52
56
 
53
- def root_document
57
+ def root
54
58
  @root ||= PMP::CollectionDocument.new(current_options.merge(href: endpoint))
55
59
  end
56
60
 
data/lib/pmp/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module PMP
4
- VERSION = "0.5.0"
4
+ VERSION = "0.5.1"
5
5
  end
data/spec/parser_spec.rb CHANGED
@@ -12,6 +12,10 @@ class TestParser < OpenStruct
12
12
  attr_accessor :version
13
13
 
14
14
  def links
15
+ links_object
16
+ end
17
+
18
+ def links_object
15
19
  @links ||= PMP::Links.new(self)
16
20
  end
17
21
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kuklewicz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-01 00:00:00.000000000 Z
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler