Class: Kuport::Materials

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Materials

Returns a new instance of Materials.



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

def initialize(page)
  @trs = page.at_css('.portlet_module > table').css('tr')
  trs.shift
  @base_url = page.uri
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



4
5
6
# File 'lib/kuport/materials.rb', line 4

def base_url
  @base_url
end

#trsObject (readonly)

Returns the value of attribute trs.



4
5
6
# File 'lib/kuport/materials.rb', line 4

def trs
  @trs
end

Instance Method Details

#materialsObject



12
13
14
# File 'lib/kuport/materials.rb', line 12

def materials
  @materials ||= trs.map{|tr| parse_table_line(tr)}.freeze
end


26
27
28
29
# File 'lib/kuport/materials.rb', line 26

def parse_link(link)
  {name: Kuport.escape_filename(link.text),
   path: Kuport.to_abs_url(base_url, link.at_css('a')[:href]),}.freeze
end

#parse_table_line(tr) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/kuport/materials.rb', line 16

def parse_table_line(tr)
  tds = tr.css('td')
  {subject: tds[1].text,
   teacher: tds[2].text,
   title:   tds[3].text,
   period:  tds[4].text,
   state:   tds[5].text,
   links:   tds[6].css('li').map{|li| parse_link(li)},}.freeze
end

#to_hObject



31
32
33
# File 'lib/kuport/materials.rb', line 31

def to_h
  materials
end

#to_json(*a) ⇒ Object



39
40
41
# File 'lib/kuport/materials.rb', line 39

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

#to_sObject



35
36
37
# File 'lib/kuport/materials.rb', line 35

def to_s
  @materials_s ||= materials.to_s
end