seeds.rb 3.61 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1 2
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
3

Zac Wood's avatar
Zac Wood committed
4
require_relative 'patriot_web_parser'
5
require_relative 'courses_loader'
Zac Wood's avatar
Zac Wood committed
6 7 8 9 10
require 'thwait'
require 'httparty'
require 'nokogiri'
require 'json'

11 12
def parse_courses(subjects)
  courses = []
Zac Wood's avatar
Zac Wood committed
13

14 15 16 17 18 19 20
  threads = subjects.map do |subject|
    Thread.new do
      courses.push(*get_courses(subject.downcase))
    end
  end

  ThreadsWait.all_waits(*threads)
Zac Wood's avatar
Zac Wood committed
21

22 23
  courses
end
Zac Wood's avatar
Zac Wood committed
24

25 26 27 28 29 30 31 32 33
def load_courses(courses, semester)
  courses.each do |course|
    Course.create!(subject: course[:subject],
                   title: course[:title],
                   course_number: course[:course_number],
                   credits: course[:credits],
                   description: course[:description],
                   semester: semester)
  end
Zac Wood's avatar
Zac Wood committed
34 35
end

36 37 38 39 40 41 42
def parse_sections(subjects)
  parser = PatriotWeb::Parser.new
  sections_in = {}

  threads = subjects.map do |subject|
    Thread.new do
      sections_in[subject] = parser.parse_courses_in_subject(subject)
43 44 45
    end
  end

46
  ThreadsWait.all_waits(*threads)
47

48 49
  sections_in
end
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
def load_sections(sections_in, semester)
  sections_in.each do |subject, sections|
    all_sections = []

    sections.each do |section|
      if section.nil? || !section.key?(:subj) || !section.key?(:course_number)
        puts "#{subject} failed section: #{section.class}"
        next
      end

      course = Course.find_or_create_by!(subject: section[:subj],
                                         course_number: section[:course_number],
                                         semester: semester)

      instructor = Instructor.find_or_create_by!(name: section[:instructor])

      section_name = "#{section[:subj]} #{section[:course_number]} #{section[:section]}"

      all_sections.push(name: section_name,
                        crn: section[:crn],
                        section_type: section[:type],
                        title: section[:title],
                        start_date: section[:start_date],
                        end_date: section[:end_date],
                        days: section[:days],
                        start_time: section[:start_time],
                        end_time: section[:end_time],
                        location: section[:location],
                        course: course,
                        instructor: instructor)
81
    end
Zac Wood's avatar
Zac Wood committed
82

83
    CourseSection.create!(all_sections)
Zac Wood's avatar
Zac Wood committed
84
  end
85 86 87 88 89 90 91 92 93
end

def wipe_db
  Closure.delete_all
  CourseSection.delete_all
  Course.delete_all
  Semester.delete_all
end

Zac Wood's avatar
oops  
Zac Wood committed
94
def load_closures(semester)
95 96 97 98 99 100 101 102 103 104 105 106
  # create closures for the days there will be no classes
  # see: https://registrar.gmu.edu/calendars/fall-2018/
  Closure.create! date: Date.new(2018, 9, 3), semester: semester
  Closure.create! date: Date.new(2018, 10, 8), semester: semester
  (21..25).each { |n| Closure.create! date: Date.new(2018, 11, n), semester: semester }
  (10..19).each { |n| Closure.create! date: Date.new(2018, 12, n), semester: semester }
end

def main
  wipe_db

  parser = PatriotWeb::Parser.new
Zac Wood's avatar
oops  
Zac Wood committed
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  puts "Parsing subjects..."
  semester = parser.parse_semesters.first
  subjects = parser.parse_subjects(semester)

  puts "Parsing courses from catalog.gmu.edu..."
  courses = parse_courses(subjects)

  db_semester = Semester.create! season: 'Fall', year: 2018

  puts "Loading courses..."
  load_courses(courses, db_semester)

  puts "Parsing sections from Patriot Web..."
  sections_in = parse_sections(subjects)

  puts "Loading sections..."
  load_sections(sections_in, db_semester)
Zac Wood's avatar
Zac Wood committed
125

Zac Wood's avatar
oops  
Zac Wood committed
126
  load_closures(db_semester)
Zac Wood's avatar
Zac Wood committed
127
end
Zac Wood's avatar
Zac Wood committed
128

129
main