Class: Tableau::TimetableParser

Inherits:
BaseParser show all
Defined in:
lib/tableau/timetableparser.rb

Constant Summary collapse

@@TIMETABLE_CODE_REGEX =
/^[A-Za-z0-9\(\)]+/

Instance Method Summary collapse

Methods inherited from BaseParser

#create_class, #create_class_weeks, #get_info, #inc_time, #initalize, #parse_table, #xpath_for_table

Constructor Details

#initialize(student_set_id = nil) ⇒ TimetableParser

Create a new TimetableParser, with a Student Set ID (aka Core Timetable)



9
10
11
12
13
14
15
16
# File 'lib/tableau/timetableparser.rb', line 9

def initialize(student_set_id = nil)
  begin
    timetable_response = Tableau::UriBuilder.new(student_set_id).read
    @raw_timetable = Nokogiri::HTML(timetable_response) if timetable_response
  rescue OpenURI::HTTPError
    return nil
  end
end

Instance Method Details

#parseObject

Parse the Timetable for all Modules within Returns: A Tableau::Timetable



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tableau/timetableparser.rb', line 24

def parse
  raise "No Timetable loaded!" unless @raw_timetable

  table_info = @@TIMETABLE_CODE_REGEX.match(get_info(@raw_timetable))
  timetable = Tableau::Timetable.new(table_info)

  table_count = 1
  table_data = @raw_timetable.xpath(xpath_for_table(table_count))

  while !table_data.empty?
    table_classes = parse_table(table_data)
    sort_classes(timetable, table_classes)
    table_data = @raw_timetable.xpath(xpath_for_table(table_count += 1))
  end
  timetable
end

#sort_classes(timetable, classes) ⇒ Object

Sort all the parsed classes into modules



42
43
44
45
46
47
48
49
50
51
# File 'lib/tableau/timetableparser.rb', line 42

def sort_classes(timetable, classes)
  classes.each do |c|
    if !(cmodule = timetable.module_for_code(c.code))
      cmodule = Tableau::Module.new(c.code)
      timetable.push_module(cmodule)
    end

    cmodule.add_class(c)
  end
end

#timetable_infoObject

Get a summary of information about this timetable



19
20
# File 'lib/tableau/timetableparser.rb', line 19

def timetable_info
end