Class: Golia

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

Instance Method Summary collapse

Constructor Details

#initialize(link) ⇒ Golia

Returns a new instance of Golia.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/golia.rb', line 8

def initialize(link)
  @host = begin
    link = "http://#{link}" unless link =~ /^http(s?)/
    "http#{$1}://" + URI.parse(link).host
  end

  @pid  = "#{Dir.tmpdir}/golia-#{URI.parse(link).host}"
  @checked, @links, @invalid, @valid, @long, @ms = [], [], [], [], [], []

  if File.exist?(@pid)
    puts "<= Founded staled pid"
    Process.kill(9, File.read(@pid).to_i) rescue nil
  end

  trap("INT") { puts "<= Golia has ended his set (crowd applauds)"; kill }

  # begin
    parse!(link)
  # rescue
  #   puts "<= Invalid url #{link}"
  #   kill
  # end
end

Instance Method Details

#killObject



48
49
50
51
# File 'lib/golia.rb', line 48

def kill
  Process.kill(9, Process.pid)
  FileUtils.rm_rf(@pid)
end

#parse!(url) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/golia.rb', line 32

def parse!(url)
  begun_at = Time.now
  response = open(url)
  @ms << Time.now-begun_at
  return if File.extname(url) != ""
  body   = response.read
  links  = body.scan(/href=["'](.+?)["']/m).flatten
  links += body.scan(/<script.+?src=["'](.+?)["']/m).flatten
  links.reject! do |link|
    link =~ /^\/$|^https?|^mailto|^javascript|#|"|'/ ||
    File.extname(link) !~ /\.css|\.js|^$/
  end
  links.map! { |link| link = "/"+link if link !~ /^\//; @host+link }
  @links.concat(links-@checked)
end

#start!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/golia.rb', line 53

def start!
  loop do
    break if @links.empty?
    @links.each do |link|
      begin
        @checked << link
        parse!(link)
        @valid << link
        @long  << link if @ms.last > 1
        puts "\e[32mValid\e[0m (%0.2fms) %s" % [@ms.last, link]
      rescue Exception => e
        @invalid << link
        puts "\e[31mInvalid\e[0m %s - %s" % [link, e.message]
      ensure
        @links.delete(link)
      end
    end
  end
  puts
  puts "======== SUMMARY ========"
  puts "Valid Links: %d" % @valid.size
  puts "Invalid Links: %d" % @invalid.size
  @invalid.each do |link|
    puts "  #{link}"
  end
  puts "Long requests: %d" % @long.size
  @long.each do |link|
    puts "  #{link}"
  end
  puts "Average load time %0.2fms" % [@ms.inject(0) { |memo, ms| memo+=ms; memo }/@ms.size]
  puts
  kill
end