Skip to content

Commit b9baf3d

Browse files
committed
fix(github-ingestor): when another url was in description
1 parent 583618a commit b9baf3d

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

lib/ingestors/github_ingestor.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ def cache_fetch(key, url)
101101

102102
# Sets material hash keys and values and add them to material
103103
def to_material(repo_data) # rubocop:disable Metrics/AbcSize
104-
homepage_nil_or_empty = nil_or_empty? repo_data['homepage']
105-
url = homepage_nil_or_empty ? repo_data['html_url'] : repo_data['homepage']
104+
github_io_homepage = github_io_homepage? repo_data['homepage']
105+
url = github_io_homepage ? repo_data['homepage'] : repo_data['html_url']
106106
redirected_url = get_redirected_url(url)
107-
doc = fetch_homepage_doc(redirected_url)
107+
html = get_html(redirected_url)
108108

109109
material = OpenStruct.new
110110
material.title = repo_data['name'].titleize
111111
material.url = url
112-
material.description = homepage_nil_or_empty ? repo_data['description'] : fetch_definition(doc, redirected_url)
112+
material.description = github_io_homepage ? fetch_definition(html, redirected_url) : repo_data['description']
113113
material.keywords = repo_data['topics']
114114
material.licence = fetch_licence(repo_data['license'])
115115
material.status = repo_data['archived'] ? 'Archived' : 'Active'
@@ -119,27 +119,30 @@ def to_material(repo_data) # rubocop:disable Metrics/AbcSize
119119
material.date_published = repo_data['pushed_at']
120120
material.date_modified = repo_data['updated_at']
121121
material.contributors = fetch_contributors(repo_data['contributors_url'], repo_data['full_name'])
122-
material.resource_type = homepage_nil_or_empty ? ['Github Repository'] : ['Github Page']
123-
material.prerequisites = fetch_prerequisites(doc)
122+
material.resource_type = github_io_homepage ? ['Github Page'] : ['Github Repository']
123+
material.prerequisites = fetch_prerequisites(html)
124124
material
125125
end
126126

127-
def nil_or_empty?(repo_data)
128-
repo_data.nil? || repo_data.empty?
127+
def github_io_homepage?(homepage)
128+
return false if homepage.nil? || homepage.empty?
129+
130+
url = URI(homepage)
131+
url.host&.downcase&.end_with?('.github.io')
129132
end
130133

131-
def fetch_homepage_doc(url)
134+
def get_html(url)
132135
response = HTTParty.get(url, follow_redirects: true, headers: { 'User-Agent' => config[:user_agent] })
133136
Nokogiri::HTML(response.body)
134137
end
135138

136139
# DEFINITION – Opens the GitHub homepage, fetches the 3 first >50 char <p> tags'text
137140
# and joins them with a 'Read more...' link at the end of the description
138141
# Some of the first <p> tags were not descriptive, thus skipping them
139-
def fetch_definition(doc, url)
142+
def fetch_definition(html, url)
140143
desc = ''
141144
round = 3
142-
doc.css('p').each do |p|
145+
html.css('p').each do |p|
143146
p_txt = p&.text&.strip&.gsub(/\s+/, ' ') || ''
144147
next if p_txt.length < 50 || round.zero?
145148

@@ -189,20 +192,20 @@ def fetch_contributors(contributors_url, full_name)
189192
end
190193

191194
# PREREQUISITES – From the homepage HTML, looks for <p> tags which are children of ...
192-
def fetch_prerequisites(doc)
195+
def fetch_prerequisites(html)
193196
prereq_paragraphs = []
194197

195198
# ... any heading tag (h1–h6) or span tag with text "prereq" (EN) or "prerreq" (ES)
196-
prereq_paragraphs = fetch_prerequisites_from_h(doc, prereq_paragraphs)
199+
prereq_paragraphs = fetch_prerequisites_from_h(html, prereq_paragraphs)
197200

198201
# ... any tag with id containing "prereq" (EN) or "prerreq" (ES)
199-
prereq_paragraphs = fetch_prerequisites_from_id_or_class(doc, prereq_paragraphs) if prereq_paragraphs.empty?
202+
prereq_paragraphs = fetch_prerequisites_from_id_or_class(html, prereq_paragraphs) if prereq_paragraphs.empty?
200203

201204
prereq_paragraphs&.join("\n")&.gsub(/\n\n+/, "\n")&.strip || ''
202205
end
203206

204-
def fetch_prerequisites_from_h(doc, prereq_paragraphs)
205-
doc.xpath('//h1|//h2|//h3|//h4|//h5|//h6|//span').each do |h|
207+
def fetch_prerequisites_from_h(html, prereq_paragraphs)
208+
html.xpath('//h1|//h2|//h3|//h4|//h5|//h6|//span').each do |h|
206209
next unless h.text =~ /prereq|prerreq/i # if prereq in text
207210

208211
paragraph = h.xpath('following-sibling::*')
@@ -212,8 +215,8 @@ def fetch_prerequisites_from_h(doc, prereq_paragraphs)
212215
prereq_paragraphs
213216
end
214217

215-
def fetch_prerequisites_from_id_or_class(doc, prereq_paragraphs)
216-
doc.xpath('//*[@id]').each do |node|
218+
def fetch_prerequisites_from_id_or_class(html, prereq_paragraphs)
219+
html.xpath('//*[@id]').each do |node|
217220
next unless prereq_node?(node)
218221

219222
extract_following_paragraphs(node, prereq_paragraphs)

0 commit comments

Comments
 (0)