puree 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,18 +7,178 @@ module Puree
7
7
  # @param endpoint [String]
8
8
  # @param optional username [String]
9
9
  # @param optional password [String]
10
- def initialize(endpoint: nil, username: nil, password: nil)
10
+ # @param optional basic_auth [Boolean]
11
+ def initialize(endpoint: nil, username: nil, password: nil, basic_auth: nil)
11
12
  super(api: :project,
12
13
  endpoint: endpoint,
13
14
  username: username,
14
- password: password)
15
+ password: password,
16
+ bleeding: false,
17
+ basic_auth: basic_auth)
18
+ end
19
+
20
+
21
+
22
+ # Acronym
23
+ #
24
+ # @return [String]
25
+ def acronym
26
+ path = '/acronym'
27
+ xpath_query_for_single_value path
28
+ end
29
+
30
+ # Description
31
+ #
32
+ # @return [String]
33
+ def description
34
+ path = '/description/localizedString'
35
+ xpath_query_for_single_value path
36
+ end
37
+
38
+ # Organisation
39
+ #
40
+ # @return [Array<Hash>]
41
+ def organisation
42
+ path = '/organisations/association/organisation'
43
+ xpath_result = xpath_query path
44
+ data = []
45
+ xpath_result.each do |i|
46
+ o = {}
47
+ o['uuid'] = i.xpath('@uuid').text.strip
48
+ o['name'] = i.xpath('name/localizedString').text.strip
49
+ o['type'] = i.xpath('typeClassification/term/localizedString').text.strip
50
+ data << o
51
+ end
52
+ data
53
+ end
54
+
55
+ # Owner
56
+ #
57
+ # @return [Hash]
58
+ def owner
59
+ path = '/owner'
60
+ xpath_result = xpath_query path
61
+ o = {}
62
+ o['uuid'] = xpath_result.xpath('@uuid').text.strip
63
+ o['name'] = xpath_result.xpath('name/localizedString').text.strip
64
+ o['type'] = xpath_result.xpath('typeClassification/term/localizedString').text.strip
65
+ o
66
+ end
67
+
68
+ # Person (internal, external, other)
69
+ #
70
+ # @return [Array<Hash>]
71
+ def person
72
+ data = {}
73
+ # internal
74
+ path = '/persons/participantAssociation'
75
+ xpath_result = xpath_query path
76
+ internal = []
77
+ external = []
78
+ other = []
79
+
80
+ xpath_result.each do |i|
81
+ o = {}
82
+ name = {}
83
+ name['first'] = i.xpath('person/name/firstName').text.strip
84
+ name['last'] = i.xpath('person/name/lastName').text.strip
85
+ o['name'] = name
86
+ o['role'] = i.xpath('personRole/term/localizedString').text.strip
87
+
88
+ uuid_internal = i.at_xpath('person/@uuid')
89
+ uuid_external = i.at_xpath('externalPerson/@uuid')
90
+ if uuid_internal
91
+ o['uuid'] = uuid_internal
92
+ internal << o
93
+ elsif uuid_external
94
+ o['uuid'] = uuid_external
95
+ external << o
96
+ else
97
+ other << o
98
+ o['uuid'] = ''
99
+ end
100
+ end
101
+ data['internal'] = internal
102
+ data['external'] = external
103
+ data['other'] = other
104
+ data
105
+ end
106
+
107
+ # Status
108
+ #
109
+ # @return [String]
110
+ def status
111
+ path = '/status/term/localizedString'
112
+ xpath_query_for_single_value path
113
+ end
114
+
115
+ # Temporal, expected and actual start and end dates as UTC datetime.
116
+ #
117
+ # @return [Hash]
118
+ def temporal
119
+ o = {}
120
+ o['expected'] = {}
121
+ o['actual'] = {}
122
+
123
+ path = '/expectedStartDate'
124
+ xpath_result = xpath_query path
125
+ o['expected']['start'] = xpath_result.text.strip
126
+
127
+ path = '/expectedEndDate'
128
+ xpath_result = xpath_query path
129
+ o['expected']['end'] = xpath_result.text.strip
130
+
131
+ path = '/startFinishDate/startDate'
132
+ xpath_result = xpath_query path
133
+ o['actual']['start'] = xpath_result.text.strip
134
+
135
+ path = '/startFinishDate/endDate'
136
+ xpath_result = xpath_query path
137
+ o['actual']['end'] = xpath_result.text.strip
138
+
139
+ o
140
+ end
141
+
142
+ # Title
143
+ #
144
+ # @return [String]
145
+ def title
146
+ path = '/title/localizedString'
147
+ xpath_query_for_single_value path
148
+ end
149
+
150
+ # Type
151
+ #
152
+ # @return [String]
153
+ def type
154
+ path = '/typeClassification/term/localizedString'
155
+ xpath_query_for_single_value path
156
+ end
157
+
158
+ # URL
159
+ #
160
+ # @return [String]
161
+ def url
162
+ path = '/projectURL'
163
+ xpath_query_for_single_value path
15
164
  end
16
165
 
17
166
  # All metadata
18
167
  #
19
168
  # @return [Hash]
20
169
  def metadata
21
- super
170
+ o = super
171
+ o['acronym'] = acronym
172
+ o['description'] = description
173
+ o['organisation'] = organisation
174
+ o['owner'] = owner
175
+ o['person'] = person
176
+ o['status'] = status
177
+ o['temporal'] = temporal
178
+ o['title'] = title
179
+ o['type'] = type
180
+ o['url'] = url
181
+ o
22
182
  end
23
183
 
24
184
  end
@@ -7,20 +7,41 @@ module Puree
7
7
  # @param endpoint [String]
8
8
  # @param optional username [String]
9
9
  # @param optional password [String]
10
- def initialize(endpoint: nil, username: nil, password: nil)
10
+ # @param optional basic_auth [Boolean]
11
+ def initialize(endpoint: nil, username: nil, password: nil, basic_auth: nil)
11
12
  super(api: :publication,
12
13
  endpoint: endpoint,
13
14
  username: username,
14
- password: password)
15
+ password: password,
16
+ basic_auth: basic_auth)
17
+ end
18
+
19
+ # Category
20
+ #
21
+ # @return [String]
22
+ def category
23
+ path = '/publicationCategory/publicationCategory/term/localizedString'
24
+ xpath_query_for_single_value path
15
25
  end
16
26
 
17
27
  # Description
18
28
  #
19
29
  # @return [String]
20
30
  def description
21
- path = '//abstract/localizedString'
22
- xpath_result = xpath_query path
23
- xpath_result ? xpath_result.text.strip : ''
31
+ path = '/abstract/localizedString'
32
+ xpath_query_for_single_value path
33
+ end
34
+
35
+ # Event
36
+ #
37
+ # @return [Hash]
38
+ def event
39
+ path = '/event'
40
+ xpath_result = xpath_query path
41
+ o = {}
42
+ o['uuid'] = xpath_result.xpath('@uuid').text.strip
43
+ o['title'] = xpath_result.xpath('title/localizedString').text.strip
44
+ o
24
45
  end
25
46
 
26
47
  # Digital Object Identifier
@@ -28,16 +49,15 @@ module Puree
28
49
  # @return [String]
29
50
  def doi
30
51
  path = '//doi'
31
- xpath_result = xpath_query path
32
- xpath_result ? xpath_result.text.strip : ''
52
+ xpath_query_for_single_value path
33
53
  end
34
54
 
35
55
  # Supporting file
36
56
  #
37
57
  # @return [Array<Hash>]
38
58
  def file
39
- path = '//electronicVersionFileAssociations/electronicVersionFileAssociation/file'
40
- xpath_result = xpath_query path
59
+ path = '/electronicVersionAssociations/electronicVersionFileAssociations/electronicVersionFileAssociation/file'
60
+ xpath_result = xpath_query path
41
61
  docs = []
42
62
  xpath_result.each do |d|
43
63
  doc = {}
@@ -51,22 +71,112 @@ module Puree
51
71
  docs.uniq
52
72
  end
53
73
 
74
+ # Organisation
75
+ #
76
+ # @return [Array<Hash>]
77
+ def organisation
78
+ path = '/organisations/association/organisation'
79
+ xpath_result = xpath_query path
80
+ data = []
81
+ xpath_result.each do |i|
82
+ o = {}
83
+ o['uuid'] = i.xpath('@uuid').text.strip
84
+ o['name'] = i.xpath('name/localizedString').text.strip
85
+ o['type'] = i.xpath('typeClassification/term/localizedString').text.strip
86
+ data << o
87
+ end
88
+ data
89
+ end
90
+
91
+ # Page
92
+ #
93
+ # @return [Array<String>]
94
+ def page
95
+ path = '/numberOfPages'
96
+ xpath_query_for_single_value path
97
+ end
98
+
99
+ # Person (internal, external, other)
100
+ #
101
+ # @return [Array<Hash>]
102
+ def person
103
+ data = {}
104
+ # internal
105
+ path = '/persons/personAssociation'
106
+ xpath_result = xpath_query path
107
+ internal = []
108
+ external = []
109
+ other = []
110
+
111
+ xpath_result.each do |i|
112
+ o = {}
113
+ name = {}
114
+ name['first'] = i.xpath('name/firstName').text.strip
115
+ name['last'] = i.xpath('name/lastName').text.strip
116
+ o['name'] = name
117
+ o['role'] = i.xpath('personRole/term/localizedString').text.strip
118
+
119
+ uuid_internal = i.at_xpath('person/@uuid')
120
+ uuid_external = i.at_xpath('externalPerson/@uuid')
121
+ if uuid_internal
122
+ o['uuid'] = uuid_internal
123
+ internal << o
124
+ elsif uuid_external
125
+ o['uuid'] = uuid_external
126
+ external << o
127
+ else
128
+ other << o
129
+ o['uuid'] = ''
130
+ end
131
+ end
132
+ data['internal'] = internal
133
+ data['external'] = external
134
+ data['other'] = other
135
+ data
136
+ end
137
+
138
+ # Status
139
+ #
140
+ # @return [Array<Hash>]
141
+ def status
142
+ path = '/publicationStatuses/publicationStatus'
143
+ xpath_result = xpath_query path
144
+ data = []
145
+ xpath_result.each do |i|
146
+ o = {}
147
+ o['stage'] = i.xpath('publicationStatus/term/localizedString').text.strip
148
+ ymd = {}
149
+ ymd['year'] = i.xpath('publicationDate/year').text.strip
150
+ ymd['month'] = i.xpath('publicationDate/month').text.strip
151
+ ymd['day'] = i.xpath('publicationDate/day').text.strip
152
+ o['date'] = Puree::Date.normalise(ymd)
153
+ data << o
154
+ end
155
+ data
156
+ end
157
+
54
158
  # Title
55
159
  #
56
160
  # @return [String]
57
161
  def title
58
- path = '//content/title'
59
- xpath_result = xpath_query path
60
- xpath_result ? xpath_result.text.strip : ''
162
+ path = '/title'
163
+ xpath_query_for_single_value path
61
164
  end
62
165
 
63
166
  # Subtitle
64
167
  #
65
168
  # @return [String]
66
169
  def subtitle
67
- path = '//content/subtitle'
68
- xpath_result = xpath_query path
69
- xpath_result ? xpath_result.text.strip : ''
170
+ path = '/subtitle'
171
+ xpath_query_for_single_value path
172
+ end
173
+
174
+ # Type
175
+ #
176
+ # @return [String]
177
+ def type
178
+ path = '/typeClassification/term/localizedString'
179
+ xpath_query_for_single_value path
70
180
  end
71
181
 
72
182
  # All metadata
@@ -74,11 +184,18 @@ module Puree
74
184
  # @return [Hash]
75
185
  def metadata
76
186
  o = super
187
+ o['category'] = category
77
188
  o['description'] = description
78
189
  o['doi'] = doi
190
+ o['event'] = event
79
191
  o['file'] = file
192
+ o['organisation'] = organisation
193
+ o['page'] = page
194
+ o['person'] = person
195
+ o['status'] = status
80
196
  o['subtitle'] = subtitle
81
197
  o['title'] = title
198
+ o['type'] = type
82
199
  o
83
200
  end
84
201
 
@@ -7,13 +7,17 @@ module Puree
7
7
  # @param endpoint [String]
8
8
  # @param optional username [String]
9
9
  # @param optional password [String]
10
- def initialize(endpoint: nil, username: nil, password: nil)
10
+ # @param optional basic_auth [Boolean]
11
+ def initialize(endpoint: nil, username: nil, password: nil, basic_auth: nil)
11
12
  super(api: :publisher,
12
13
  endpoint: endpoint,
13
14
  username: username,
14
- password: password)
15
+ password: password,
16
+ basic_auth: basic_auth)
15
17
  end
16
18
 
19
+
20
+
17
21
  # All metadata
18
22
  #
19
23
  # @return [Hash]
@@ -4,27 +4,48 @@ module Puree
4
4
  #
5
5
  class Resource
6
6
 
7
+ attr_reader :response
8
+
7
9
  # @param api [String]
8
10
  # @param endpoint [String]
9
11
  # @param optional username [String]
10
12
  # @param optional password [String]
13
+ # @param optional bleeding [Boolean]
14
+ # @param optional basic_auth [Boolean]
11
15
  def initialize( api: nil,
12
16
  endpoint: nil,
13
17
  username: nil,
14
- password: nil)
18
+ password: nil,
19
+ bleeding: true,
20
+ basic_auth: nil)
15
21
  @resource_type = api
16
22
  @api_map = Puree::Map.new.get
17
- @endpoint = endpoint.nil? ? Puree::Configuration.endpoint : endpoint
18
- @username = username.nil? ? Puree::Configuration.username : username
19
- @password = password.nil? ? Puree::Configuration.password : password
23
+ @endpoint = endpoint.nil? ? Puree.endpoint : endpoint
24
+ @latest_api = bleeding
25
+ @basic_auth = basic_auth.nil? ? Puree.basic_auth : basic_auth
26
+ if @basic_auth === true
27
+ @username = username.nil? ? Puree.username : username
28
+ @password = password.nil? ? Puree.password : password
29
+ end
20
30
  end
21
31
 
22
32
  # Get
23
33
  #
24
34
  # @param uuid [String]
25
35
  # @param id [String]
26
- # @return [HTTParty::Response]
27
- def get(uuid: nil, id: nil)
36
+ # @return [Hash]
37
+ def get(uuid: nil, id: nil, rendering: :xml_long)
38
+ reset
39
+
40
+ @options = {
41
+ basic_auth: @basic_auth,
42
+ latest_api: @latest_api,
43
+ resource_type: @resource_type.to_sym,
44
+ rendering: rendering,
45
+ uuid: uuid,
46
+ id: id
47
+ }
48
+
28
49
  missing = missing_credentials
29
50
  if !missing.empty?
30
51
  missing.each do |m|
@@ -35,19 +56,15 @@ module Puree
35
56
 
36
57
  # strip any trailing slash
37
58
  @endpoint = @endpoint.sub(/(\/)+$/, '')
38
- @auth = Base64::strict_encode64(@username + ':' + @password)
39
59
 
40
- @options = {
41
- latest_api: true,
42
- resource_type: @resource_type.to_sym,
43
- rendering: :xml_long,
44
- uuid: uuid,
45
- id: id
46
- }
47
- headers = {
48
- 'Accept' => 'application/xml',
49
- 'Authorization' => 'Basic ' + @auth
50
- }
60
+ headers = {}
61
+ headers['Accept'] = 'application/xml'
62
+
63
+ if @options[:basic_auth] === true
64
+ @auth = Base64::strict_encode64(@username + ':' + @password)
65
+ headers['Authorization'] = 'Basic ' + @auth
66
+ end
67
+
51
68
  query = {}
52
69
  query['rendering'] = @options[:rendering]
53
70
 
@@ -59,26 +76,26 @@ module Puree
59
76
  end
60
77
  end
61
78
 
62
- begin
63
- @response = HTTParty.get(build_url, query: query, headers: headers, timeout: 120)
64
- rescue HTTParty::Error => e
65
- puts 'HttParty::Error '+ e.message
79
+ if @options['rendering']
80
+ query['rendering'] = @options['rendering']
66
81
  end
67
82
 
68
- if get_data?
69
- response_name = service_response_name
70
- content = @response.parsed_response[response_name]['result']['content']
71
- set_content(content)
83
+ begin
84
+ url = build_url
85
+ req = HTTP.headers accept: headers['Accept']
86
+ if @options[:basic_auth]
87
+ req = req.auth headers['Authorization']
88
+ end
89
+ @response = req.get(url, params: query)
90
+ @doc = Nokogiri::XML @response.body
91
+ @doc.remove_namespaces!
92
+
93
+ rescue HTTP::Error => e
94
+ puts 'HTTP::Error '+ e.message
72
95
  end
73
- @response
74
- end
75
96
 
76
- # Response, if get method has been called
77
- #
78
- # @return [HTTParty::Response]
79
- # @return [Nil]
80
- def response
81
- @response ? @response : nil
97
+ get_data? ? metadata : {}
98
+
82
99
  end
83
100
 
84
101
  # Set content
@@ -103,24 +120,24 @@ module Puree
103
120
  #
104
121
  # @return [String]
105
122
  def created
106
- data = node 'created'
107
- !data.nil? && !data.empty? ? data.strip : ''
123
+ path = '/created'
124
+ xpath_query_for_single_value path
108
125
  end
109
126
 
110
127
  # Modified (UTC datetime)
111
128
  #
112
129
  # @return [String]
113
130
  def modified
114
- data = node 'modified'
115
- !data.nil? && !data.empty? ? data.strip : ''
131
+ path = '/modified'
132
+ xpath_query_for_single_value path
116
133
  end
117
134
 
118
135
  # UUID
119
136
  #
120
137
  # @return [String]
121
138
  def uuid
122
- data = node 'uuid'
123
- !data.nil? && !data.empty? ? data.strip : ''
139
+ path = '/@uuid'
140
+ xpath_query_for_single_value path
124
141
  end
125
142
 
126
143
  # All metadata
@@ -139,30 +156,13 @@ module Puree
139
156
  private
140
157
 
141
158
 
142
- def collect_uuid
143
- path = '//renderedItem/@renderedContentUUID'
144
- xpath_result = xpath_query path
145
- xpath_result.each { |i| @uuids << i.text.strip }
146
- end
147
-
148
-
149
-
150
-
151
-
152
- # Node
153
- #
154
- # @return [Hash]
155
- def node(path)
156
- @content ? @content[path] : {}
157
- end
158
-
159
-
160
- # Is there any data after get?
159
+ # Is there any data after get? For a response that provides a count of the results.
161
160
  #
162
161
  # @return [Boolean]
163
162
  def get_data?
164
- response_name = service_response_name
165
- @response.parsed_response[response_name]['count'] != '0' ? true : false
163
+ path = service_xpath_count
164
+ xpath_result = @doc.xpath path
165
+ xpath_result.text.strip === '1' ? true : false
166
166
  end
167
167
 
168
168
  def service_name
@@ -175,6 +175,18 @@ module Puree
175
175
  @api_map[:resource_type][resource_type][:response]
176
176
  end
177
177
 
178
+ def service_xpath_base
179
+ service_response_name + '/result/content'
180
+ end
181
+
182
+ def service_xpath_count
183
+ service_response_name + '/count'
184
+ end
185
+
186
+ def service_xpath(str_to_find)
187
+ service_xpath_base + str_to_find
188
+ end
189
+
178
190
  def build_url
179
191
  service = service_name
180
192
  if @options[:latest_api] === false
@@ -185,27 +197,40 @@ module Puree
185
197
  @endpoint + '/' + service_api_mode
186
198
  end
187
199
 
200
+ # content based
188
201
  def xpath_query(path)
189
- xml = @response.body
190
- doc = Nokogiri::XML xml
191
- doc.remove_namespaces!
192
- doc.xpath path
202
+ path_from_root = service_xpath path
203
+ @doc.xpath path_from_root
193
204
  end
194
205
 
206
+ def xpath_query_for_single_value(path)
207
+ xpath_result = xpath_query path
208
+ xpath_result ? xpath_result.text.strip : ''
209
+ end
210
+
211
+
195
212
  def missing_credentials
196
213
  missing = []
197
214
  if @endpoint.nil?
198
215
  missing << 'endpoint'
199
216
  end
200
- if @username.nil?
201
- missing << 'username'
202
- end
203
- if @password.nil?
204
- missing << 'password'
217
+
218
+ if @options[:basic_auth] === true
219
+ if @username.nil?
220
+ missing << 'username'
221
+ end
222
+ if @password.nil?
223
+ missing << 'password'
224
+ end
205
225
  end
226
+
206
227
  missing
207
228
  end
208
229
 
230
+ def reset
231
+ @response = nil
232
+ end
233
+
209
234
  alias :find :get
210
235
 
211
236
  end