Class: Popularity::Search
- Inherits:
-
Object
- Object
- Popularity::Search
- Defined in:
- lib/popularity/search.rb
Instance Attribute Summary collapse
-
#info ⇒ Object
Returns the value of attribute info.
-
#results ⇒ Object
Returns the value of attribute results.
-
#sources ⇒ Object
Returns the value of attribute sources.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url) ⇒ Search
constructor
A new instance of Search.
- #to_json(options = {}) ⇒ Object
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. 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
#info ⇒ Object
Returns the value of attribute info.
3 4 5 |
# File 'lib/popularity/search.rb', line 3 def info @info end |
#results ⇒ Object
Returns the value of attribute results.
4 5 6 |
# File 'lib/popularity/search.rb', line 4 def results @results end |
#sources ⇒ Object
Returns the value of attribute sources.
5 6 7 |
# File 'lib/popularity/search.rb', line 5 def sources @sources end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
6 7 8 |
# File 'lib/popularity/search.rb', line 6 def total @total end |
#url ⇒ Object (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( ={}) 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 |