Class: HackerNews::Comment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_id, node) ⇒ Comment

Returns a new instance of Comment.



7
8
9
10
11
12
13
14
15
16
# File 'lib/hacker_news/comment.rb', line 7

def initialize(item_id, node)
  @item_id = item_id
  @node = node
  @user = node.css("span.comhead a")[0].text
  @url = "http://news.ycombinator.com/" + node.css("span.comhead a")[1].attributes["href"].value
  @created_at = node.css("span.comhead")[0].children[1].text.strip[0..-4] rescue nil
  @html = node.css("span.comment font")[0].inner_html
  @level = node.css('img[src="http://ycombinator.com/images/s.gif"]')[0].attributes["width"].value.to_i
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/hacker_news/comment.rb', line 5

def children
  @children
end

#created_atObject

Returns the value of attribute created_at.



5
6
7
# File 'lib/hacker_news/comment.rb', line 5

def created_at
  @created_at
end

#htmlObject

Returns the value of attribute html.



5
6
7
# File 'lib/hacker_news/comment.rb', line 5

def html
  @html
end

#item_idObject

Returns the value of attribute item_id.



5
6
7
# File 'lib/hacker_news/comment.rb', line 5

def item_id
  @item_id
end

#levelObject

Returns the value of attribute level.



5
6
7
# File 'lib/hacker_news/comment.rb', line 5

def level
  @level
end

#nodeObject

Returns the value of attribute node.



5
6
7
# File 'lib/hacker_news/comment.rb', line 5

def node
  @node
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/hacker_news/comment.rb', line 5

def url
  @url
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/hacker_news/comment.rb', line 5

def user
  @user
end

Instance Method Details

#commentsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hacker_news/comment.rb', line 18

def comments
  chidren = []
  next_node = node.next
  return chidren unless next_node
  next_comment = next_node ? self.class.new(self.item_id, next_node) : nil
  while next_node && next_comment && self.root_of?(next_comment) && self.parent_of?(next_comment)
    children << next_comment
    
    next_node = next_node.next
    next_comment = next_node ? self.class.new(self.item_id, next_node) : nil
  end
  children
end

#parent_of?(other_comment = nil) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/hacker_news/comment.rb', line 36

def parent_of?(other_comment = nil)
  other_comment && self.level + 40 == other_comment.level
end

#root_of?(other_comment = nil) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/hacker_news/comment.rb', line 32

def root_of?(other_comment = nil)
  other_comment && self.level < other_comment.level
end