golia 1.0 → 1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/bin/golia +8 -69
  2. data/golia.gemspec +2 -2
  3. data/lib/golia.rb +72 -0
  4. metadata +4 -3
data/bin/golia CHANGED
@@ -1,71 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'rubygems'
3
- require 'open-uri'
4
- require 'net/http'
5
- require 'fileutils'
6
- require 'benchmark'
7
- require 'tmpdir'
8
-
9
- puts "You need to pass a domain ex: www.lipsiasoft.com" and exit unless ARGV[0]
10
-
11
- @host = ARGV[0]
12
- @host = "http://#{@host}" unless @host =~ /^http/
13
- @pid = "#{Dir.tmpdir}/golia-#{@host.gsub(/^http:\/\//, '')}"
14
- @checked, @links, @invalid, @valid, @long, @ms = [], [], [], [], [], []
15
-
16
- if File.exist?(@pid)
17
- puts "<= Founded staled pid"
18
- Process.kill(9, File.read(@pid).to_i) rescue nil
19
- end
20
-
21
- trap("INT") { puts "<= Golia has ended his set (crowd applauds)"; remove_pid; Process.kill(9, Process.pid) }
22
- File.open(@pid, "w") { |f| f.write Process.pid }
23
-
24
- def remove_pid
25
- FileUtils.rm_rf(@pid)
26
- end
27
-
28
- def parse!(url)
29
- begun_at = Time.now
30
- response = open(url)
31
- @ms << Time.now-begun_at
32
- body = response.read
33
- links = body.scan(/<a.+?href=["'](.+?)["']/m).flatten
34
- links.reject! { |link| link =~ /^\/$|^http|^mailto|^javascript|#/ || File.extname(link) != "" }
35
- links.map! { |link| link = "/"+link if link !~ /^\//; @host+link }
36
- @links.concat(links-@checked)
37
- end
38
-
39
- parse!(@host)
40
-
41
- loop do
42
- break if @links.empty?
43
- @links.each do |link|
44
- begin
45
- @checked << link
46
- parse!(link)
47
- @valid << link
48
- @long << link if @ms.last > 1
49
- puts "\e[32mValid\e[0m (%0.2fms) %s" % [@ms.last, link]
50
- rescue Exception => e
51
- @invalid << link
52
- puts "\e[31mInvalid\e[0m %s - %s" % [link, e.message]
53
- ensure
54
- @links.delete(link)
55
- end
56
- end
57
- end
58
- puts
59
- puts "======== SUMMARY ========"
60
- puts "Valid Links: %d" % @valid.size
61
- puts "Invalid Links: %d" % @invalid.size
62
- @invalid.each do |link|
63
- puts " #{link}"
64
- end
65
- puts "Long requests: %d" % @long.size
66
- @long.each do |link|
67
- puts " #{link}"
68
- end
69
- puts "Average load time %0.2fms" % (@ms.inject(0) { |memo, ms| memo+=ms; memo }/@ms.size)
70
- puts
71
- remove_pid
3
+ require File.dirname(__FILE__) + '/../lib/golia'
4
+
5
+ if ARGV[0]
6
+ golia = Golia.new(ARGV[0])
7
+ golia.start!
8
+ else
9
+ puts "<= You need to pass a domain ex: www.lipsiasoft.com"
10
+ end
data/golia.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.description = "Golia is an website performance analyzer. Check speed and dead links."
9
9
  s.default_executable = "golia"
10
10
  s.executables = ["golia"]
11
- s.version = "1.0"
11
+ s.version = "1.1"
12
12
  s.date = Time.now.strftime("%Y-%m-%d")
13
13
  s.extra_rdoc_files = Dir["*.rdoc"]
14
- s.files = %w(README.rdoc Rakefile golia.gemspec)
14
+ s.files = %w(README.rdoc Rakefile golia.gemspec) + Dir["lib/**/*"]
15
15
  end
data/lib/golia.rb ADDED
@@ -0,0 +1,72 @@
1
+ require 'open-uri'
2
+ require 'net/http'
3
+ require 'fileutils'
4
+ require 'benchmark'
5
+ require 'tmpdir'
6
+
7
+ class Golia
8
+ def initialize(host)
9
+ @host = host
10
+ @host = "http://#{@host}" unless @host =~ /^http/
11
+ @pid = "#{Dir.tmpdir}/golia-#{@host.gsub(/^http:\/\//, '')}"
12
+ @checked, @links, @invalid, @valid, @long, @ms = [], [], [], [], [], []
13
+
14
+ if File.exist?(@pid)
15
+ puts "<= Founded staled pid"
16
+ Process.kill(9, File.read(@pid).to_i) rescue nil
17
+ end
18
+
19
+ trap("INT") { puts "<= Golia has ended his set (crowd applauds)"; kill; Process.kill(9, Process.pid) }
20
+
21
+ parse!(@host)
22
+ end
23
+
24
+ def parse!(url)
25
+ begun_at = Time.now
26
+ response = open(url)
27
+ @ms << Time.now-begun_at
28
+ body = response.read
29
+ links = body.scan(/<a.+?href=["'](.+?)["']/m).flatten
30
+ links.reject! { |link| link =~ /^\/$|^http|^mailto|^javascript|#/ || File.extname(link) != "" }
31
+ links.map! { |link| link = "/"+link if link !~ /^\//; @host+link }
32
+ @links.concat(links-@checked)
33
+ end
34
+
35
+ def kill
36
+ FileUtils.rm_rf(@pid)
37
+ end
38
+
39
+ def start!
40
+ loop do
41
+ break if @links.empty?
42
+ @links.each do |link|
43
+ begin
44
+ @checked << link
45
+ parse!(link)
46
+ @valid << link
47
+ @long << link if @ms.last > 1
48
+ puts "\e[32mValid\e[0m (%0.2fms) %s" % [@ms.last, link]
49
+ rescue Exception => e
50
+ @invalid << link
51
+ puts "\e[31mInvalid\e[0m %s - %s" % [link, e.message]
52
+ ensure
53
+ @links.delete(link)
54
+ end
55
+ end
56
+ end
57
+ puts
58
+ puts "======== SUMMARY ========"
59
+ puts "Valid Links: %d" % @valid.size
60
+ puts "Invalid Links: %d" % @invalid.size
61
+ @invalid.each do |link|
62
+ puts " #{link}"
63
+ end
64
+ puts "Long requests: %d" % @long.size
65
+ @long.each do |link|
66
+ puts " #{link}"
67
+ end
68
+ puts "Average load time %0.2fms" % [@ms.inject(0) { |memo, ms| memo+=ms; memo }/@ms.size]
69
+ puts
70
+ kill
71
+ end
72
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: golia
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
- version: "1.0"
8
+ - 1
9
+ version: "1.1"
10
10
  platform: ruby
11
11
  authors:
12
12
  - DAddYE
@@ -30,6 +30,7 @@ files:
30
30
  - README.rdoc
31
31
  - Rakefile
32
32
  - golia.gemspec
33
+ - lib/golia.rb
33
34
  - bin/golia
34
35
  has_rdoc: true
35
36
  homepage: http://www.padrinorb.com