Class: Popularity::RedditShare

Inherits:
Crawler
  • Object
show all
Includes:
ContainerMethods
Defined in:
lib/popularity/networks/reddit_share.rb

Instance Attribute Summary

Attributes inherited from Crawler

#url

Instance Method Summary collapse

Methods included from ContainerMethods

included

Methods inherited from Crawler

#async_done?, #fetch, #fetch_async, #has_response?, #host, #response, #response_json, #valid?

Constructor Details

#initialize(*args) ⇒ RedditShare

Returns a new instance of RedditShare.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/popularity/networks/reddit_share.rb', line 6

def initialize(*args)
  super(*args)
  posts_json = response_json["data"]["children"]
  posts_json.each do |child|
    new_json = response_json.clone

    new_json["data"]["children"] = [child]
    url = "http://reddit.com#{child["data"]["permalink"]}"
    post = RedditResult.new(url, JSON.dump([new_json]))

    self.add_result(post)
  end

  self
end

Instance Method Details

#nameObject



40
41
42
# File 'lib/popularity/networks/reddit_share.rb', line 40

def name
  "reddit"
end

#postsObject



36
37
38
# File 'lib/popularity/networks/reddit_share.rb', line 36

def posts
  @results.size rescue 0
end

#request_urlObject



44
45
46
# File 'lib/popularity/networks/reddit_share.rb', line 44

def request_url
  "http://www.reddit.com/r/search/search.json?q=url:#{@url}"
end

#to_json(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/popularity/networks/reddit_share.rb', line 22

def to_json(options ={})
  total = {"comments" => 0, "posts" => 0, "score" => 0}
  return total unless @results

  @results.collect(&:to_json).each do |json|
    json.each do |key, value|
        total[key] ||= 0
        total[key] += value
    end
  end
  total["posts"] = posts
  total
end