Class: Popularity::RedditPost

Inherits:
Crawler
  • Object
show all
Defined in:
lib/popularity/networks/reddit_post.rb

Direct Known Subclasses

RedditResult

Instance Attribute Summary

Attributes inherited from Crawler

#url

Instance Method Summary collapse

Methods inherited from Crawler

#async_done?, #fetch, #fetch_async, #has_response?, #host, #initialize, #response, #response_json, #to_json

Constructor Details

This class inherits a constructor from Popularity::Crawler

Instance Method Details

#as_json(options = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/popularity/networks/reddit_post.rb', line 19

def as_json(options = {})      
  {
    "comments" => comments,
    "score" => score
  }
end

#commentsObject



11
12
13
14
15
16
17
# File 'lib/popularity/networks/reddit_post.rb', line 11

def comments
  begin
    response_json[0]["data"]["children"][0]["data"]["num_comments"] 
  rescue
    0
  end
end

#nameObject



37
38
39
# File 'lib/popularity/networks/reddit_post.rb', line 37

def name
  "reddit"
end

#scoreObject



3
4
5
6
7
8
9
# File 'lib/popularity/networks/reddit_post.rb', line 3

def score
  begin
    response_json[0]["data"]["children"][0]["data"]["score"] 
  rescue
    0
  end
end

#totalObject



26
27
28
# File 'lib/popularity/networks/reddit_post.rb', line 26

def total
  comments + score
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/popularity/networks/reddit_post.rb', line 30

def valid?
  return false unless host == 'reddit.com'
  
  path = URI.parse(@url).path
  path.split('/').delete_if { |a| a.empty? }.size < 6
end