class OsuCcScraper::Course

Constants

SECTIONS_XPATH
TITLE_XPATH

Public Class Methods

from_json(json) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 18
def self.from_json(json)
  Course.new(*JSON.parse(json).values)
end

Public Instance Methods

sections() click to toggle source
# File lib/osu-cc-scraper/course.rb, line 9
def sections
  html = fetch_sections
  parse_sections(html)
end
to_json() click to toggle source
# File lib/osu-cc-scraper/course.rb, line 14
def to_json
  self.to_h.to_json
end

Private Instance Methods

fetch_column(document, selector) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 52
def fetch_column(document, selector)
  document.xpath(selector)&.text&.delete("\r\n")&.strip
end
fetch_sections() click to toggle source
# File lib/osu-cc-scraper/course.rb, line 27
def fetch_sections
  open("#{ENDPOINT}/CourseDetail.aspx?subjectcode=#{subject_code}&coursenumber=#{course_number}").read
end
parse_campus(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 72
def parse_campus(row)
  fetch_column(row, "td[position() = 9]")
end
parse_capacity(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 84
def parse_capacity(row)
  fetch_column(row, "td[position() = 13]")&.to_i
end
parse_crn(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 60
def parse_crn(row)
  fetch_column(row, "td[position() = 2]")
end
parse_current(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 88
def parse_current(row)
  fetch_column(row, "td[position() = 14]")&.to_i
end
parse_instructor(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 68
def parse_instructor(row)
  fetch_column(row, "td[position() = 6]")
end
parse_section(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 64
def parse_section(row)
  fetch_column(row, "td[position() = 3]")
end
parse_sections(html) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 31
def parse_sections(html)
  document = Oga.parse_html(html)

  document.xpath(SECTIONS_XPATH).map { |row|
    Section.new(
      subject_code,
      course_number,
      name,
      parse_term(row),
      parse_crn(row),
      parse_section(row),
      parse_instructor(row),
      parse_campus(row),
      parse_type(row),
      parse_status(row),
      parse_capacity(row),
      parse_current(row)
    )
  }
end
parse_status(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 80
def parse_status(row)
  fetch_column(row, "td[position() = 12]")
end
parse_term(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 56
def parse_term(row)
  fetch_column(row, "td[position() = 1]")
end
parse_type(row) click to toggle source
# File lib/osu-cc-scraper/course.rb, line 76
def parse_type(row)
  fetch_column(row, "td[position() = 11]")
end