Class: Kuport::Timetable

Inherits:
Object
  • Object
show all
Defined in:
lib/kuport/timetable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Timetable

Returns a new instance of Timetable.



5
6
7
8
9
10
# File 'lib/kuport/timetable.rb', line 5

def initialize(doc)
  period_line = doc.at_css('.portlet_module > .ac > table').css('tr')
  parse_dates(period_line.shift)
  parse_special(period_line.pop)
  parse_table(period_line)
end

Instance Attribute Details

#datesObject (readonly)

Returns the value of attribute dates.



3
4
5
# File 'lib/kuport/timetable.rb', line 3

def dates
  @dates
end

#specialObject (readonly)

Returns the value of attribute special.



3
4
5
# File 'lib/kuport/timetable.rb', line 3

def special
  @special
end

#tableObject (readonly)

Returns the value of attribute table.



3
4
5
# File 'lib/kuport/timetable.rb', line 3

def table
  @table
end

#yearObject (readonly)

Returns the value of attribute year.



3
4
5
# File 'lib/kuport/timetable.rb', line 3

def year
  @year
end

Instance Method Details

#compactObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kuport/timetable.rb', line 59

def compact
  # 土曜日から連続して授業の無い日を消す
  table.reverse_each do |key, val|
    break unless val.all?{|elem| elem[:name].nil?}
    table.delete(key)
  end

  # 週を通してn時限目が無ければ消す
  6.downto(0).each do |i|
    break unless table.all?{|key, val| val[i][:name].nil?}
    table.each{|key, val| val.pop}
  end
end

#parse_class_text(text) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/kuport/timetable.rb', line 48

def parse_class_text(text)
  text.strip!
  text = '' if text == '-'
  text = text.to_half_str

  name,room,period = text.split("\n")
  name.sub!(/\[(.+)\]/, '\1') if name
  period.sub!(/\((\w+)\)/, '\1') if period
  [name,room,period]
end

#parse_dates(dates_doc) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kuport/timetable.rb', line 18

def parse_dates(dates_doc)
  dates_doc.br_to_return
  tds = dates_doc.css('td')
  @year = tds.shift.text

  @dates = tds.map do |td|
    # 月日曜, 祝
    date,special = td.text.sub("\n", ' ').split("\n")
    {date: date, special: special}
  end
end

#parse_special(special_doc) ⇒ Object

集中講義(1週間共通)



13
14
15
16
# File 'lib/kuport/timetable.rb', line 13

def parse_special(special_doc)
  @special = special_doc.css('td')[1].text
  @special = '' if @special == '-'
end

#parse_table(period_line) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kuport/timetable.rb', line 30

def parse_table(period_line)
  @table = {mon: [], tue: [], wed: [], thurs: [], fri: [], sat: []}

  period_line.each do |tr|
    tds = tr.css('td')
    tds.shift # 横枠破棄(1~7時限 集中講義等)

    tds.zip(@table).each do |(td, day)| # 各曜日のn限
      td.br_to_return
      name,room,period = parse_class_text(td.text)

      # 休講とか (kyuko)
      status = td.css('img').map{|img| File.basename_noext(img['src'])}
      day[1] << {name: name, room: room, period: period, status: status}
    end
  end
end

#to_hObject



73
74
75
76
77
78
79
80
# File 'lib/kuport/timetable.rb', line 73

def to_h
  @data_hash ||= {
    year: year,
    dates: dates,
    table: table,
    special: special,
  }
end

#to_json(*a) ⇒ Object



86
87
88
# File 'lib/kuport/timetable.rb', line 86

def to_json(*a)
  @data_json ||= to_h.to_json(*a)
end

#to_sObject



82
83
84
# File 'lib/kuport/timetable.rb', line 82

def to_s
  @data_str = to_h.to_s
end