-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaardvark-schema-patching.rb
More file actions
38 lines (30 loc) · 1.18 KB
/
aardvark-schema-patching.rb
File metadata and controls
38 lines (30 loc) · 1.18 KB
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
require 'json'
int_array_keys = %w( gbl_indexYear_im )
string_array_keys = %w( dct_description_sm
dct_identifier_sm
dct_language_sm
dct_publisher_sm
gbl_resourceClass_sm
gbl_resourceType_sm
)
def as_string_array(value)
return value if value.is_a? Array and value.first.is_a? String
[value.to_s].flatten
end
def as_int_array(value)
return value if value.is_a? Array and value.first.is_a? Integer
[value.to_int].flatten
end
Dir.glob("./metadata-aardvark/*/**/*.json").each do |path|
# read record
record = JSON.parse File.read(path)
# patch record
# cast values as arrays of strings
string_array_keys.each { |key| record[key] = as_string_array record[key] }
# cast values as arrays of integers
int_array_keys.each { |key| record[key] = as_int_array record[key] }
# special case: gbl_resourceClass_sm should be set to "Other" by default instead of empty string
record['gbl_resourceClass_sm'] = ['Other'] if record['gbl_resourceClass_sm'].first.empty?
# write updated
File.write(path, JSON.pretty_generate(record))
end