Skip to content

Commit 02f7173

Browse files
committed
copilot comments
1 parent 71e8efa commit 02f7173

4 files changed

Lines changed: 58 additions & 38 deletions

File tree

lib/ingestors/auto_parsing.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Ingestors
2+
module AutoParsing
3+
def auto_parse(var, description)
4+
@auto_parse_cache ||= {}
5+
json_path = File.join(Rails.root, 'lib', 'ingestors', 'auto_parser_mappings', "#{var.to_s}.json")
6+
return nil unless File.exist?(json_path)
7+
8+
mtime = File.mtime(json_path)
9+
cached = @auto_parse_cache[var]
10+
if cached && cached[:mtime] == mtime
11+
mapping = cached[:mapping]
12+
else
13+
mapping = JSON.parse(File.read(json_path))
14+
@auto_parse_cache[var] = { mtime: mtime, mapping: mapping }
15+
end
16+
17+
mapping
18+
.select{ |key, val| description&.downcase&.include?(key.to_s.downcase) }
19+
&.values
20+
&.uniq
21+
end
22+
end
23+
end

lib/ingestors/event_ingestion.rb

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Ingestors
22
module EventIngestion
3+
include AutoParsing
4+
35
def add_event(event)
46
if event.is_a?(Hash)
57
c = EventsController.new
@@ -9,7 +11,12 @@ def add_event(event)
911
end
1012
TeSS::Config.feature['auto_parse_vars'].each do |var|
1113
new_val = auto_parse(var, event.description)
12-
event.send("#{var}=", new_val)
14+
next if new_val.blank?
15+
16+
current_val = event.send(var) if event.respond_to?(var)
17+
if !event.respond_to?(var) || current_val.blank?
18+
event.send("#{var}=", new_val)
19+
end
1320
end
1421
@events << event unless event.nil?
1522
end
@@ -26,19 +33,6 @@ def convert_location(input)
2633
input
2734
end
2835

29-
def auto_parse(var, description)
30-
json_path = File.join(Rails.root, 'lib', 'ingestors', 'auto_parser_mappings', "#{var.to_s}.json")
31-
res = nil
32-
if File.exist?(json_path)
33-
mapping = JSON.parse(File.read(json_path))
34-
res = mapping
35-
.select{ |key, val| description.downcase.include?(key.to_s.downcase) }
36-
.values
37-
.uniq
38-
end
39-
res
40-
end
41-
4236
def parse_dates(input, timezone = nil)
4337
Time.use_zone(timezone) do
4438
# try to split on obvious interval markers
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Ingestors
22
module MaterialIngestion
3+
include AutoParsing
4+
35
def add_material(material)
46
if material.is_a?(Hash)
57
c = MaterialsController.new
@@ -8,23 +10,15 @@ def add_material(material)
810
material = OpenStruct.new(c.send(:material_params))
911
end
1012
TeSS::Config.feature['auto_parse_vars'].each do |var|
11-
new_val = auto_parse(var, event.description)
12-
event.send("#{var}=", new_val)
13-
end
14-
@materials << material unless material.nil?
15-
end
13+
new_val = auto_parse(var, material.description)
14+
next if new_val.blank?
1615

17-
def auto_parse(var, description)
18-
json_path = File.join(Rails.root, 'lib', 'ingestors', 'auto_parser_mappings', "#{var.to_s}.json")
19-
res = nil
20-
if File.exist?(json_path)
21-
mapping = JSON.parse(File.read(json_path))
22-
res = mapping
23-
.select{ |key, val| description.downcase.include?(key.to_s.downcase) }
24-
.values
25-
.uniq
16+
current_val = material.send(var) if material.respond_to?(var)
17+
if !material.respond_to?(var) || current_val.blank?
18+
material.send("#{var}=", new_val)
19+
end
2620
end
27-
res
21+
@materials << material unless material.nil?
2822
end
2923
end
3024
end

test/unit/ingestors/ingestor_test.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,32 @@ class IngestorTest < ActiveSupport::TestCase
103103
content_provider: provider, user: users(:admin))
104104
ingestor = Ingestors::Ingestor.new
105105
new_event = OpenStruct.new(url: 'https://some-course.net',
106-
title: 'Yet another course',
106+
title: 'Yet another event',
107107
start: '2021-01-31 13:00:00',
108108
end:'2021-01-31 14:00:00',
109109
description: 'professor, tool criticism and software citation in text')
110+
new_material = OpenStruct.new(url: 'https://some-course.net',
111+
title: 'Yet another course',
112+
description: 'professor, tool criticism and software citation in text')
110113
with_settings({ feature: { auto_parse_vars: auto_parse_vars } }) do
111114
ingestor.add_event(new_event)
115+
ingestor.add_material(new_material)
112116
assert_difference('provider.events.count', 1) do
113-
ingestor.write(user, provider, source: @source)
117+
assert_difference('provider.materials.count', 1) do
118+
ingestor.write(user, provider, source: @source)
119+
end
114120
end
115121
end
116-
event = Event.find_by(title: 'Yet another course')
117-
if enabled == 'enabled'
118-
assert_equal(['researchers'], event.target_audience)
119-
assert_equal(['Research software management', 'Data infrastructure'], event.keywords)
120-
else
121-
assert_equal([], event.target_audience)
122-
assert_equal([], event.keywords)
122+
event = Event.find_by(title: 'Yet another event')
123+
material = Material.find_by(title: 'Yet another course')
124+
[event, material].each do |obj|
125+
if enabled == 'enabled'
126+
assert_equal(['researchers'].sort, obj.target_audience.sort)
127+
assert_equal(['Research software management', 'Data infrastructure'].sort, obj.keywords.sort)
128+
else
129+
assert_equal([], obj.target_audience)
130+
assert_equal([], obj.keywords)
131+
end
123132
end
124133
end
125134
end

0 commit comments

Comments
 (0)