sla 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c4f0612c8ccebbc1c29a22eee3ad61cd7610563
4
- data.tar.gz: 1e6ed70e361c653b2c780e4be344ecd624529efc
3
+ metadata.gz: 40e98d12594ea7a28b2dc3d13bc739052d94e5b6
4
+ data.tar.gz: 7e3cfcd96379993df956f848ada469bd51d60680
5
5
  SHA512:
6
- metadata.gz: b2c1eb8bc98e1c0139e7e7155744cba9266364c6edc1f5bd254af233643f4eee50693171fc247f3555b31e1d1a6191df90bcb763dac825935253274805bee1ba
7
- data.tar.gz: 216a0aa3ca464ad342826f60c1f1e73dbde5b5f0d17aadafe24551d658f52e665d7ae3513b5b086039e66b5f9800d27f447caa54c5ffd9cec58e341bbb9205d0
6
+ metadata.gz: 1097f835963ed1be41a4b71fa14aace3a9d3ea03398d182125a96bdd6c893fe9d98fd604514184b8d727590376964d26641cb97d0b03b7f5ddafbe22523920f9
7
+ data.tar.gz: e2a63d7e2210d7f78863cb8032dd2128276d7ba8417bb12076f7beba40a373d93e9f85f43daeb69c43d5ab3ee9e15ca80956ba21e6a153a170d7bb0c6ff203c0
data/README.md CHANGED
@@ -25,6 +25,30 @@ Or with bundler:
25
25
  gem 'sla'
26
26
  ```
27
27
 
28
+
29
+ Features
30
+ --------------------------------------------------
31
+
32
+ - Easy to use command line interface
33
+ - Built in caching, to avoid over stressing the server
34
+ - Outputs the site tree to screen and log file
35
+
36
+
37
+ Example Output
38
+ --------------------------------------------------
39
+ ```
40
+ $ sla check localhost:3000
41
+ 1 200 - http://localhost:3000
42
+ 2 200 -- /whiskey
43
+ 3 200 --- /whiskey/tango
44
+ 4 404 ---- /whiskey/tango/foxtrot
45
+ 5 200 -- /ten
46
+ 6 200 --- /ten/four
47
+ 7 200 -- /roger
48
+ Done with 1 failures
49
+ ```
50
+
51
+
28
52
  Usage
29
53
  --------------------------------------------------
30
54
 
@@ -48,12 +72,20 @@ Options:
48
72
  Set cache life in seconds [default: 86400]
49
73
 
50
74
  --cache-dir DIR
51
- Set the cache directory [default: cache]
75
+ Set the cache directory
52
76
 
53
- --color
77
+ --no-color
54
78
  Disable colors in output
55
79
 
80
+ --no-log
81
+ Disable logging
82
+
83
+ --log LOGFILE
84
+ Set the name of the logfile [default: sla.log]
85
+
56
86
  Examples:
57
87
  sla check example.com
58
88
  sla check example.com -c360 -d10
89
+ sla check example.com --cache-dir my_cache --no-log
90
+ sla check example.com --depth 10 --log my_log.log
59
91
  ```
data/lib/sla/cache.rb CHANGED
@@ -2,6 +2,14 @@ module SLA
2
2
  class Cache
3
3
  include Singleton
4
4
 
5
+ def self.get(url)
6
+ instance.cache.get url
7
+ end
8
+
9
+ def self.settings
10
+ instance.cache
11
+ end
12
+
5
13
  def cache
6
14
  @cache ||= cache!
7
15
  end
data/lib/sla/checker.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module SLA
2
- class Checker < Base
2
+ class Checker
3
3
  include Colsole
4
4
 
5
- attr_accessor :checked_links, :results, :max_depth, :next_check
5
+ attr_accessor :checked_links, :max_depth, :next_check
6
6
 
7
7
  def initialize
8
8
  @max_depth = 10
@@ -14,30 +14,31 @@ module SLA
14
14
  checked_links.count
15
15
  end
16
16
 
17
- def on_check(urls=nil, depth=1, &block)
18
- urls ||= [base_url]
17
+ def on_check(urls, depth=1, &block)
18
+ urls = [urls] if urls.is_a? String
19
19
 
20
20
  self.next_check = []
21
21
 
22
22
  urls.each do |url|
23
23
  check_url url, depth, &block
24
- if depth < max_depth
24
+ if depth < max_depth && !next_check.empty?
25
25
  on_check next_check, depth+1, &block
26
26
  end
27
27
  end
28
28
  end
29
29
 
30
30
  def check_url(url, depth, &_block)
31
- page = Page.new url, depth: depth, base_url: base_url
31
+ page = Page.new url, depth: depth
32
32
  page.validate
33
- checked_links.push url
34
33
 
35
34
  yield page if block_given?
36
35
  return if depth >= max_depth
36
+ return if checked_links.include? url
37
+
38
+ checked_links.push url
37
39
 
38
40
  page.links.each do |link|
39
- next if checked_links.include? link.url
40
- next_check.push link.url
41
+ next_check.push link.url unless next_check.include? link.url
41
42
  end
42
43
  end
43
44
  end
@@ -1,5 +1,5 @@
1
1
  module SLA
2
- class CommandLine < Base
2
+ class CommandLine
3
3
  include Singleton
4
4
  include Colsole
5
5
 
@@ -17,7 +17,8 @@ module SLA
17
17
 
18
18
  def handle(args)
19
19
  if args['check']
20
- @no_color = args['--color']
20
+ @no_color = args['--no-color']
21
+ @no_log = args['--no-log']
21
22
  check_domain args
22
23
  end
23
24
  end
@@ -25,34 +26,38 @@ module SLA
25
26
  def check_domain(args)
26
27
  checker = Checker.new
27
28
  checker.max_depth = args['--depth'].to_i
28
- checker.cache.life = args['--cache'].to_i
29
- checker.cache.dir = args['--cache-dir'] if args['--cache-dir']
30
- url_manager.base_url = args['DOMAIN']
29
+ Cache.settings.life = args['--cache'].to_i
30
+ Cache.settings.dir = args['--cache-dir'] if args['--cache-dir']
31
+ logfile = args['--log']
32
+ start_url = args['DOMAIN']
31
33
 
32
- File.unlink 'log.log' if File.exist? 'log.log'
34
+ start_url = "http://#{start_url}" unless start_url[0..3] == 'http'
35
+
36
+ File.unlink logfile if File.exist? logfile
33
37
 
34
38
  count = 1
35
39
  failed = 0
36
40
 
37
- open('log.log', 'a') do |f|
38
- checker.on_check do |page|
39
- indent = '-' * page.depth
41
+ log = []
40
42
 
41
- status = page.status
42
- colored_status = color_status status
43
- failed +=1 if status != '200'
43
+ checker.on_check start_url do |page|
44
+ indent = '-' * page.depth
44
45
 
45
- say "#{count} #{colored_status} #{indent} #{page.name}"
46
- f.puts "#{count} #{status} #{indent} #{page.name}"
47
- count += 1
48
- end
46
+ status = page.status
47
+ colored_status = color_status status
48
+ failed +=1 if status != '200'
49
49
 
50
- color = failed > 0 ? '!txtred!' : '!txtgrn!'
51
- color = "" if @no_color
52
- say "#{color}Done with #{failed} failures"
53
- f.puts "Done with #{failed} failures"
50
+ say "#{count} #{colored_status} #{indent} #{page.name}"
51
+ log.push "#{count} #{status} #{indent} #{page.name}" unless @no_log
52
+ count += 1
54
53
  end
55
54
 
55
+ color = failed > 0 ? '!txtred!' : '!txtgrn!'
56
+ color = "" if @no_color
57
+ say "#{color}Done with #{failed} failures"
58
+ log.push "Done with #{failed} failures" unless @no_log
59
+
60
+ File.write logfile, log.join("\n") unless @no_log
56
61
  end
57
62
 
58
63
  def color_status(status)
data/lib/sla/docopt.txt CHANGED
@@ -18,9 +18,17 @@ Options:
18
18
  --cache-dir DIR
19
19
  Set the cache directory
20
20
 
21
- --color
21
+ --no-color
22
22
  Disable colors in output
23
23
 
24
+ --no-log
25
+ Disable logging
26
+
27
+ --log LOGFILE
28
+ Set the name of the logfile [default: sla.log]
29
+
24
30
  Examples:
25
31
  sla check example.com
26
32
  sla check example.com -c360 -d10
33
+ sla check example.com --cache-dir my_cache --no-log
34
+ sla check example.com --depth 10 --log my_log.log
data/lib/sla/link.rb CHANGED
@@ -1,15 +1,41 @@
1
1
  module SLA
2
2
  class Link
3
- attr_accessor :text, :url
3
+ attr_accessor :text, :href
4
+ attr_reader :parent
4
5
 
5
- def initialize(text, url)
6
- @text = text
7
- @url = url
6
+ def initialize(href, opts={})
7
+ @href = href
8
+ @text = opts[:text]
9
+ self.parent = opts[:parent] || @href
10
+ end
11
+
12
+ def uri
13
+ @uri ||= URI.parse href
14
+ end
15
+
16
+ def parent=(url)
17
+ @parent = url.is_a?(String) ? URI.parse(url) : url
8
18
  end
9
19
 
10
20
  def path
11
- uri = URI.parse url
12
21
  uri.request_uri
13
22
  end
23
+
24
+ def full_uri
25
+ return uri if uri.absolute? || !parent.absolute?
26
+ URI.join parent, href
27
+ end
28
+
29
+ def url
30
+ full_uri.to_s
31
+ end
32
+
33
+ def external?
34
+ parent.host != full_uri.host
35
+ end
36
+
37
+ def interesting?
38
+ !external? && full_uri.scheme =~ /^http/
39
+ end
14
40
  end
15
41
  end
data/lib/sla/page.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module SLA
2
- class Page < Base
2
+ class Page
3
3
  attr_accessor :depth, :status, :base_uri
4
4
  attr_reader :url
5
5
 
@@ -32,21 +32,12 @@ module SLA
32
32
  end
33
33
  end
34
34
 
35
- def protocol
36
- @protocol ||= protocol!
37
- end
38
-
39
- def protocol!
40
- uri = URI.parse url
41
- uri.scheme
42
- end
43
-
44
35
  def content
45
36
  @content ||= content!
46
37
  end
47
38
 
48
39
  def content!
49
- response = cache.get url
40
+ response = Cache.get url
50
41
  self.status = response.error ? '404' : '200'
51
42
  self.base_uri = response.base_uri
52
43
  response.content
@@ -61,12 +52,11 @@ module SLA
61
52
  end
62
53
 
63
54
  def links!
64
- links = doc.css('a')
55
+ anchors = doc.css('a')
65
56
  result = []
66
- links.each do |link|
67
- href = url_manager.absolute link['href'], base_uri
68
- next unless href
69
- result.push Link.new link.text, href
57
+ anchors.each do |a|
58
+ link = Link.new a['href'], text: a.text, parent: base_uri
59
+ result.push link if link.interesting?
70
60
  end
71
61
  result
72
62
  end
data/lib/sla/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SLA
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/sla.rb CHANGED
@@ -6,14 +6,8 @@ require 'docopt'
6
6
  require 'uri'
7
7
 
8
8
  require 'sla/version'
9
- require 'sla/base'
10
9
  require 'sla/page'
11
10
  require 'sla/link'
12
11
  require 'sla/checker'
13
12
  require 'sla/command_line'
14
13
  require 'sla/cache'
15
- require 'sla/url_manager'
16
-
17
- # Remove before deploy
18
- # require 'byebug'
19
- # require 'pp'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-15 00:00:00.000000000 Z
11
+ date: 2016-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole
@@ -160,14 +160,12 @@ files:
160
160
  - README.md
161
161
  - bin/sla
162
162
  - lib/sla.rb
163
- - lib/sla/base.rb
164
163
  - lib/sla/cache.rb
165
164
  - lib/sla/checker.rb
166
165
  - lib/sla/command_line.rb
167
166
  - lib/sla/docopt.txt
168
167
  - lib/sla/link.rb
169
168
  - lib/sla/page.rb
170
- - lib/sla/url_manager.rb
171
169
  - lib/sla/version.rb
172
170
  homepage: https://github.com/DannyBen/sla
173
171
  licenses:
@@ -192,5 +190,5 @@ rubyforge_project:
192
190
  rubygems_version: 2.6.6
193
191
  signing_key:
194
192
  specification_version: 4
195
- summary: Site Link Analyzer
193
+ summary: Command Line Site Link Analyzer
196
194
  test_files: []
data/lib/sla/base.rb DELETED
@@ -1,17 +0,0 @@
1
- module SLA
2
- class Base
3
- attr_accessor :domain
4
-
5
- def cache
6
- @cache ||= Cache.instance.cache
7
- end
8
-
9
- def url_manager
10
- @url_manager ||= UrlManager.instance
11
- end
12
-
13
- def base_url
14
- url_manager.base_url.to_s
15
- end
16
- end
17
- end
@@ -1,23 +0,0 @@
1
- module SLA
2
- class UrlManager < Base
3
- include Singleton
4
-
5
- attr_reader :uri, :base_url
6
-
7
- def base_url=(url)
8
- url = "http://#{url}" unless url[0..3] == 'http'
9
- response = cache.get url
10
- @base_url = response.base_uri
11
- end
12
-
13
- def absolute(relative, base=nil)
14
- return false if relative =~ /^(tel|mailto|#)/
15
- base ||= base_url
16
- relative = URI.encode relative
17
- result = URI.join(base, relative)
18
-
19
- return false unless result.host == base_url.host
20
- result.to_s
21
- end
22
- end
23
- end