Class: Hackernews::Story
- Inherits:
-
Data
- Object
- Data
- Hackernews::Story
- Defined in:
- lib/hackernews/story.rb
Instance Attribute Summary collapse
-
#by ⇒ Object
readonly
Returns the value of attribute by.
-
#descendants ⇒ Object
readonly
Returns the value of attribute descendants.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#by ⇒ Object (readonly)
Returns the value of attribute by
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def by @by end |
#descendants ⇒ Object (readonly)
Returns the value of attribute descendants
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def descendants @descendants end |
#id ⇒ Object (readonly)
Returns the value of attribute id
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def id @id end |
#score ⇒ Object (readonly)
Returns the value of attribute score
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def score @score end |
#text ⇒ Object (readonly)
Returns the value of attribute text
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def text @text end |
#time ⇒ Object (readonly)
Returns the value of attribute time
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def time @time end |
#title ⇒ Object (readonly)
Returns the value of attribute title
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def type @type end |
#url ⇒ Object (readonly)
Returns the value of attribute url
7 8 9 |
# File 'lib/hackernews/story.rb', line 7 def url @url end |
Class Method Details
.from_hash(hash) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/hackernews/story.rb', line 8 def self.from_hash(hash) return unless hash new( id: hash.fetch("id", nil), type: hash.fetch("type", nil), by: hash.fetch("by", ""), time: hash.fetch("time", 0).to_i, title: hash.fetch("title", "Untitled"), url: hash.fetch("url", ""), score: hash.fetch("score", 0).to_i, descendants: hash.fetch("descendants", 0).to_i, text: hash.fetch("text", "") ) end |
Instance Method Details
#article_url ⇒ Object
28 29 30 |
# File 'lib/hackernews/story.rb', line 28 def article_url url.to_s.strip.empty? ? "https://news.ycombinator.com/item?id=#{id}" : url end |
#domain ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/hackernews/story.rb', line 32 def domain return "news.ycombinator.com" if url.to_s.strip.empty? URI.parse(url).hostname.to_s.delete_prefix("www.") rescue URI::InvalidURIError "" end |
#hn_markdown ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/hackernews/story.rb', line 40 def hn_markdown body = CGI.unescapeHTML(text.to_s) body = body.gsub("<p>", "\n\n") body = body.gsub("<pre><code>", "\n\n```") body = body.gsub("</code></pre>", "```\n\n") "# #{title}\n\n#{body}" end |
#readable? ⇒ Boolean
24 25 26 |
# File 'lib/hackernews/story.rb', line 24 def readable? %w[story job poll].include?(type) end |