Class: HackerNews::Comment

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#contentObject (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

#depthObject (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

#idObject (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

#parentObject

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

#submitterObject (readonly)

Returns the username of the comment’s author.



16
17
18
# File 'lib/hnruby/comment.rb', line 16

def submitter
  @submitter
end

#timeObject (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



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hnruby/comment.rb', line 73

def <=>(comment)
  left, right = [self, comment].map do |i|
    i.time.split[0].to_i * {
        "minute" => 60,    "minutes" => 60,
        "hour"   => 3600,  "hours"   => 3600,
        "day"    => 86400, "days"    => 86400
    }[i.time.split[1]]
  end

  -(right <=> left)
end

#child_of?(comment) ⇒ Boolean

Returns true if self is a child of comment.

Returns:

  • (Boolean)


64
65
66
# File 'lib/hnruby/comment.rb', line 64

def child_of?(comment)
  @parent && @parent.id == comment.id
end

#inspectObject

:nodoc:



69
70
71
# File 'lib/hnruby/comment.rb', line 69

def inspect
  "<Comment> by #{@submitter} at #{@time}"
end

#parent_of?(comment) ⇒ Boolean

Returns true if comment is a child of self.

Returns:

  • (Boolean)


59
60
61
# File 'lib/hnruby/comment.rb', line 59

def parent_of?(comment)
  comment.parent && comment.parent.id == @id
end

#plaintextObject

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