Sha256: 6fa2a95bc7ea034342edb2e16ef16f8f790b06bb450839b22913b75387681b3a

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module Popularity
  class RedditShare < Crawler
    include Popularity::ContainerMethods

    class RedditResult < Popularity::RedditPost
      def initialize(url, r)
        super(url)
        @response = r

        self
      end

      def has_response?
        true
      end

      def valid?
        URI.parse(@url).host
      end

      def fetch
        false
      end

      def fetch_async
        false
      end
    end

    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

2 entries across 2 versions & 1 rubygems

Version Path
popularity-0.1.0 lib/popularity/networks/reddit_share.rb
popularity-0.0.1 lib/popularity/reddit_share.rb