class Popularity::Search

Attributes

info[RW]
results[RW]
sources[RW]
url[R]

Public Class Methods

new(url) click to toggle source
# File lib/popularity/search.rb, line 8
def initialize(url)
  @url = url
  @info = {}
  total_score = []

  selected_types.each do |network|
    network.fetch_async do |code, body|
      add_result(network)
      begin

      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
end

Public Instance Methods

as_json(options ={}) click to toggle source
# File lib/popularity/search.rb, line 34
def as_json(options ={})
  json = {}
  self.results.collect do |result|
    json[result.name.to_s] = result.as_json
  end

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

  json["total"] = total

  json
end
total() click to toggle source
# File lib/popularity/search.rb, line 49
def total
  self.results.collect(&:total).compact.reduce(:+)
end

Protected Instance Methods

add_result(result) click to toggle source
# File lib/popularity/search.rb, line 63
def add_result(result)
  self.sources ||= []
  self.results ||= []
  self.results << result
  self.sources << result.name.to_sym

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

  # if there's a facebook result, this class will
  # have a facebook method returning it
  self.define_singleton_method(result.name.to_sym) { result }
end
selected_types() click to toggle source
# File lib/popularity/search.rb, line 55
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
to_s() click to toggle source
# File lib/popularity/search.rb, line 76
def to_s
  self.class.name
end