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.
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_url ⇒ Object (readonly)
Returns the value of attribute base_url.
4 5 6 |
# File 'lib/kuport/materials.rb', line 4 def base_url @base_url end |
#trs ⇒ Object (readonly)
Returns the value of attribute trs.
4 5 6 |
# File 'lib/kuport/materials.rb', line 4 def trs @trs end |
Instance Method Details
#materials ⇒ Object
12 13 14 |
# File 'lib/kuport/materials.rb', line 12 def materials @materials ||= trs.map{|tr| parse_table_line(tr)}.freeze end |
#parse_link(link) ⇒ Object
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_h ⇒ Object
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_s ⇒ Object
35 36 37 |
# File 'lib/kuport/materials.rb', line 35 def to_s @materials_s ||= materials.to_s end |