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.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#initialize(url) ⇒ Search
constructor
A new instance of Search.
- #total ⇒ Object
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. 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
#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 |
#url ⇒ Object (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( ={}) 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 ⇒ Object
49 50 51 |
# File 'lib/popularity/search.rb', line 49 def total self.results.collect(&:total).compact.reduce(:+) end |