Class: Hackernews::Story

Inherits:
Data
  • Object
show all
Defined in:
lib/hackernews/story.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#byObject (readonly)

Returns the value of attribute by

Returns:

  • (Object)

    the current value of by



7
8
9
# File 'lib/hackernews/story.rb', line 7

def by
  @by
end

#descendantsObject (readonly)

Returns the value of attribute descendants

Returns:

  • (Object)

    the current value of descendants



7
8
9
# File 'lib/hackernews/story.rb', line 7

def descendants
  @descendants
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



7
8
9
# File 'lib/hackernews/story.rb', line 7

def id
  @id
end

#scoreObject (readonly)

Returns the value of attribute score

Returns:

  • (Object)

    the current value of score



7
8
9
# File 'lib/hackernews/story.rb', line 7

def score
  @score
end

#textObject (readonly)

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



7
8
9
# File 'lib/hackernews/story.rb', line 7

def text
  @text
end

#timeObject (readonly)

Returns the value of attribute time

Returns:

  • (Object)

    the current value of time



7
8
9
# File 'lib/hackernews/story.rb', line 7

def time
  @time
end

#titleObject (readonly)

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



7
8
9
# File 'lib/hackernews/story.rb', line 7

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



7
8
9
# File 'lib/hackernews/story.rb', line 7

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url

Returns:

  • (Object)

    the current value of 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_urlObject



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

#domainObject



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_markdownObject



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

Returns:

  • (Boolean)


24
25
26
# File 'lib/hackernews/story.rb', line 24

def readable?
  %w[story job poll].include?(type)
end