Sha256: 3a0ce7c5f46b81b1bd10b50120ef81443b7ba036786c1be9967ea892ced93020

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

module Popularity
  class Search
    attr_accessor :info
    attr_accessor :results
    attr_accessor :sources
    attr_reader :total
    attr_reader :url

    def initialize(url)
      @url = url
      @info = {}
      total_score = []

      selected_types.each do |network|
        network.fetch_async do |code, body|
          add_result(network)
          begin 
            if network.has_response?
              total_score << network.total
            end
          rescue Exception => e
            puts "#{network.name} had an accident"
            puts e.message
            puts e.backtrace.join("\n")
          end
        end
      end

      loop do 
        # we want the requests to be asyncronous, but we don't 
        # want gem users to have to deal with async code
        # 
        break if selected_types.all? { |network| network.async_done? }
      end

      @total = total_score.reduce(:+)
    end

    def to_json(options ={})
      json = {}
      self.results.collect do |result|
        json[result.name.to_s] = result.to_json
      end

      self.sources.collect do |source|
        json[source.to_s] = self.send(source.to_sym).to_json
      end

      json["total"] = total

      json
    end

    protected

    def selected_types
      # github.com stats only valid for github urls, etc
      @types ||= Popularity::TYPES.collect { |n|
        network = n.new(@url)
        network if network.valid?
      }.compact
    end

    def add_result(result)
      self.sources ||= []
      self.results ||= []
      self.results << result
      self.sources << result.name.to_sym

      self.instance_variable_set "@#{result.name}", result

      self.define_singleton_method(result.name.to_sym) { result }
    end

    def to_s
      self.class.name
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
popularity-0.1.1 lib/popularity/search.rb
popularity-0.1.0 lib/popularity/search.rb
popularity-0.0.1 lib/popularity/search.rb