Class: Kuport::Materials
- Inherits:
-
Object
- Object
- Kuport::Materials
- Defined in:
- lib/kuport/materials.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#trs ⇒ Object
readonly
Returns the value of attribute trs.
Instance Method Summary collapse
-
#initialize(page) ⇒ Materials
constructor
A new instance of Materials.
- #materials ⇒ Object
- #parse_link(link) ⇒ Object
- #parse_table_line(tr) ⇒ Object
- #to_h ⇒ Object
- #to_json(*a) ⇒ Object
- #to_s ⇒ Object
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_url ⇒ Object (readonly)
Returns the value of attribute base_url.
3 4 5 |
# File 'lib/kuport/materials.rb', line 3 def base_url @base_url end |
#trs ⇒ Object (readonly)
Returns the value of attribute trs.
3 4 5 |
# File 'lib/kuport/materials.rb', line 3 def trs @trs end |
Instance Method Details
#materials ⇒ Object
11 12 13 |
# File 'lib/kuport/materials.rb', line 11 def materials @materials ||= trs.map{|tr| parse_table_line(tr)}.freeze end |
#parse_link(link) ⇒ Object
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_h ⇒ Object
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_s ⇒ Object
34 35 36 |
# File 'lib/kuport/materials.rb', line 34 def to_s @materials_s ||= materials.to_s end |