Skip to content

Commit 4d26ecd

Browse files
committed
remove duplicated code in OAI-PMH ingestor
1 parent 2c0c6b7 commit 4d26ecd

1 file changed

Lines changed: 15 additions & 60 deletions

File tree

lib/ingestors/oai_pmh_ingestor.rb

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
module Ingestors
55
class OaiPmhIngestor < Ingestor
6-
attr_reader :verbose
7-
86
def self.config
97
{
108
key: 'oai_pmh',
@@ -14,6 +12,13 @@ def self.config
1412
}
1513
end
1614

15+
def initialize
16+
super
17+
18+
# to use some helper functions that are instance level methods of BioschemasIngestor
19+
@bioschemas_manager = BioschemasIngestor.new
20+
end
21+
1722
def read(source_url)
1823
client = OAI::Client.new source_url, headers: { 'From' => config[:mail] }
1924
found_bioschemas = begin
@@ -136,11 +141,11 @@ def read_oai_rdf(client)
136141
@messages << bioschemas_summary
137142
end
138143

139-
deduplicate(provider_events).each do |event_params|
144+
@bioschemas_manager.deduplicate(provider_events).each do |event_params|
140145
add_event(event_params)
141146
end
142147

143-
deduplicate(provider_materials).each do |material_params|
148+
@bioschemas_manager.deduplicate(provider_materials).each do |material_params|
144149
add_material(material_params)
145150
end
146151

@@ -160,27 +165,27 @@ def parse_bioschemas(content)
160165

161166
begin
162167
events = Tess::Rdf::EventExtractor.new(content, :rdfxml).extract do |p|
163-
convert_params(p)
168+
@bioschemas_manager.convert_params(p)
164169
end
165170
courses = Tess::Rdf::CourseExtractor.new(content, :rdfxml).extract do |p|
166-
convert_params(p)
171+
@bioschemas_manager.convert_params(p)
167172
end
168173
course_instances = Tess::Rdf::CourseInstanceExtractor.new(content, :rdfxml).extract do |p|
169-
convert_params(p)
174+
@bioschemas_manager.convert_params(p)
170175
end
171176
learning_resources = Tess::Rdf::LearningResourceExtractor.new(content, :rdfxml).extract do |p|
172-
convert_params(p)
177+
@bioschemas_manager.convert_params(p)
173178
end
174179
output[:totals]['Events'] += events.count
175180
output[:totals]['Courses'] += courses.count
176181
output[:totals]['CourseInstances'] += course_instances.count
177182
output[:totals]['LearningResources'] += learning_resources.count
178183

179-
deduplicate(events + courses + course_instances).each do |event|
184+
@bioschemas_manager.deduplicate(events + courses + course_instances).each do |event|
180185
output[:resources][:events] << event
181186
end
182187

183-
deduplicate(learning_resources).each do |material|
188+
@bioschemas_manager.deduplicate(learning_resources).each do |material|
184189
output[:resources][:materials] << material
185190
end
186191
rescue StandardError => e
@@ -199,55 +204,5 @@ def parse_bioschemas(content)
199204

200205
output
201206
end
202-
203-
# ---- This is copied unchanged from bioschemas_ingestor.rb and needs to be refactored. ----
204-
# note that also attr_reader :verbose is probably related to this
205-
206-
# If duplicate resources have been extracted, prefer ones with the most metadata.
207-
def deduplicate(resources)
208-
return [] unless resources.any?
209-
210-
puts "De-duplicating #{resources.count} resources" if verbose
211-
hash = {}
212-
scores = {}
213-
resources.each do |resource|
214-
resource_url = resource[:url]
215-
puts " Considering: #{resource_url}" if verbose
216-
if hash[resource_url]
217-
score = metadata_score(resource)
218-
# Replace the resource if this resource has a higher metadata score
219-
puts " Duplicate! Comparing #{score} vs. #{scores[resource_url]}" if verbose
220-
if score > scores[resource_url]
221-
puts ' Replacing resource' if verbose
222-
hash[resource_url] = resource
223-
scores[resource_url] = score
224-
end
225-
else
226-
puts ' Not present, adding' if verbose
227-
hash[resource_url] = resource
228-
scores[resource_url] = metadata_score(resource)
229-
end
230-
end
231-
232-
puts "#{hash.values.count} resources after de-duplication" if verbose
233-
234-
hash.values
235-
end
236-
237-
# Score based on number of metadata fields available
238-
def metadata_score(resource)
239-
score = 0
240-
resource.each_value do |value|
241-
score += 1 unless value.nil? || value == {} || value == [] || (value.is_a?(String) && value.strip == '')
242-
end
243-
244-
score
245-
end
246-
247-
def convert_params(params)
248-
params[:description] = convert_description(params[:description]) if params.key?(:description)
249-
250-
params
251-
end
252207
end
253208
end

0 commit comments

Comments
 (0)