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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/popularity/search.rb', line 9

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

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

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#to_json(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/popularity/search.rb', line 39

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