-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_methods.rb
437 lines (394 loc) · 12.7 KB
/
helper_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env ruby
require 'archivesspace/client'
#require_relative 'authentication'
def aspace_login()
#configure access
@config = ArchivesSpace::Configuration.new({
base_uri: ENV['ASPACE_URL'],
base_repo: "",
username: ENV['ASPACE_USER'],
password: ENV['ASPACE_PASSWORD'],
#page_size: 50,
throttle: 0,
verify_ssl: false
})
#log in
@client = ArchivesSpace::Client.new(@config).login
end
def aspace_staging_login()
#configure access
@config = ArchivesSpace::Configuration.new({
base_uri: ENV['ASPACE_STAGING_URL'],
base_repo: "",
username: ENV['ASPACE_USER'],
password: ENV['ASPACE_PASSWORD'],
#page_size: 50,
throttle: 0,
verify_ssl: false,
})
#log in
@client = ArchivesSpace::Client.new(@config).login
end
def get_all_resource_records_for_institution(resolve = [])
#run through all repositories (1 and 2 are reserved for admin use)
resources_endpoints = []
repos_all = (3..12).to_a
repos_all.each do |repo|
resources_endpoints << 'repositories/'+repo.to_s+'/resources'
end
#debug
#puts "endpoints to process are:"
#puts resources_endpoints
#for each endpoint, get the count of records
@results = []
resources_endpoints.each do |endpoint|
@ids_by_endpoint = []
@ids_by_endpoint << @client.get(endpoint, {
query: {
all_ids: true
}}).parsed
@ids_by_endpoint = @ids_by_endpoint.flatten!
count_ids = @ids_by_endpoint.count
#debug
#puts "number of records to retrieve for #{endpoint}:"
#puts count_ids
#for each endpoint, get the record by id and add to array of records
paginate_endpoint(@ids_by_endpoint, count_ids, endpoint, resolve)
end #close resources_endpoints.each
#return array of results
@results = @results.flatten!
end #close method
def get_all_event_records_for_institution(resolve = [])
#run through all repositories (1 and 2 are reserved for admin use)
resources_endpoints = []
repos_all = (3..12).to_a
repos_all.each do |repo|
resources_endpoints << 'repositories/'+repo.to_s+'/events'
end
#debug
#puts "endpoints to process are:"
#puts resources_endpoints
#for each endpoint, get the count of records
@results = []
resources_endpoints.each do |endpoint|
@ids_by_endpoint = []
@ids_by_endpoint << @client.get(endpoint, {
query: {
all_ids: true
}}).parsed
@ids_by_endpoint = @ids_by_endpoint.flatten!
count_ids = @ids_by_endpoint.count
#debug
#puts "number of records to retrieve for #{endpoint}:"
#puts count_ids
#for each endpoint, get the record by id and add to array of records
paginate_endpoint(@ids_by_endpoint, count_ids, endpoint, resolve)
end #close resources_endpoints.each
#return array of results
@results = @results.flatten!
end #close method
def paginate_endpoint(ids, count_ids, endpoint, resolve)
count_processed_records = 0
while count_processed_records < count_ids do
last_record = [count_processed_records+249, count_ids].min
@results << @client.get(endpoint, {
query: {
id_set: ids[count_processed_records..last_record],
resolve: resolve
}
}).parsed
count_processed_records = last_record
end
end
def construct_endpoint(repo, endpoint_name)
endpoint = 'repositories/'+repo.to_s+'/'+endpoint_name.to_s
end
def get_paginated_records(repo, endpoint_name, resolve)
@results = []
#get endpoint
endpoint = construct_endpoint(repo, endpoint_name)
#get all ids
ids = []
ids << @client.get(endpoint, {
query: {
all_ids: true
}}).parsed
#get a count of ids
count_ids = ids.flatten!.count
#for each id, get the record and add to array of records
paginate_endpoint(ids, count_ids, endpoint, resolve)
end
def get_all_records_for_repo_endpoint(repo, endpoint_name, resolve = [])
get_paginated_records(repo, endpoint_name, resolve)
@results.flatten!
end
def get_single_resource_by_id(repo, id, resolve = [])
endpoint_name = '/resources/'
endpoint = construct_endpoint(repo, endpoint_name)
id = id.to_s
@client.get(endpoint + id,{
query: {
id_set: id,
resolve: resolve
}}).parsed
end
def get_single_archival_object_by_id(repo, id, resolve = [])
endpoint_name = '/archival_objects/'
endpoint = construct_endpoint(repo, endpoint_name)
id = id.to_s
@client.get(endpoint + id,{
query: {
id_set: id,
resolve: resolve
}}).parsed
end
def get_single_container_by_id(repo, id, resolve = [])
endpoint_name = '/top_containers/'
endpoint = construct_endpoint(repo, endpoint_name)
id = id.to_s
@client.get(endpoint + id,{
query: {
id_set: id,
resolve: resolve
}}).parsed
end
def get_single_event_by_id(repo, id, resolve = [])
endpoint_name = '/events/'
endpoint = construct_endpoint(repo, endpoint_name)
id = id.to_s
@client.get(endpoint + id,{
query: {
id_set: id,
resolve: resolve
}}).parsed
end
def get_single_do_by_id(repo, id, resolve = [])
endpoint_name = '/digital_objects/'
endpoint = construct_endpoint(repo, endpoint_name)
id = id.to_s
@client.get(endpoint + id,{
query: {
id_set: id,
resolve: resolve
}}).parsed
end
def get_single_archival_object_by_cid(repo, cid, resolve = [])
endpoint_name = 'archival_objects'
components_all = get_all_records_for_repo_endpoint(repo, endpoint_name, resolve)
selected_resources = components_all.select do |c|
c['ref_id'] == cid
end
end
def get_single_resource_by_eadid(repo, eadid, resolve = [])
endpoint_name = 'resources'
collections_all = get_all_records_for_repo_endpoint(repo, endpoint_name, resolve)
selected_resources = collections_all.select do |c|
c['ead_id'] == eadid
end
end
def get_uris_by_eadids(eadids, resolve = [])
collections_all = get_all_resource_records_for_institution()
selected_resources = []
uris = []
selected_resources << collections_all.select {|collection| eadids.include? collection['ead_id']}
selected_resources.flatten.each {|resource| uris << "#{resource['uri']}, #{resource['ead_id']}"}
uris
end
#get resource records by eadids
#this method assumes that there is no duplication of eadids across repositories
def get_array_of_resources_by_eadids(eadids, resolve = [])
collections_all = get_all_resource_records_for_institution()
selected_resources = []
selected_resources << collections_all.select {|collection| eadids.include? collection['ead_id']}
end
def get_agent_by_id(agent_type, agent_id, resolve = [])
endpoint_name = '/agents/' + agent_type
ids = @client.get(endpoint_name.to_s, {
query: {
id_set: agent_id.to_s,
resolve: resolve
#all_ids: true
}}).parsed
end
def get_person_by_id_as_xml(repo_id, agent_id)
endpoint_name = '/repositories/' + repo_id.to_s + '/archival_contexts/people/' + agent_id.to_s + '.xml'
@client.get(endpoint_name.to_s).parsed
end
def get_all_top_container_records_for_institution(resolve = [])
#run through all repositories (1 and 2 are reserved for admin use)
resources_endpoints = []
repos_all = (3..12).to_a
repos_all.each do |repo|
resources_endpoints << 'repositories/'+repo.to_s+'/top_containers'
end
#for each endpoint, get the count of records
@results = []
resources_endpoints.each do |endpoint|
@ids_by_endpoint = []
@ids_by_endpoint << @client.get(endpoint, {
query: {
all_ids: true
}}).parsed
@ids_by_endpoint = @ids_by_endpoint.flatten!
count_ids = @ids_by_endpoint.count
#for each endpoint, get the record by id and add to array of records
paginate_endpoint(@ids_by_endpoint, count_ids, endpoint, resolve)
end #close resources_endpoints.each
@results = @results.flatten!
end
def get_all_digital_object_records_for_a_repository(repo, resolve = [])
resources_endpoints = []
resources_endpoints << 'repositories/'+repo.to_s+'/digital_objects'
#for each endpoint, get the count of records
@results = []
resources_endpoints.each do |endpoint|
@ids_by_endpoint = []
@ids_by_endpoint << @client.get(endpoint, {
query: {
all_ids: true
}}).parsed
@ids_by_endpoint = @ids_by_endpoint.flatten!
count_ids = @ids_by_endpoint.count
#for each endpoint, get the record by id and add to array of records
paginate_endpoint(@ids_by_endpoint, count_ids, endpoint, resolve)
end #close resources_endpoints.each
@results = @results.flatten!
end
def get_all_archival_objects_for_resource(repo, id, resolve = [])
archival_objects_all = get_all_records_for_repo_endpoint(repo, 'archival_objects', resolve)
archival_objects_filtered =
archival_objects_all.select do |ao|
ao['resource']['ref'] == "/repositories/#{repo}/resources/#{id}"
end
end
#add a revision statement to a resource uri.
def add_revision_statement(uri, description)
resource = @client.get(uri).parsed
revision_statement =
{"date"=>Time.now,
"description"=>description}
resource['revision_statements'] = resource['revision_statements'].append(revision_statement)
post = @client.post(uri, resource.to_json)
puts post.body
end
def get_all_resource_uris_for_institution()
#run through all repositories (1 and 2 are reserved for admin use)
resources_endpoints = []
repos_all = (3..12).to_a
repos_all.each do |repo|
resources_endpoints << 'repositories/'+repo.to_s+'/resources'
end
@uris = []
resources_endpoints.each do |endpoint|
ids_by_endpoint = []
ids_by_endpoint << @client.get(endpoint, {
query: {
all_ids: true
}}).parsed
ids_by_endpoint = ids_by_endpoint.flatten!
ids_by_endpoint.each do |id|
@uris << "/#{endpoint}/#{id}"
end
end #close resources_endpoints.each
@uris
end #close method
#return a hash of users (does not include credentials)
def get_users()
endpoint_name = '/users'
ids = @client.get(endpoint_name, {
query: {
all_ids: true
}}).parsed.join(',')
users = @client.get(endpoint_name, {
query: {
id_set: ids
}
}).parsed
end
#return a hash of user credentials
def get_user_permissions()
ids = @client.get('/users', {
query: {
all_ids: true
}}).parsed
ids.map { |id| @client.get("/users/#{id}").parsed }
end
#add a maintenance statement to agent records or create the field if it doesn't exist
def add_maintenance_history(record, text)
if record['agent_maintenance_histories'].nil?
record['agent_maintenance_histories'] = [{
"maintenance_event_type"=>"updated",
"maintenance_agent_type"=>"machine",
"agent"=>"system",
"event_date"=>"#{Time.now}",
"descriptive_note"=>text.to_s,
"created_by"=>"aspace_helpers",
"publish"=>true,
"jsonmodel_type"=>"agent_maintenance_history"
}]
else
record['agent_maintenance_histories'] << {
"maintenance_event_type"=>"updated",
"maintenance_agent_type"=>"machine",
"agent"=>"system",
"event_date"=>"#{Time.now}",
"descriptive_note"=>text.to_s,
"created_by"=>"aspace_helpers",
"publish"=>true,
"jsonmodel_type"=>"agent_maintenance_history"
}
end
end
#add a revision statement to a resource record
def add_resource_revision_statement(record, text)
record['revision_statements'] << {
"date"=>"#{Time.now}",
"created_by"=>"system",
"last_modified_by"=>"aspace_helpers",
"create_time"=>"#{Time.now}",
"description"=>text.to_s,
"publish"=>true,
"jsonmodel_type"=>"revision_statement"
}
end
#get resource uri's for specific repositories
#add repositories in as an array of ids
def get_all_resource_uris_for_repos(repos = [])
resources_endpoints = []
repos.each do |repo|
resources_endpoints << 'repositories/'+repo.to_s+'/resources'
end
@uris = []
resources_endpoints.each do |endpoint|
ids_by_endpoint = []
ids_by_endpoint << @client.get(endpoint, {
query: {
all_ids: true
}}).parsed
ids_by_endpoint = ids_by_endpoint.flatten!
ids_by_endpoint.each do |id|
@uris << "/#{endpoint}/#{id}"
end
end #close resources_endpoints.each
@uris
end #close method
def get_index_of_resource_uri(uri, repo)
uris = get_all_resource_uris_for_repos([repo])
uris.index(uri)
end
#get linked objects from descriptive records
def get_resolved_objects_from_ids(repository_id, input_ids, record_type, record_types_to_prefetch)
all_records = []
count_processed_records = 0
count_ids = input_ids.count
while count_processed_records < count_ids
last_record = [count_processed_records+29, count_ids].min
all_records << @client.get("/repositories/#{repository_id}/#{record_type}",
query: {
id_set: input_ids[count_processed_records..last_record],
resolve: record_types_to_prefetch
}).parsed
count_processed_records = last_record
end
all_records = all_records.flatten
end