-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_import.rb
229 lines (188 loc) · 6.61 KB
/
data_import.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
# frozen_string_literal: true
require_relative '../helpers/folio_request'
# Module to encapsulate methods used by import_profiles rake tasks
module DataImportTaskHelpers
include FolioRequestHelper
def job_profiles_json
JSON.parse(File.read("#{Settings.json}/data_import/job_profiles.json"))
end
def job_profiles_post(obj)
@@folio_request.post('/data-import-profiles/jobProfiles', obj.to_json)
end
def job_profiles_put(obj)
@@folio_request.put("/data-import-profiles/jobProfiles/#{obj['profile']['id']}", obj.to_json)
end
def job_profiles_delete(id)
@@folio_request.delete("/data-import-profiles/jobProfiles/#{id}", response_code: true)
end
def job_profiles_get(name)
response = @@folio_request.get_cql('/data-import-profiles/jobProfiles', "name==#{name}")['jobProfiles']
begin
response[0]['id']
rescue NoMethodError
nil
end
end
def match_profiles_json
JSON.parse(File.read("#{Settings.json}/data_import/match_profiles.json"))
end
def match_profiles_post(obj)
@@folio_request.post('/data-import-profiles/matchProfiles', obj.to_json)
end
def match_profiles_put(obj)
@@folio_request.put("/data-import-profiles/matchProfiles/#{obj['profile']['id']}", obj.to_json)
end
def match_profiles_delete(id)
@@folio_request.delete("/data-import-profiles/matchProfiles/#{id}", response_code: true)
end
def match_profiles_get(name)
response = @@folio_request.get_cql('/data-import-profiles/matchProfiles', "name==#{name}")['matchProfiles']
begin
response[0]['id']
rescue NoMethodError
nil
end
end
def action_profiles_json
JSON.parse(File.read("#{Settings.json}/data_import/action_profiles.json"))
end
def action_profiles_post(obj)
@@folio_request.post('/data-import-profiles/actionProfiles', obj.to_json)
end
def action_profiles_put(obj)
@@folio_request.put("/data-import-profiles/actionProfiles/#{obj['profile']['id']}", obj.to_json)
end
def action_profiles_delete(id)
@@folio_request.delete("/data-import-profiles/actionProfiles/#{id}", response_code: true)
end
def action_profiles_get(name)
response = @@folio_request.get_cql('/data-import-profiles/actionProfiles', "name==#{name}")['actionProfiles']
begin
response[0]['id']
rescue NoMethodError
nil
end
end
def mapping_profiles_json
JSON.parse(File.read("#{Settings.json}/data_import/mapping_profiles.json"))
end
def mapping_profiles_post(obj)
@@folio_request.post('/data-import-profiles/mappingProfiles', obj.to_json)
end
def mapping_profiles_put(obj)
@@folio_request.put("/data-import-profiles/mappingProfiles/#{obj['profile']['id']}", obj.to_json)
end
def mapping_profiles_delete(id)
@@folio_request.delete("/data-import-profiles/mappingProfiles/#{id}", response_code: true)
end
def mapping_profiles_get(name)
response = @@folio_request.get_cql('/data-import-profiles/mappingProfiles', "name==#{name}")['mappingProfiles']
begin
response[0]['id']
rescue NoMethodError
nil
end
end
def profile_associations_json
JSON.parse(File.read("#{Settings.json}/data_import/profile_associations.json"))
end
def import_profile_hash(obj)
payload = {}
payload['profile'] = obj
payload
end
def profile_associations_post(payload, master, detail)
@@folio_request.post("/data-import-profiles/profileAssociations?detail=#{detail}&master=#{master}",
payload.to_json)
end
def profile_associations_put(payload, master, detail)
@@folio_request.put("/data-import-profiles/profileAssociations/#{payload['id']}?detail=#{detail}&master=#{master}",
payload.to_json)
end
def pull_action_profiles
hash = @@folio_request.get('/data-import-profiles/actionProfiles?withRelations=true&limit=999')
trim_default_data(hash, 'actionProfiles')
hash.to_json
end
def pull_job_profiles
hash = @@folio_request.get('/data-import-profiles/jobProfiles?limit=999')
trim_default_data(hash, 'jobProfiles')
hash.to_json
end
def pull_mapping_profiles
hash = @@folio_request.get('/data-import-profiles/mappingProfiles?withRelations=true&limit=999')
trim_default_data(hash, 'mappingProfiles')
hash.to_json
end
def pull_match_profiles
hash = @@folio_request.get('/data-import-profiles/matchProfiles?withRelations=true&limit=999')
trim_default_data(hash, 'matchProfiles')
hash.to_json
end
def pull_profile_associations
hash = { 'profileAssociations' => [] }
master = %w[JOB ACTION MATCH]
master.each do |profile_m|
details = profile_m == 'ACTION' ? %w[ACTION MAPPING MATCH] : %w[ACTION MATCH]
details.each do |profile_d|
profile_associations = @@folio_request.get(
"/data-import-profiles/profileAssociations?master=#{profile_m}_PROFILE&detail=#{profile_d}_PROFILE"
)
profile_associations['profileAssociations'].each do |obj|
hash['profileAssociations'].append(obj) unless duplicate_association(hash, obj)
end
end
end
hash.to_json
end
def duplicate_association(hash, obj)
duplicate_association = false
hash['profileAssociations'].each do |assoc|
(assoc.key(obj['masterProfileId']) &&
assoc.key(obj['detailProfileId']) &&
assoc.key(obj['masterProfileType']) &&
assoc.key(obj['detailProfileType'])) && duplicate_association = true
end
duplicate_association
end
def pull_marc_bib_mappings
hash = @@folio_request.get('/mapping-rules/marc-bib')
hash.to_json
end
def pull_marc_hold_mappings
hash = @@folio_request.get('/mapping-rules/marc-holdings')
hash.to_json
end
def marc_bib_mapping_json
JSON.parse(File.read("#{Settings.json}/data_import/marc_bib_mappings.json"))
end
def marc_bib_mapping_put(obj)
@@folio_request.put('/mapping-rules/marc-bib', obj.to_json)
end
def marc_hold_mapping_json
JSON.parse(File.read("#{Settings.json}/data_import/marc_hold_mappings.json"))
end
def marc_hold_mapping_put(obj)
@@folio_request.put('/mapping-rules/marc-holdings', obj.to_json)
end
def profile_associations_load(payload, master, detail)
uuids = profile_associations_ids
if uuids.include?(payload['id'])
profile_associations_put(payload, master, detail)
else
profile_associations_post(payload, master, detail)
end
end
def remove_values(hash, key_name)
hash.each do |k, v|
if k == key_name
hash.delete(k)
elsif v.is_a?(Hash)
remove_values(v, key_name)
elsif v.is_a?(Array)
v.flatten.each { |x| remove_values(x, key_name) if x.is_a?(Hash) }
end
end
hash
end
end