<commit_before>require 'yaml'
require_relative 'downloader'
require_relative 'parser'

module Timetable
  class Calendar
    attr_reader :course, :yoe

    def initialize(course, yoe)
      @config = YAML.load_file("config/timetable.yml")

      validate_arguments(course, yoe)

      @course = course
      @yoe = yoe.to_i
      @course_year = course_year
      @course_id = course_id

      process_all
    end

    def to_ical
      return "An error has occurred" if @cal.nil?
      @cal.to_ical
    end

  private

    # Checks that the parameters provided by the user are valid,
    # i.e. the course name exists and the yoe is within range
    def validate_arguments(course, yoe_text)
      unless @config['courses'].has_key?(course)
        raise ArgumentError, %Q{Invalid course name "#{course}"}
      end

      yoe = yoe_text.to_i
      unless valid_years.include?(yoe)
        raise ArgumentError, %Q{Invalid year of entry "#{yoe_text}"}
      end
    end

    # Downloads and parses all the necessary files
    def process_all
      @config['seasons'].each do |season|
        @config['week_ranges'].each do |weeks|
          download(season, weeks)
          parse
        end
      end
    end

    def download(season, weeks)<commit_msg>Return nil instead of an error message on failure<commit_after>require 'yaml'
require_relative 'downloader'
require_relative 'parser'

module Timetable
  class Calendar
    attr_reader :course, :yoe

    def initialize(course, yoe)
      @config = YAML.load_file("config/timetable.yml")

      validate_arguments(course, yoe)

      @course = course
      @yoe = yoe.to_i
      @course_year = course_year
      @course_id = course_id

      process_all
    end

    def to_ical
      @cal.to_ical unless @cal.nil?
    end

  private

    # Checks that the parameters provided by the user are valid,
    # i.e. the course name exists and the yoe is within range
    def validate_arguments(course, yoe_text)
      unless @config['courses'].has_key?(course)
        raise ArgumentError, %Q{Invalid course name "#{course}"}
      end

      yoe = yoe_text.to_i
      unless valid_years.include?(yoe)
        raise ArgumentError, %Q{Invalid year of entry "#{yoe_text}"}
      end
    end

    # Downloads and parses all the necessary files
    def process_all
      @config['seasons'].each do |season|
        @config['week_ranges'].each do |weeks|
          download(season, weeks)
          parse
        end
      end
    end

    def download(season, weeks)
{'fraction_non_alphanumeric': 0.08449581506576326, 'fraction_numerical': 0.0, 'mean_word_length': 2.61671469740634, 'pattern_counts': {'":': 0, '<': 3, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '569725', 'n_tokens_mistral': 788, 'n_tokens_neox': 756, 'n_words': 226}