Class: Popularity::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/popularity/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Search

Returns a new instance of Search.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#infoObject

Returns the value of attribute info.



3
4
5
# File 'lib/popularity/search.rb', line 3

def info
  @info
end

#resultsObject

Returns the value of attribute results.



4
5
6
# File 'lib/popularity/search.rb', line 4

def results
  @results
end

#sourcesObject

Returns the value of attribute sources.



5
6
7
# File 'lib/popularity/search.rb', line 5

def sources
  @sources
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/popularity/search.rb', line 6

def url
  @url
end

Instance Method Details

#as_json(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

#totalObject



49
50
51
# File 'lib/popularity/search.rb', line 49

def total
  self.results.collect(&:total).compact.reduce(:+)
end