Class: Waw::Commands::ProfileCommand

Inherits:
Command show all
Defined in:
lib/waw/commands/profile_command.rb

Instance Attribute Summary

Attributes inherited from Command

#trace, #verbosity

Instance Method Summary collapse

Methods inherited from Command

#add_options, #exit, #info, #initialize, #options, #run, #shell_exec, #verbose

Constructor Details

This class inherits a constructor from Waw::Commands::Command

Instance Method Details

#__run(requester_file, arguments) ⇒ Object

Runs the sub-class defined command



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/waw/commands/profile_command.rb', line 44

def __run(requester_file, arguments)
  require 'waw/wspec/browser'
  
  # Start the server first
  thread, waw_kernel = ::Waw::Commands::StartCommand.new.waw_start(requester_file, verbose)
  visited, browser, location = {}, ::Waw::WSpec::Browser.new, waw_kernel.config.web_base
  
  # Launch the visit
  t1 = Time.now
  visit(browser, location, visited)
  t2 = Time.now
  
  # Stop the server now
  thread.exit
  
  # Show statistics
  req_by_sec = visited.size.to_f/(t2-t1)
  avg_by_sec = (t2-t1)/visited.size
  
  puts "#{visited.size} pages visited in #{t2-t1} seconds (#{req_by_sec} req./sec., #{avg_by_sec} sec./req.)"
rescue Interrupt => ex
  info "waw-profile stopping now... ciao!" if verbose
end


7
8
9
10
11
# File 'lib/waw/commands/profile_command.rb', line 7

def banner
  <<-EOF
    Usage: [waw-]profile [options]
  EOF
end

#check_command_policyObject

Start command is always safe



14
15
16
# File 'lib/waw/commands/profile_command.rb', line 14

def check_command_policy
  true
end

#ignore?(location, visited) ⇒ Boolean

Ignores a given location?

Returns:



19
20
21
# File 'lib/waw/commands/profile_command.rb', line 19

def ignore?(location, visited)
  visited[location] or /\.(gif|zip|pdf|jpg|csv)$/ =~ location
end

#visit(browser, location, visited = {}) ⇒ Object

Recursively visits the whole website



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/waw/commands/profile_command.rb', line 24

def visit(browser, location, visited = {})
  return if ignore?(location, visited)
  visited[location] = true
  browser.location = location
  unless browser.is200
    puts "Hohoho, I've found a lost internal link #{location}"
    return
  else
    puts "Visiting #{location}" if verbose
    browser.all_internal_links.each do |link|
      begin
        visit(browser, link[:href], visited)
      rescue URI::InvalidURIError => ex
        puts "Hohoho, I've found something really wrong #{link[:href]}"
      end
    end
  end 
end