Class: OnThisDay::Item

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

Instance Method Summary collapse

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
  remove_noprint_elements!
  set_and_remove_year!
end

Instance Method Details

#htmlObject



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 remove_noprint_elements!
  @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

#textObject



40
41
42
# File 'lib/onthisday.rb', line 40

def text
  @element.inner_text.gsub('','')
end

#topicsObject

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

#yearObject



23
24
25
# File 'lib/onthisday.rb', line 23

def year
  @year
end