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.



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

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.



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

def base_url
  @base_url
end

#trsObject (readonly)

Returns the value of attribute trs.



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

def trs
  @trs
end

Instance Method Details

#materialsObject



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

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


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

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



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

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,
   downloaded: (tds[5].text == 'ダウンロード済み'),
   links:   tds[6].css('li').map{|li| parse_link(li)},}.freeze
end

#to_hObject



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

def to_h
  materials
end

#to_json(*a) ⇒ Object



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

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

#to_sObject



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

def to_s
  @materials_s ||= materials.to_s
end