Class: HackerNews::NewsItem

Inherits:
Object
  • Object
show all
Defined in:
lib/hnruby/newsitem.rb

Overview

Represents a story on HN— access more or less all information about it

Since this class’s constructor takes bits of raw html, it’s not meant to be accessed except via the HackerNews::StoryList module:

news_item = HackerNews::StoryList[1]\
#=> "Profitless Prosperity" <article: 6622394>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(article_raw, meta_raw) ⇒ NewsItem

:stopdoc:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hnruby/newsitem.rb', line 35

def initialize(article_raw, meta_raw)
	article = article_raw / "td"
	meta = meta_raw / "td"

	@number = article.first.content.to_i
	@url = article[2].at("a").attributes["href"].value
	@url = HN_URL + @url if url.start_with? "item?id="
	@title = article[2].children.first.content

	if meta.children.length == 5
		#This is a normal article
		@category = :article

		@points = meta[1].children.first.content.to_i
		@submitter = meta[1].children[2].content
		@time = meta[1].children[3].content.delete("|").strip
		@comment_url = meta[1].children[4].attributes["href"].value
		@comment_page = nil #Don't needlessly download until asked
		@comments = meta[1].children[4].content.to_i
	else
		#This, by contrast, is a job offer, which lacks all meta-info aside
		#from submission time
		@category = :joboffer

		@time = meta[1].content
		@points, @submitter, @comment_url, @comment_page, @comments = nil
	end
end

Instance Attribute Details

#commentsObject (readonly)

Returns the number of comments the story currently has. If self is a job offer, returns nil.



32
33
34
# File 'lib/hnruby/newsitem.rb', line 32

def comments
  @comments
end

#numberObject (readonly)

Returns self‘s rank on Hacker News at the time self was created.



14
15
16
# File 'lib/hnruby/newsitem.rb', line 14

def number
  @number
end

#pointsObject (readonly)

Returns the number of points the story has, or had at the time self was created.

If self is a job offer, returns nil.



23
24
25
# File 'lib/hnruby/newsitem.rb', line 23

def points
  @points
end

#submitterObject (readonly)

Returns the username of the story’s submitter. If self is a job offer, returns nil.



26
27
28
# File 'lib/hnruby/newsitem.rb', line 26

def submitter
  @submitter
end

#timeObject (readonly)

Returns the (as a string) how long ago the comment was submitted.

nitem.time	#=> "5 minutes ago"


29
30
31
# File 'lib/hnruby/newsitem.rb', line 29

def time
  @time
end

#titleObject (readonly)

Returns the story’s title.



18
19
20
# File 'lib/hnruby/newsitem.rb', line 18

def title
  @title
end

#urlObject (readonly)

Returns the story’s URL.



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

def url
  @url
end

Instance Method Details

#article?Boolean

Returns true if self is an article (with points, comments, etc.).

Returns:

  • (Boolean)


66
67
68
# File 'lib/hnruby/newsitem.rb', line 66

def article?
	@category == :article
end

#comment_pageObject

Returns the CommentPage corresponding to self

news_item.comment_page\
#=> "Amazon and the "profitless business model" fallacy" <21 comments>


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

def comment_page
	return @comment_page if @comment_page
	@comment_page = CommentPage.new comment_url if @comment_url
end

#comment_urlObject

Returns the URL to the story’s comment page. On self-posts, this will be equivalent to url.

news_item.comment_url\
#=> "https://news.ycombinator.com/item?id=6620598"


85
86
87
# File 'lib/hnruby/newsitem.rb', line 85

def comment_url
	"#{HN_URL}#{@comment_url}" if @comment_url
end

#inspectObject

:nodoc:



97
98
99
# File 'lib/hnruby/newsitem.rb', line 97

def inspect
	"\"#{@title}\" <#{@category}: #{@comment_url[8..-1]}>"
end

#joboffer?Boolean

Returns true if self is a job offer (without points, comments, etc.).

Returns:

  • (Boolean)


71
72
73
# File 'lib/hnruby/newsitem.rb', line 71

def joboffer?
	@category == :joboffer
end

#submitter_urlObject

Returns the URL to the HN profile of the submitter.

news_item.submitter_url	#=> "https://news.ycombinator.com/user?id=user"


77
78
79
# File 'lib/hnruby/newsitem.rb', line 77

def submitter_url
	"#{HN_URL}user?id=#{@submitter}" if @submitter
end