Skip to content

Commit b5d1946

Browse files
committed
Switch to arrays and content_tag. Improve scraper log formatting
1 parent d1cf867 commit b5d1946

5 files changed

Lines changed: 38 additions & 41 deletions

File tree

app/helpers/materials_helper.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,21 @@ def display_attribute(resource, attribute, show_label: true, title: nil, markdow
8484
html_escape(block_given? ? yield(value) : value)
8585
end
8686
end
87-
string = "<p class=\"#{attribute}#{show_label ? ' no-spacing' : ''}\">"
88-
unless value.blank? || value.try(:strip) == 'License Not Specified'
89-
string += "<strong class='text-primary'> #{title || resource.class.human_attribute_name(attribute)}: </strong>" if show_label
90-
if list
91-
string += '<ul>'
92-
value.each do |v|
93-
string += "<li>#{v}</li>"
87+
classes = [attribute]
88+
classes << 'no-spacing' if show_label
89+
content_tag(:p, class: classes) do
90+
unless value.blank? || value.try(:strip) == 'License Not Specified'
91+
concat content_tag(:strong, "#{title || resource.class.human_attribute_name(attribute)}: ", class: 'text-primary') if show_label
92+
if list
93+
concat content_tag(:ul, value.map { |v| content_tag(:li, v) })
94+
elsif expandable
95+
height_limit = expandable.is_a?(Numeric) ? expandable : nil
96+
concat content_tag(:div, value.to_s, class: 'tess-expandable', 'data-height-limit': height_limit ? height_limit : nil)
97+
else
98+
concat value.to_s
9499
end
95-
string += '</ul>'
96-
elsif expandable
97-
height_limit = expandable.is_a?(Numeric) ? expandable : nil
98-
string += "<div class=\"tess-expandable\"#{" data-height-limit=\"#{height_limit}\"" if height_limit}>" + value.to_s + '</div>'
99-
else
100-
string += value.to_s
101100
end
102101
end
103-
string += '</p>'
104-
string.html_safe
105102
end
106103

107104
def display_people(resource, attribute)

lib/ingestors/bioschemas_ingestor.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def read(source_url)
2323
provider_events = []
2424
provider_materials = []
2525
totals = Hash.new(0)
26-
no_bioschema_urls = "Bioschemas not found in:\n"
26+
no_bioschema_urls = []
2727
sources.each do |url|
2828
source = open_url(url)
2929
output = read_content(source, url: url)
@@ -33,19 +33,19 @@ def read(source_url)
3333
output[:totals].each do |key, value|
3434
totals[key] += value
3535
end
36-
no_bioschema_urls += "\n - #{url}" if !source.nil? && output[:totals].values.sum.zero?
36+
no_bioschema_urls << url if !source.nil? && output[:totals].values.sum.zero?
3737
end
3838
end
3939

4040
if totals.keys.any?
41-
bioschemas_summary = "Bioschemas summary:\n"
41+
bioschemas_summary = ["Bioschemas summary:\n"]
4242
totals.each do |type, count|
43-
bioschemas_summary += "\n - #{type}: #{count}"
43+
bioschemas_summary << " - #{type}: #{count}"
4444
end
45-
@messages << bioschemas_summary
45+
@messages << bioschemas_summary.join("\n")
4646
end
4747

48-
@messages << no_bioschema_urls
48+
@messages << "Bioschemas not found in:\n\n#{no_bioschema_urls.map { |u| " - #{u}" }.join("\n")}" if no_bioschema_urls.any?
4949

5050
deduplicate(provider_events).each do |event_params|
5151
add_event(event_params)
@@ -113,13 +113,13 @@ def read_content(content, url: nil)
113113
error = 'A parsing error'
114114
comment = 'Please check your page contains valid JSON-LD or HTML.'
115115
end
116-
message = "#{error} occurred while reading"
116+
message = String.new("#{error} occurred while reading")
117117
if url.present? && url != 'https://example.com'
118-
message += ": #{url} "
118+
message << ": #{url} "
119119
else
120-
message += " the source"
120+
message << " the source"
121121
end
122-
message += ". #{comment}" if comment
122+
message << ". #{comment}" if comment
123123
@messages << message
124124
end
125125

lib/ingestors/concerns/sitemap_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def parse_txt_sitemap(url)
4141
end
4242

4343
def log_sitemap(ext, url, count)
44-
@messages << "Parsing .#{ext} sitemap: #{url}\n - #{count} URLs found"
44+
@messages << "Parsing .#{ext} sitemap: #{url}\n\n - #{count} URLs found"
4545
end
4646
end
4747
end

lib/ingestors/oai_pmh_ingestor.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ def read_oai_rdf(client)
9494
end
9595

9696
if totals.keys.any?
97-
bioschemas_summary = "Bioschemas summary:\n"
97+
bioschemas_summary = ["Bioschemas summary:\n"]
9898
totals.each do |type, count|
99-
bioschemas_summary += "\n - #{type}: #{count}"
99+
bioschemas_summary << " - #{type}: #{count}"
100100
end
101-
@messages << bioschemas_summary
101+
@messages << bioschemas_summary.join("\n")
102102
end
103103

104104
@bioschemas_manager.deduplicate(provider_events).each do |event_params|

lib/scraper.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def run
104104

105105
def scrape(source, user = get_user, index: 0)
106106
source_start = Time.now
107-
output = ''
107+
output = []
108108
log '', 1
109109
if source.enabled
110110
log t('scraper.messages.processing', source: source.content_provider&.title, num: index.to_s), 1
@@ -114,9 +114,9 @@ def scrape(source, user = get_user, index: 0)
114114
end
115115
if validate_source(source)
116116
log t('scraper.messages.valid_source'), 2
117-
output += "**Provider:** #{source.content_provider.title}\n\n"
118-
output += "<span style='url-wrap'>**URL:** #{source.url}</span>\n\n"
119-
output += "**Method:** #{source.ingestor_title}\n\n"
117+
output << "**Provider:** #{source.content_provider.title}"
118+
output << "<span style='url-wrap'>**URL:** #{source.url}</span>"
119+
output << "**Method:** #{source.ingestor_title}"
120120

121121
# get ingestor
122122
ingestor = Ingestors::IngestorFactory.get_ingestor(source.method)
@@ -127,16 +127,16 @@ def scrape(source, user = get_user, index: 0)
127127
# read records
128128
ingestor.read(source.url)
129129
unless ingestor.messages.blank?
130-
output += "\n## Reading\n\n"
131-
ingestor.messages.each { |m| output += "#{m}\n" }
130+
output << "\n## Reading"
131+
output += ingestor.messages
132132
ingestor.messages.clear
133133
end
134134

135135
# write resources
136136
ingestor.write(user, source.content_provider, source: source)
137137
unless ingestor.messages.blank?
138-
output += "\n## Writing\n\n"
139-
ingestor.messages.each { |m| output += "#{m}\n" }
138+
output << "\n## Writing"
139+
output += ingestor.messages
140140
ingestor.messages.clear
141141
end
142142

@@ -152,14 +152,14 @@ def scrape(source, user = get_user, index: 0)
152152
", rejected[#{source.resources_rejected}]", 2
153153
end
154154
rescue StandardError => e
155-
output += "\n**Failed:** #{e.message}\n\n"
155+
output << "\n**Failed:** #{e.message}"
156156
log "Ingestor: #{ingestor.class} failed with: #{e.message}\t#{e.backtrace[0]}", 2
157157
ensure
158158
source.finished_at = Time.now
159159
run_time = source.finished_at - source_start
160-
output += "\n**Finished at:** #{source.finished_at.strftime '%H:%M on %A, %d %B %Y (UTC)'}\n"
161-
output += "\n**Run time:** #{run_time.round(2)}s\n"
162-
source.log = output
160+
output << "\n**Finished at:** #{source.finished_at.strftime '%H:%M on %A, %d %B %Y (UTC)'}"
161+
output << "**Run time:** #{run_time.round(2)}s"
162+
source.log = output.join("\n\n")
163163
begin
164164
# only update enabled sources
165165
source.save! if source.enabled && !source.is_a?(Scraper::ConfigSource)

0 commit comments

Comments
 (0)