Class: Cookler

Inherits:
Object
  • Object
show all
Defined in:
lib/cookler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(targets, max_results = nil) ⇒ Cookler

Returns a new instance of Cookler.



8
9
10
11
12
13
14
15
16
17
# File 'lib/cookler.rb', line 8

def initialize(targets, max_results=nil)
    @targets      = targets
    @max_results  = (max_results.nil? ? 15 : max_results)
    @conn         = Mongo::Connection.new
    @db           = @conn['cookler-db']
    @pages        = @db['pages']
    @stats        = @db['stats']
    @depth_limit  = 3
    @threads      = []
end

Class Method Details

.analyze(targets, max_results = nil) ⇒ Object



19
20
21
22
# File 'lib/cookler.rb', line 19

def self.analyze(targets, max_results=nil)
  cookler = self.new(targets, max_results)
  cookler.run
end

Instance Method Details

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cookler.rb', line 24

def run
  
  total_words = []
  
  @pages.remove
  @stats.remove
  
  general_stime = Time.now.to_f
  @targets.each do |target|
    @threads << Thread.new(target) do |current_target|
      current_target_stime = Time.now.to_f
      current_target_words = _get_words_on_pages current_target
      total_words += current_target_words
      current_target_wstats = Hash[current_target_words.group_by{ |word| word.to_s.downcase}.map{ |word, instances| [word, instances.length] }.sort_by(&:last).reverse]
      @stats.insert({:type => current_target[0], :duration => (Time.now.to_f - current_target_stime).to_s, :wstats => current_target_wstats})
    end
  end
  @threads.each { |aThread|  aThread.join }
  
  general_wstats = Hash[total_words.group_by{ |word| word.to_s.downcase}.map{ |word, instances| [word, instances.length] }.sort_by(&:last).reverse]
  @stats.insert({:type => :general, :duration => (Time.now.to_f - general_stime).to_s, :wstats => general_wstats})
end