Class: HackerNews::NewsItem
- Inherits:
-
Object
- Object
- HackerNews::NewsItem
- 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
-
#comments ⇒ Object
readonly
Returns the number of comments the story currently has.
-
#number ⇒ Object
readonly
Returns
self
‘s rank on Hacker News at the timeself
was created. -
#points ⇒ Object
readonly
Returns the number of points the story has, or had at the time
self
was created. -
#submitter ⇒ Object
readonly
Returns the username of the story’s submitter.
-
#time ⇒ Object
readonly
Returns the (as a string) how long ago the comment was submitted.
-
#title ⇒ Object
readonly
Returns the story’s title.
-
#url ⇒ Object
readonly
Returns the story’s URL.
Instance Method Summary collapse
-
#article? ⇒ Boolean
Returns true if
self
is an article (with points, comments, etc.). -
#comment_page ⇒ Object
Returns the CommentPage corresponding to
self
. -
#comment_url ⇒ Object
Returns the URL to the story’s comment page.
-
#initialize(article_raw, meta_raw) ⇒ NewsItem
constructor
:stopdoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#joboffer? ⇒ Boolean
Returns true if
self
is a job offer (without points, comments, etc.). -
#submitter_url ⇒ Object
Returns the URL to the HN profile of the submitter.
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 |
# File 'lib/hnruby/newsitem.rb', line 35 def initialize(article_raw, ) article = article_raw / "td" = / "td" @number = article.first.content.to_i @url = article[2].at("a").attributes["href"].value @url.prepend(HN_URL) if url.start_with? "item?id=" @title = article[2].children.first.content if .children.length == 5 #This is a normal article @points = [1].children.first.content.to_i @submitter = [1].children[2].content @time = [1].children[3].content.delete("|").strip @comment_url = [1].children[4].attributes["href"].value @comments = [1].children[4].content.to_i else #This, by contrast, is a job offer, which lacks all meta-info aside #from submission time @time = [1].content end end |
Instance Attribute Details
#comments ⇒ Object (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 |
#number ⇒ Object (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 |
#points ⇒ Object (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 |
#submitter ⇒ Object (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 |
#time ⇒ Object (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 |
#title ⇒ Object (readonly)
Returns the story’s title.
18 19 20 |
# File 'lib/hnruby/newsitem.rb', line 18 def title @title end |
#url ⇒ Object (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.).
60 61 62 |
# File 'lib/hnruby/newsitem.rb', line 60 def article? !@points.nil? end |
#comment_page ⇒ Object
Returns the CommentPage corresponding to self
.
If self
is a job offer, returns nil
.
news_item.comment_page\
#=> "Amazon and the "profitless business model" fallacy" <21 comments>
87 88 89 |
# File 'lib/hnruby/newsitem.rb', line 87 def comment_page @comment_page ||= CommentPage.new comment_url if @comment_url end |
#comment_url ⇒ Object
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"
79 80 81 |
# File 'lib/hnruby/newsitem.rb', line 79 def comment_url "#{HN_URL}#{@comment_url}" if @comment_url end |
#inspect ⇒ Object
:nodoc:
92 93 94 |
# File 'lib/hnruby/newsitem.rb', line 92 def inspect "\"#{@title}\" <#{@category}: #{@comment_url[8..-1]}>" end |
#joboffer? ⇒ Boolean
Returns true if self
is a job offer (without points, comments, etc.).
65 66 67 |
# File 'lib/hnruby/newsitem.rb', line 65 def joboffer? @points.nil? end |
#submitter_url ⇒ Object
Returns the URL to the HN profile of the submitter.
news_item.submitter_url #=> "https://news.ycombinator.com/user?id=user"
71 72 73 |
# File 'lib/hnruby/newsitem.rb', line 71 def submitter_url "#{HN_URL}user?id=#{@submitter}" if @submitter end |