Class: HackerNews::CommentPage

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hnruby/comment.rb

Overview

Represents the list of comments pertaining to a particluar HN story.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ CommentPage

Returns a new CommentPage corresponding to url.

cpage = HackerNews::CommentPage.new\
"https://news.ycombinator.com/item?id=6621679"  #=> "C--" <12 Comments>


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hnruby/comment.rb', line 96

def initialize(url)
  html = Nokogiri::HTML open(url)
  @title = html.title.chomp " | Hacker News"
  @url = url

  html = (html / "tr")[8..-3]
  @comments = []
  lineage = []
  (0...html.length).select{ |i| i.even? }.each do |i|
    com = Comment.new html[i]
    com.parent = com.depth > 0 ? lineage[com.depth - 1] : nil
    lineage[com.depth] = com
    @comments << com
  end
end

Instance Attribute Details

#titleObject (readonly)

Represents the title of



90
91
92
# File 'lib/hnruby/comment.rb', line 90

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



91
92
93
# File 'lib/hnruby/comment.rb', line 91

def url
  @url
end

Instance Method Details

#[](index) ⇒ Object

Returns the Comment object at index.



124
125
126
# File 'lib/hnruby/comment.rb', line 124

def [](index)
  @comments[index]
end

#at(index) ⇒ Object

Returns the Comment object at index. Equivalent to self[index].



130
131
132
# File 'lib/hnruby/comment.rb', line 130

def at(index)
  @comments[index]
end

#eachObject

Passes all comments in self as a block



113
114
115
116
# File 'lib/hnruby/comment.rb', line 113

def each
  @comments.each { |i| yield i }
  self
end

#inspectObject

:nodoc:



135
136
137
# File 'lib/hnruby/comment.rb', line 135

def inspect
  "\"#{@title}\" <#{@comments.length} comments>"
end

#to_aObject

Returns array containing every Comment object within the page.



119
120
121
# File 'lib/hnruby/comment.rb', line 119

def to_a
  @comments
end

#to_sObject

:nodoc:



140
141
142
# File 'lib/hnruby/comment.rb', line 140

def to_s
  inspect
end