Skip to content

Instantly share code, notes, and snippets.

@iheanyi
Last active August 29, 2015 14:10
Show Gist options
  • Save iheanyi/2c5510e9c5ecb30f0133 to your computer and use it in GitHub Desktop.
Save iheanyi/2c5510e9c5ecb30f0133 to your computer and use it in GitHub Desktop.
Easy method of finding and determining corequisites, prerequisites, restrictions, and any other minor detail shown on the Notre Dame description page. If any of these details are returned as nil, then it does not have that specific attribute.
require 'nokogiri'
require 'faraday'
@term = "201420"
@crn = "14433"
@crn = "21129"
@crn = "22091"
@conn = Faraday.new(:url => 'https://class-search.nd.edu/reg/srch/ClassSearchServlet') do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.adapter Faraday.default_adapter
end
puts "Beginning the script."
response = @conn.get '', {:CRN => @crn, :TERM => @term}
content = response.body.strip
document = Nokogiri::HTML(content)
course_restriction_node = document.at('span:contains("Cannot Have Taken")')
#course_restriction_node = document.search "[text()*='Cannot Have Taken:']"
puts course_restriction_node.content.strip
#puts course_restriction_node.parent
#puts course_restriction_node.parent.next_element
course_restriction_text = course_restriction_node.next_sibling.next_sibling.content.strip
puts course_restriction_text
#fetch_restrictions(document)
def fetch_course_details
response = @conn.get '', {:CRN => @crn, :TERM => @term}
content = response.body.strip
document = Nokogiri::HTML(content)
fetch_course_description(document)
fetch_course_restrictions(document)
fetch_corequisites(document)
fetch_prerequisites(document)
end
def fetch_course_description(doc)
description_node = doc.at('span:contains("Course Description")')
desc_text = description_node.next_sibling.next_sibling.content.strip
puts desc_text
end
def fetch_course_restrictions(doc)
restriction_node = doc.at('span:contains("Cannot Have Taken")')
if restriction_node
restriction_text = restriction_node.next_sibling.next_sibling.content.strip
restriction_text.strip.split.join('').gsub(/\s+/, '')
restriction_array = restriction_text.split(",")
#puts restriction_array.length
restriction_array.each do |restriction|
course_num = restriction.gsub(/\s+/, '').gsub(/\W/, '').strip
puts course_num
end
end
# For restriction text, it should split on the comma because that's the delimiter for multiple values.
# After splitting or attempting to split on the comma, then the code should next call split
# on the space between the Dept & Course or remove the space to join the two parts together,
# so then the database/model can query for this object and create something new.
end
def fetch_corequisites(doc)
corequisites_node = doc.at('span:contains("Corequisites")')
if corequisites_node
corequisites_text = corequisites_node.next_sibling.next_sibling.content.strip
corequisites_array = corequisites_text.split(",")
corequisites_array.each do |node|
node_text = node.gsub!(/\s+/, '').strip
puts node_text.length
puts node_text
end
end
end
def fetch_prerequisites(doc)
prerequisites_node = doc.at('span:contains("Prerequisites")')
if prerequisites_node
prereq_text = prerequisites_node.next_element.next_sibling.content.strip
if prereq_text.include? "or"
join_word = "or"
elsif prereq_text.include? "and"
join_word = "and"
else
join_word = ''
end
prereq_array = prereq_text.split(",") || prereq_text.split("or") || prereq_text.split("and")
prereq_array.each do |node|
node_text = node.gsub!(/\s+/, '').gsub!(/\W/, '').strip
end
end
end
fetch_course_details
  • Get majors required
  • Get campus required?
  • Get degree required
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment