lib/popularity.rb in popularity-0.1.1 vs lib/popularity.rb in popularity-0.2.1
- old
+ new
@@ -5,23 +5,21 @@
module Popularity
TYPES = []
end
-Gem.find_files("popularity/networks/*.rb").each { |path|
+Gem.find_files("popularity/networks/*.rb").each { |path|
require path
file_name = path.split('/').last.split('.').first
class_name = file_name.gsub(/_[a-z]|^[a-z]/, &:upcase).gsub('_', '')
# Auto register the types in /networks
Popularity::TYPES << Object.const_get("Popularity::#{class_name}")
}
module Popularity
def self.search(*urls)
- response = {}
-
MultiSearch.new(:urls => urls)
end
def self.select_types(url)
# github.com stats only valid for github urls, etc
@@ -41,53 +39,48 @@
@searches.each do |search|
search.results.each do |result|
add_search_result(result)
end
end
- end
+ end
def results
searches.collect(&:results).reduce(:+)
end
- def to_json(options = {})
+ def as_json(options = {})
json = {}
self.searches.collect do |search|
- json[search.url] = search.to_json
+ json[search.url] = search.as_json
end
self.sources.collect do |source|
- json[source.to_s] = self.send(source.to_sym).to_json
+ json[source.to_s] = self.send(source.to_sym).as_json
end
json["total"] = total
json
end
def total
- total = 0
- self.searches.each do |a|
- total += a.total
- end
-
- total
+ self.searches.collect(&:total).compact.reduce(:+)
end
protected
def add_search_result(result)
- container = self.instance_variable_get("@#{result.name}")
+ container = self.instance_variable_get("@#{result.name}")
unless container
- @sources ||= []
- @sources << result.name.to_sym
+ @sources ||= []
+ @sources << result.name.to_sym
container = Popularity::ResultsContainer.new
self.instance_variable_set "@#{result.name}", container
self.define_singleton_method(result.name.to_sym) { container }
end
container.add_result(result)
end
end
-end
\ No newline at end of file
+end