Class: HackerNews::Comment
- Inherits:
-
Object
- Object
- HackerNews::Comment
- Defined in:
- lib/hnruby/comment.rb
Overview
Represents a single comment from a CommentPage. Since its constructor is passed raw bits of html, this isn’t meant to be accessed directly, but rather via a CommentPage object:
page = HackerNews::StoryList[1].comment_page
comment = page[1] #=> <Comment> by user 1 day ago
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the actual content of the comment, as a string (with original html formatting).
-
#depth ⇒ Object
readonly
Returns the nesting depth of a comment— top-level comments are zero, replies to same are one, etc.
-
#id ⇒ Object
readonly
Returns the ID number of the comment.
-
#parent ⇒ Object
Returns the Comment object representing this parent’s comment, or
nil
if this is a top-level comment. -
#submitter ⇒ Object
readonly
Returns the username of the comment’s author.
-
#time ⇒ Object
readonly
Returns (as a human-readable string) how long ago the comment was submitted, as of this object’s creation.
Instance Method Summary collapse
-
#<=>(comment) ⇒ Object
Returns 1 if
self
is older thancomment
, and -1 ifcomment
is older thanself
. -
#child_of?(comment) ⇒ Boolean
Returns true if
self
is a child ofcomment
. -
#initialize(html_raw) ⇒ Comment
constructor
:stopdoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#parent_of?(comment) ⇒ Boolean
Returns true if
comment
is a child ofself
. -
#plaintext ⇒ Object
Returns the comment’s content, minus html formatting:.
Constructor Details
#initialize(html_raw) ⇒ Comment
:stopdoc:
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hnruby/comment.rb', line 38 def initialize(html_raw) html = html_raw / "td" @depth = html.at("img").attribute("width").content.to_i / 40 @id = (html / "a")[2].attribute("href").content.delete("item?id=").to_i @submitter = (html / "a")[1].content @submitter_url = (html / "a")[1].attribute("href").content @time = (html / "span")[1].content.split[1..-3].join(" ") @content = (html / "font")[-2].inner_html end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the actual content of the comment, as a string (with original html formatting).
comment.content #=> "This is a comment. It <i>even has formatting<i>!"
24 25 26 |
# File 'lib/hnruby/comment.rb', line 24 def content @content end |
#depth ⇒ Object (readonly)
Returns the nesting depth of a comment— top-level comments are zero, replies to same are one, etc.
14 15 16 |
# File 'lib/hnruby/comment.rb', line 14 def depth @depth end |
#id ⇒ Object (readonly)
Returns the ID number of the comment.
comment.id #=> 6630625
27 28 29 |
# File 'lib/hnruby/comment.rb', line 27 def id @id end |
#parent ⇒ Object
Returns the Comment object representing this parent’s comment, or nil
if this is a top-level comment.
commentpage[1].depth #=> 1
commentpage[1].parent #=> <Comment> by user 10 hours ago
commentpage[0].depth #=> 0
commentpage[0].parent #=> nil
35 36 37 |
# File 'lib/hnruby/comment.rb', line 35 def parent @parent end |
#submitter ⇒ Object (readonly)
Returns the username of the comment’s author.
16 17 18 |
# File 'lib/hnruby/comment.rb', line 16 def submitter @submitter end |
#time ⇒ Object (readonly)
Returns (as a human-readable string) how long ago the comment was submitted, as of this object’s creation.
comment.time #=> "5 minutes ago"
20 21 22 |
# File 'lib/hnruby/comment.rb', line 20 def time @time end |
Instance Method Details
#<=>(comment) ⇒ Object
Returns 1 if self
is older than comment
, and -1 if comment
is older than self
. Since comments can’t be exactly the same age, never returns 0.
If comment
isn’t actually a Comment, returns nil
.
74 75 76 |
# File 'lib/hnruby/comment.rb', line 74 def <=>(comment) #Since IDs are sequential, this sorts by time comment.respond_to?(:id) && comment.id <=> @id end |
#child_of?(comment) ⇒ Boolean
Returns true if self
is a child of comment
.
66 67 68 |
# File 'lib/hnruby/comment.rb', line 66 def child_of?(comment) @parent && comment.respond_to?(:id) && @parent.id == comment.id end |
#inspect ⇒ Object
:nodoc:
79 80 81 |
# File 'lib/hnruby/comment.rb', line 79 def inspect "<Comment> by #{@submitter}, #{@time}" end |
#parent_of?(comment) ⇒ Boolean
Returns true if comment
is a child of self
.
59 60 61 62 63 |
# File 'lib/hnruby/comment.rb', line 59 def parent_of?(comment) if comment.respond_to?(:parent) comment.parent && comment.parent.id == @id end end |
#plaintext ⇒ Object
Returns the comment’s content, minus html formatting:
comment.content #=> "This is a comment <i>with html formatting</i>."
comment.plaintext #=> "This is a comment with html formatting."
54 55 56 |
# File 'lib/hnruby/comment.rb', line 54 def plaintext Nokogiri::HTML(@content).content end |