Open eightBitter opened 2 years ago
Here's the gist in case I lose access:
module Common
class SubjectGetter
def self.call
Aspace_Client.client.use_global_repository
page = 1
data = []
response = Aspace_Client.client.get('subjects', query: {page: page, page_size: 100})
last_page = response.result['last_page']
while page <= last_page
response = Aspace_Client.client.get('subjects', query: {page: page, page_size: 100})
data << response.result['results']
page += 1
end
data.flatten
end
end
class SubjectIndexMaker
def self.call(data = SubjectGetter.call)
index = {}
data.each do |record|
index[record['title'].gsub(" -- ", "--")] = record['uri']
end
index
end
end
class SubjectAttacher
def self.call(data, field, index = SubjectIndexMaker.call)
data.each do |record|
# sets the variable to empty array if the referenced array is nil; otherwise sets the variable to the array
# this makes it so that this doesn't override the array if it already exists - it would instead add to the array
subjects_refs = record["subjects__refs"].nil? ? [] : record["subjects__refs"]
record[field].each {|entity| subjects_refs << index[entity]}
record["subjects__refs"] = subjects_refs
end
data
end
end
class Subjects < Thor
desc 'get_subjects', 'retrieve API response of all subject data in ASpace'
def get_subjects(*args)
SubjectGetter.call(args)
end
desc 'make_index', 'create the following index - "title:uri"'
def make_index(*args)
SubjectIndexMaker.call
end
desc "attach_subjects", "attach subjects refs to object by matching values from the given field. assumes DATA is an array of hashes, FIELD is a string"
def attach_subjects(data,field)
SubjectAttacher.call(data, field)
end
end
end
#----- separate spec file
RSpec.describe Common::SubjectIndexMaker do
describe '.call' do
it 'returns index' do
# now it is easy to text what this does with different input
# because you can just put test input in the test
data = 'paste/type data in expected format here'
expected = 'paste/type data in expected format here'
expect(Common::SubjectIndexMaker.call(data)).to eq(expected)
end
end
end
#----- separate spec file
RSpec.describe Common::SubjectAttacher do
describe '.call' do
it 'returns index' do
# now it is easy to text what this does with different input
# because you can just put test input in the test
data = 'paste/type data in expected format here'
field = 'fieldname'
index = 'paste/type data in expected format here'
expected = 'paste/type data in expected format here'
expect(Common::SubjectAttacher.call(data, field, index)).to eq(expected)
end
end
end