kleos_test 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7cdb0602baff97def0b5d1325c7d59221d966871
4
+ data.tar.gz: e203bfa4a9c52005eb6278e75362026d3affeb5d
5
+ SHA512:
6
+ metadata.gz: b9a81643e49f042a013da0936c9dce670d888dee3909d903f5c37624e297e4c72e073b1a7cc7eccf4eff0a807051258ba656175090ea8746c5c43668d3dbc23f
7
+ data.tar.gz: f97a3373ee5241a0399a9a42a068e7783a93895c207ead7769b91d89ebeb79fb649406a770e226d02e362b34bc877a2449ef8f52476c2a5ad1e2d006e4f58847
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'pry'
6
+ gem 'rest-client'
7
+ gem 'nokogiri'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Akkuzin Maxim
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # KleosTest
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kleos_test`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kleos_test'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kleos_test
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kleos_test.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kleos_test"
5
+ require "pry"
6
+
7
+ Pry.start
data/bin/kleos_test ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pry'
4
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
5
+ require 'kleos_test'
6
+
7
+ puts "*** Check for inside links ***\n\n"
8
+ begin
9
+ collector = KleosTest::LinksCollector.new
10
+ collector.get_new_links
11
+ end while KleosTest::LinksCollector.unverified_inside_links.size > 0
12
+
13
+ puts "\n\n*** Check for outside links ***\n\n"
14
+ collector = KleosTest::LinksCollector.new
15
+ collector.verify_outside_links
16
+
17
+ broken_inside = KleosTest::LinksCollector.invalid_inside_links
18
+ broken_outside = KleosTest::LinksCollector.invalid_outside_links
19
+
20
+ result = <<EOF
21
+ *** Result: ***
22
+ Total count of inside links: #{KleosTest::LinksCollector.inside_links.count}
23
+ - valid: #{KleosTest::LinksCollector.valid_inside_links.count}
24
+ - invalid: #{broken_inside.count}
25
+ Total count of outside links: #{KleosTest::LinksCollector.outside_links.count}
26
+ - valid: #{KleosTest::LinksCollector.valid_outside_links.count}
27
+ - invalid: #{broken_outside.count}
28
+ ---------------
29
+ Broken links:
30
+ EOF
31
+
32
+ if broken_inside.empty? and broken_outside.empty?
33
+ result << "none.\n"
34
+ elsif broken_inside.empty?
35
+ result << "- inside: none\n"
36
+ result << broken_outside.join("\n")
37
+ elsif broken_outside.empty?
38
+ result << broken_inside.join("\n")
39
+ result << "- outside: none\n"
40
+ end
41
+
42
+ puts result
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'kleos_test/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kleos_test"
7
+ spec.version = KleosTest::VERSION
8
+ spec.authors = ["Akkuzin Maxim"]
9
+ spec.email = ["akkmaxon2307@gmail.com"]
10
+
11
+ spec.summary = "Test task from kleos.ru"
12
+ spec.description = "Looking for broken links on kleos.ru"
13
+ spec.homepage = "https://github.com/makkuzin/kleos_test"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.bindir = "bin"
18
+ spec.executables = ["kleos_test"]
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.13"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,124 @@
1
+ class KleosTest::LinksCollector
2
+ BASE_ADDRESS = 'https://www.kleos.ru'
3
+
4
+ @@unverified_inside_links = ['/']
5
+ @@unverified_outside_links = []
6
+ @@verified_links = []
7
+ @@inside_links = { valid: [], invalid: [] }
8
+ @@outside_links = { valid: [], invalid: [] }
9
+ @@inside_downloads = 0
10
+
11
+ def initialize
12
+ @target = get_target_link
13
+ end
14
+
15
+ def get_target_link
16
+ @@unverified_inside_links.empty? ? '/' : @@unverified_inside_links.shift
17
+ end
18
+
19
+ def get_new_links
20
+ page, response_code = download_inside_webpage
21
+ if response_code == 200
22
+ @@inside_links[:valid] << @target
23
+ @@verified_links << @target
24
+ links = extract_links(page)
25
+ refill_unverified_links(links)
26
+ else
27
+ @@inside_links[:invalid] << @target
28
+ @@verified_links << @target
29
+ end
30
+ end
31
+
32
+ def verify_outside_links
33
+ @@unverified_outside_links.each_with_index do |link, i|
34
+ code = download_outside_webpage(link, i + 1)
35
+ if code == 200
36
+ @@outside_links[:valid] << link
37
+ else
38
+ @@outside_links[:invalid] << link
39
+ end
40
+ end
41
+ end
42
+
43
+ def download_inside_webpage
44
+ address = @target.include?('http') ? @target : BASE_ADDRESS + @target
45
+ @@inside_downloads += 1
46
+ print "Downloading (#{@@inside_downloads}|#{@@unverified_inside_links.size})\
47
+ #{address}..."
48
+ response = RestClient.get(address)
49
+ puts "OK"
50
+ [response.body, response.code]
51
+ rescue RestClient::ExceptionWithResponse => e
52
+ puts "ERROR"
53
+ [e.response.body, e.response.code]
54
+ rescue URI::InvalidURIError
55
+ puts "ERROR"
56
+ puts "BAD URI"
57
+ ['fake body', 1]
58
+ end
59
+
60
+ def download_outside_webpage(address, counter)
61
+ print "Downloading (#{counter}|#{@@unverified_outside_links.size - counter})\
62
+ #{address}..."
63
+ response = RestClient.get(address)
64
+ puts "OK"
65
+ response.code
66
+ rescue RestClient::ExceptionWithResponse => e
67
+ puts "ERROR"
68
+ e.response.code
69
+ rescue URI::InvalidURIError
70
+ puts "ERROR"
71
+ puts "BAD URI"
72
+ 1
73
+ end
74
+
75
+ def extract_links(page)
76
+ query = '//a[@href!=""]'
77
+ query += '[not(starts-with(@href, "javascript:void(0)"))]'
78
+ query += '[not(starts-with(@href, "#"))]'
79
+ query += '[not(starts-with(@href, "mailto"))]'
80
+ Nokogiri::HTML(page).xpath(query).map { |link| link.attribute('href').value }
81
+ end
82
+
83
+ def refill_unverified_links(links)
84
+ regexp = /^(\/|\?|https?:\/\/(www\.)?kleos\.ru)(?!\/forum)/
85
+ @@unverified_inside_links.concat(links.select do |link|
86
+ link.match(regexp) && !(
87
+ @@unverified_inside_links.include?(link) ||
88
+ @@verified_links.include?(link))
89
+ end.uniq)
90
+
91
+ @@unverified_outside_links.concat(
92
+ links.reject { |link| link.match(regexp) }).uniq!
93
+ end
94
+
95
+ class << self
96
+ def unverified_inside_links
97
+ @@unverified_inside_links
98
+ end
99
+
100
+ def inside_links
101
+ @@inside_links[:valid] | @@inside_links[:invalid]
102
+ end
103
+
104
+ def valid_inside_links
105
+ @@inside_links[:valid]
106
+ end
107
+
108
+ def invalid_inside_links
109
+ @@inside_links[:invalid]
110
+ end
111
+
112
+ def outside_links
113
+ @@outside_links[:valid] | @@outside_links[:invalid]
114
+ end
115
+
116
+ def valid_outside_links
117
+ @@outside_links[:valid]
118
+ end
119
+
120
+ def invalid_outside_links
121
+ @@outside_links[:invalid]
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,3 @@
1
+ module KleosTest
2
+ VERSION = "0.1.0"
3
+ end
data/lib/kleos_test.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rest-client'
2
+ require 'nokogiri'
3
+ require 'kleos_test/version'
4
+ require 'kleos_test/links_collector'
5
+
6
+ module KleosTest
7
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kleos_test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akkuzin Maxim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Looking for broken links on kleos.ru
42
+ email:
43
+ - akkmaxon2307@gmail.com
44
+ executables:
45
+ - kleos_test
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/kleos_test
56
+ - bin/setup
57
+ - kleos_test.gemspec
58
+ - lib/kleos_test.rb
59
+ - lib/kleos_test/links_collector.rb
60
+ - lib/kleos_test/version.rb
61
+ homepage: https://github.com/makkuzin/kleos_test
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.5.1
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Test task from kleos.ru
85
+ test_files: []