lib/page_rankr/tracker.rb in PageRankr-1.7.1 vs lib/page_rankr/tracker.rb in PageRankr-2.0.0
- old
+ new
@@ -1,23 +1,48 @@
+require 'typhoeus'
+
module PageRankr
- class Tracker
+ module Tracker
attr_accessor :site_trackers
def initialize
- @site_trackers = self.class.constants.collect{|tracker| tracker.to_s.downcase.to_sym}
+ @site_trackers = self.class.constants.collect{|tracker| symbol_for(tracker)}
end
def lookup(site, *trackers)
trackers = site_trackers if trackers.empty?
tracked = {}
+ hydra = Typhoeus::Hydra.new
trackers.each do |tracker|
- name, klass = tracker.to_s.capitalize, self.class
+ name, klass = constant_name(tracker), self.class
next unless klass.const_defined? name
- tracked[tracker] = klass.const_get(name).new(site).tracked
+ tracked[tracker] = klass.const_get(name).new(site)
+ hydra.queue tracked[tracker].request
end
+ hydra.run
+
+ tracked.keys.each do |tracker|
+ tracked[tracker] = tracked[tracker].tracked
+ end
+
tracked
+ end
+
+ private
+
+ def symbol_for(klass)
+ word = klass.to_s.dup
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/){|match| "#{$1}_#{$2}" }
+ word.gsub!(/([a-z\d])([A-Z])/){|match| "#{$1}_#{$2}" }
+ word.tr!("-", "_")
+ word.downcase!
+ word.to_sym
+ end
+
+ def constant_name(sym)
+ sym.to_s.split('_').collect{|str| str.capitalize}.join
end
end
end
\ No newline at end of file