<commit_before>require "rest-client"
require "httpclient"
require Pathname(File.dirname(__FILE__)) + "rest"

module SFU

  class Course
    class << self
      def terms(sfuid)
        terms = REST.json REST.terms_url, "&username=#{sfuid}"
        if terms == 404 || terms.empty?
          404
        elsif terms == 500
          500
        else
          terms["teachingSemester"]
        end
      end

      def for_instructor(sfuid, term_code = nil)
        terms(sfuid).map do |term|
          if term_code.nil?
            courses = REST.json REST.courses_url, "&username=#{sfuid}&term=#{term["peopleSoftCode"]}"
            courses["teachingCourse"]
          else
            if term["peopleSoftCode"] == term_code
              courses = REST.json REST.courses_url, "&username=#{sfuid}&term=#{term["peopleSoftCode"]}"
              courses["teachingCourse"]
            end
          end
        end
      end

      def info(course, term)
        REST.json REST.course_info_url, "&course=#{course}&term=#{term}"
      end

      def associated_class_for_section(json_data, section_code)
        associated_class = nil
        json_data.each do |info|
          section = info["course"]["section"].downcase
          associated_class = info["course"]["associatedClass"] if section.eql?(section_code)
        end
        associated_class
      end

      def section_tutorials(course_code, term_code, section_code)
        details = info(course_code, term_code)
        sections = []
        has_no_child_sections = true
        section = nil

	      unless details == "[]"
          associated_class = associated_class_for_section(details, section_code)
          details.each do |info|
            section = info["course"]["section"].downcase
            classType = info["course"]["classType"]

	          if classType.eql?("n") && associated_class == info["course"]["associatedClass"]
              sections << info["course"]["section"]
              has_no_child_sections = false
            end
	        end
        end

        # Return main section e.g. d100 only for courses with no tutorial/lab sections
        sections << section_code.upcase if has_no_child_sections && !section.nil?

        sections
      end

      def title(course_code, term_code, section_code)
        details = info(course_code, term_code)
        title = nil
        if details == 500
          title = 500
        elsif details == 404
          title == 404
        elsif details != "[]"
          details.each do |info|
            section = info["course"]["section"].downcase
            title = info["course"]["title"] if section.eql? section_code.downcase
          end
        end<commit_msg>Only check/return 'child' sections for enrollment sections<commit_after>require "rest-client"
require "httpclient"
require Pathname(File.dirname(__FILE__)) + "rest"

module SFU

  class Course
    class << self
      def terms(sfuid)
        terms = REST.json REST.terms_url, "&username=#{sfuid}"
        if terms == 404 || terms.empty?
          404
        elsif terms == 500
          500
        else
          terms["teachingSemester"]
        end
      end

      def for_instructor(sfuid, term_code = nil)
        terms(sfuid).map do |term|
          if term_code.nil?
            courses = REST.json REST.courses_url, "&username=#{sfuid}&term=#{term["peopleSoftCode"]}"
            courses["teachingCourse"]
          else
            if term["peopleSoftCode"] == term_code
              courses = REST.json REST.courses_url, "&username=#{sfuid}&term=#{term["peopleSoftCode"]}"
              courses["teachingCourse"]
            end
          end
        end
      end

      def info(course, term)
        REST.json REST.course_info_url, "&course=#{course}&term=#{term}"
      end

      def sections_exists?(json_data, section_code)
        exists = false
        json_data.each do |info|
          section = info["course"]["section"].to_s.downcase
          exists = true if section.eql?(section_code)
        end
        exists
      end

      def is_enrollment_section?(json_data, section_code)
        enrollment_section = false
        json_data.each do |info|
          section = info["course"]["section"].to_s.downcase
          classType = info["course"]["classType"].to_s.downcase
          enrollment_section = true if section.eql?(section_code) && classType.eql?("e")
        end
        enrollment_section
      end

      def associated_class_for_section(json_data, section_code)
        associated_class = nil
        json_data.each do |info|
          section = info["course"]["section"].to_s.downcase
          associated_class = info["course"]["associatedClass"] if section.eql?(section_code)
        end
        associated_class
      end

      def section_tutorials(course_code, term_code, section_code)
        details = info(course_code, term_code)
        sections = []
        has_no_child_sections = true

	      if details != "[]" && is_enrollment_section?(details, section_code)
          associated_class = associated_class_for_section(details, section_code)
          details.each do |info|
            classType = info["course"]["classType"]

	          if classType.eql?("n") && associated_class == info["course"]["associatedClass"]
              sections << info["course"]["section"]
              has_no_child_sections = false
            end
	        end
        end

        # Return main section e.g. d100 only for courses with no tutorial/lab sections
        sections << section_code.upcase if has_no_child_sections && sections_exists?(details, section_code)

        sections
      end

      def title(course_code, term_code, section_code)
        details = info(course_code, term_code)
        title = nil
        if details == 500
          title = 500
        elsif details == 404
          title == 404
        elsif details != "[]"
          details.each do |info|
            section = info["course"]["section"].downcase
            title = info["course"]["title"] if section.eql? section_code.downcase
          end
        end
{'fraction_non_alphanumeric': 0.10606808090774544, 'fraction_numerical': 0.008880118401578688, 'mean_word_length': 2.2027382833070037, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5926454', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1707, 'n_words': 381}