Class: HackerNews::Comment
- Inherits:
-
Object
- Object
- HackerNews::Comment
- Defined in:
- lib/hacker_news/comment.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#html ⇒ Object
Returns the value of attribute html.
-
#item_id ⇒ Object
Returns the value of attribute item_id.
-
#level ⇒ Object
Returns the value of attribute level.
-
#node ⇒ Object
Returns the value of attribute node.
-
#url ⇒ Object
Returns the value of attribute url.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #comments ⇒ Object
-
#initialize(item_id, node) ⇒ Comment
constructor
A new instance of Comment.
- #parent_of?(other_comment = nil) ⇒ Boolean
- #root_of?(other_comment = nil) ⇒ Boolean
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
#children ⇒ Object
Returns the value of attribute children.
5 6 7 |
# File 'lib/hacker_news/comment.rb', line 5 def children @children end |
#created_at ⇒ Object
Returns the value of attribute created_at.
5 6 7 |
# File 'lib/hacker_news/comment.rb', line 5 def created_at @created_at end |
#html ⇒ Object
Returns the value of attribute html.
5 6 7 |
# File 'lib/hacker_news/comment.rb', line 5 def html @html end |
#item_id ⇒ Object
Returns the value of attribute item_id.
5 6 7 |
# File 'lib/hacker_news/comment.rb', line 5 def item_id @item_id end |
#level ⇒ Object
Returns the value of attribute level.
5 6 7 |
# File 'lib/hacker_news/comment.rb', line 5 def level @level end |
#node ⇒ Object
Returns the value of attribute node.
5 6 7 |
# File 'lib/hacker_news/comment.rb', line 5 def node @node end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/hacker_news/comment.rb', line 5 def url @url end |
#user ⇒ Object
Returns the value of attribute user.
5 6 7 |
# File 'lib/hacker_news/comment.rb', line 5 def user @user end |
Instance Method Details
#comments ⇒ Object
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
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
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 |