Class: Tableau::ModuleParser

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

Constant Summary collapse

@@MODULE_ID_REGEX =
/^CE[\d]{5}-[1-8]/

Instance Attribute Summary collapse

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(module_code = nil) ⇒ ModuleParser

Create a new ModuleParser, with an optional module code



12
13
14
15
16
17
18
19
# File 'lib/tableau/moduleparser.rb', line 12

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

Instance Attribute Details

#raw_timetableObject (readonly)

Returns the value of attribute raw_timetable.



9
10
11
# File 'lib/tableau/moduleparser.rb', line 9

def raw_timetable
  @raw_timetable
end

Instance Method Details

#module_infoObject



21
22
23
24
25
26
# File 'lib/tableau/moduleparser.rb', line 21

def module_info
  mod, types = parse, Set.new
  mod.classes.each { |c| types.add?(c.type) }

  return { name: mod.name, code: mod.module_id, types: types }
end

#parseObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tableau/moduleparser.rb', line 28

def parse
  raise "No module timetable loaded!" unless @raw_timetable

  #Get the ID and Name from the first <table>
  raw_info    = @raw_timetable.xpath(@@COURSE_DESCRIPTION_XPATH).to_html
  module_id   = @@MODULE_ID_REGEX.match(raw_info).to_s
  module_name = raw_info.gsub(module_id, '')

  mod = Tableau::Module.new(module_id, name: module_name)
  table_count = 1
  table_data = @raw_timetable.xpath(xpath_for_table(table_count))

  # Iterate through each timetable until xpath returns no more timetable tables
  while !table_data.empty?
    tables_classes = parse_table(table_data)
    tables_classes.each { |c| mod.classes << c }
    table_data = @raw_timetable.xpath(xpath_for_table(table_count += 1))
  end

  mod #return the module to the caller
end