Class: OnThisDay::Item
- Inherits:
-
Object
- Object
- OnThisDay::Item
- Defined in:
- lib/onthisday.rb
Instance Method Summary collapse
- #html ⇒ Object
-
#initialize(element) ⇒ Item
constructor
A new instance of Item.
-
#remove_noprint_elements! ⇒ Object
Remove any child nodes with class “nopront”.
- #set_and_remove_year! ⇒ Object
- #text ⇒ Object
-
#topics ⇒ Object
Rescursively search for all a elements in this element and return their value (removing /wiki/).
- #year ⇒ Object
Constructor Details
#initialize(element) ⇒ Item
Returns a new instance of Item.
8 9 10 11 12 13 |
# File 'lib/onthisday.rb', line 8 def initialize(element) @element = element @year = nil set_and_remove_year! end |
Instance Method Details
#html ⇒ Object
44 45 46 |
# File 'lib/onthisday.rb', line 44 def html @element.inner_html.gsub(' – ','') end |
#remove_noprint_elements! ⇒ Object
Remove any child nodes with class “nopront”. This removes the boilerplate Wikinews, Obituries etc.
17 18 19 20 21 |
# File 'lib/onthisday.rb', line 17 def @element.xpath('//*[starts-with(@class,"noprint")]').each do |node| node.children.remove end end |
#set_and_remove_year! ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/onthisday.rb', line 27 def set_and_remove_year! @element.xpath('./a').each do |node| text = node.content # if the title of the link looks like a year, e.g. "1879", set # the year of this item and remove the node text.match /\A\d{2,4}( BC)?\z/ do |m| @year = m.to_s node.remove end end end |
#text ⇒ Object
40 41 42 |
# File 'lib/onthisday.rb', line 40 def text @element.inner_text.gsub(' – ','') end |
#topics ⇒ Object
Rescursively search for all a elements in this element and return their value (removing /wiki/)
50 51 52 53 54 |
# File 'lib/onthisday.rb', line 50 def topics @element.xpath('.//a').map do |a| a.attr('href').gsub('/wiki/','') end end |
#year ⇒ Object
23 24 25 |
# File 'lib/onthisday.rb', line 23 def year @year end |