Class: HackerNews::CommentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/hn/parsers/comment_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(entry_id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hn/parsers/comment_parser.rb', line 7

def parse(entry_id)
  doc = Nokogiri::HTML(open("https://news.ycombinator.com/item?id=#{entry_id}"))
  comment_data = doc.css('td.default').map { |td| extract_comment_from_td(td) }
  comments = []

  comment_data.each_with_index do |c, index|
    if c.level == 0
      comments << c
    else
      parent = comment_data[0...index].select { |cm| cm.level == c.level - 1 }.last
      parent.children << c
    end
  end

  comments
end