Sha256: a4dc6c3230ba16f92530dacda69d3ca2923c2c222eb73aad4ba3ac56e534ca7a

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module Popularity
  class RedditShare < Crawler
    include Popularity::ContainerMethods

    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

    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

    def posts
      @results.size rescue 0
    end

    def name
      "reddit"
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
popularity-0.1.1 lib/popularity/networks/reddit_share.rb