url-checker 1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -0
- data/bin/checkurls +1 -1
- data/config/environment.rb +10 -0
- data/lib/url_checker.rb +3 -79
- data/lib/url_checker/url_checker.rb +83 -0
- data/lib/url_checker/version.rb +5 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc8f608df4641ec27291dca1cc2170b2dde74210
|
4
|
+
data.tar.gz: 157d02f42cb51fd56272580b93ac5e87b6e3ad2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54c89e9bd0322c92b915c8613ae60a3e65d414b1b349b468691a970ffc3a5e1bc5570fe2775f292da86bcb13305c6dbc6f6fc36c862590b73b34aa2756bc1fdd
|
7
|
+
data.tar.gz: 792b7708b2652eece3adfb76a354ca8156bd88fdf40178557bec69e7555f088941f8f2aaba5f6e8e3927bb55c63b30b230f5104314656d4a9b09ec50e0520601
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/HarlemSquirrel/url-checker/badges/gpa.svg)](https://codeclimate.com/github/HarlemSquirrel/url-checker)
|
2
|
+
|
3
|
+
# URL Checker
|
4
|
+
|
5
|
+
A Ruby CLI for checking a list of URLs from a CSV file. Checkout the [RubGems page](https://rubygems.org/gems/url-checker).
|
6
|
+
|
7
|
+
## Getting started
|
8
|
+
|
9
|
+
### Install and run from RubyGems
|
10
|
+
|
11
|
+
```sh
|
12
|
+
$ gem install url-checker
|
13
|
+
$ checkurls path/to/file.csv
|
14
|
+
```
|
15
|
+
|
16
|
+
### Install and run manually
|
17
|
+
|
18
|
+
Clone the repo and bundle
|
19
|
+
|
20
|
+
```sh
|
21
|
+
$ git clone https://github.com/HarlemSquirrel/url-checker.git
|
22
|
+
$ bundle install
|
23
|
+
```
|
24
|
+
|
25
|
+
Try the test file
|
26
|
+
|
27
|
+
```sh
|
28
|
+
$ bin/checkurls spec/fixtures/test.csv
|
29
|
+
404 Not Found http://google.com/cheese
|
30
|
+
301 Moved Permanently http://google.com
|
31
|
+
200 OK https://www.google.com
|
32
|
+
3 URLs checked with 1 issue(s).
|
33
|
+
Results saved to spec/fixtures/test_results_2017-02-19-11:44:27.csv
|
34
|
+
```
|
data/bin/checkurls
CHANGED
data/lib/url_checker.rb
CHANGED
@@ -1,83 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require_relative '../config/environment'
|
3
|
-
|
4
|
-
# The main class
|
5
|
-
class UrlChecker
|
6
|
-
BAD_CALL_MSG = 'Please call with one CSV file with URLs in the first column'
|
7
|
-
RESULTS_HEADERS = %w(Response URL).freeze
|
8
|
-
|
9
|
-
attr_reader :file_path, :results_file_path
|
10
|
-
|
11
|
-
def initialize(file_path:)
|
12
|
-
@file_path = file_path
|
13
|
-
end
|
14
|
-
|
15
|
-
def call
|
16
|
-
@results = [RESULTS_HEADERS]
|
17
|
-
@num_issues = 0
|
18
|
-
check_urls_from_csv
|
19
|
-
display_summary
|
20
|
-
write_results
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.call
|
24
|
-
if ARGV.length == 1 && ARGV.first.match?(/\.csv\z/)
|
25
|
-
url_checker = new(file_path: ARGV.first).call
|
26
|
-
else
|
27
|
-
puts BAD_CALL_MSG.red
|
28
|
-
end
|
29
|
-
url_checker
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
2
|
|
34
|
-
|
35
|
-
|
36
|
-
def check_url(url)
|
37
|
-
uri = URI(url)
|
38
|
-
response = Net::HTTP.get_response uri
|
39
|
-
collect_result response, url
|
40
|
-
display_result response, url
|
41
|
-
end
|
42
|
-
|
43
|
-
def check_urls_from_csv
|
44
|
-
threads = []
|
45
|
-
CSV.foreach(file_path) do |row|
|
46
|
-
url = row[0]
|
47
|
-
threads << Thread.new { check_url url } if url.match?(/http/)
|
48
|
-
end
|
49
|
-
threads.each(&:join)
|
50
|
-
end
|
51
|
-
|
52
|
-
def collect_result(response, url)
|
53
|
-
line = ["#{response.code} #{response.message}", url]
|
54
|
-
results << line
|
55
|
-
end
|
56
|
-
|
57
|
-
def display_result(response, url)
|
58
|
-
msg = " #{response.code} #{response.message} #{url}"
|
59
|
-
case response
|
60
|
-
when Net::HTTPSuccess, Net::HTTPRedirection
|
61
|
-
puts msg.green
|
62
|
-
else
|
63
|
-
@num_issues += 1
|
64
|
-
puts msg.red
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def display_summary
|
69
|
-
num_checked = results.length - 1
|
70
|
-
msg = " #{num_checked} URLs checked with #{num_issues} issue(s)."
|
71
|
-
num_issues.positive? ? puts(msg.yellow) : puts(msg.green)
|
72
|
-
end
|
3
|
+
require_relative '../config/environment'
|
73
4
|
|
74
|
-
|
75
|
-
|
76
|
-
@results_file_path = file_path.gsub('.csv', "_results_#{time}.csv")
|
77
|
-
puts " Results saved to #{results_file_path}"
|
78
|
-
CSV.open(results_file_path, 'wb') do |csv|
|
79
|
-
results.each { |r| csv << r }
|
80
|
-
end
|
81
|
-
results_file_path
|
82
|
-
end
|
5
|
+
# The main module
|
6
|
+
module UrlChecker
|
83
7
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UrlChecker
|
4
|
+
# The main class
|
5
|
+
class UrlChecker
|
6
|
+
BAD_CALL_MSG = 'Please call with one CSV file with URLs in the first column'
|
7
|
+
RESULTS_HEADERS = %w(Response URL).freeze
|
8
|
+
|
9
|
+
attr_reader :file_path, :results_file_path
|
10
|
+
|
11
|
+
def initialize(file_path:)
|
12
|
+
@file_path = file_path
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
@results = [RESULTS_HEADERS]
|
17
|
+
@num_issues = 0
|
18
|
+
display_summary Benchmark.measure { check_urls_from_csv }.real
|
19
|
+
write_results
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.call
|
23
|
+
if ARGV.length == 1 && ARGV.first.match?(/\.csv\z/)
|
24
|
+
url_checker = new(file_path: ARGV.first).call
|
25
|
+
else
|
26
|
+
puts BAD_CALL_MSG.red
|
27
|
+
end
|
28
|
+
url_checker
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
attr_accessor :num_issues, :results
|
34
|
+
|
35
|
+
def check_url(url)
|
36
|
+
uri = URI(url)
|
37
|
+
response = Net::HTTP.get_response uri
|
38
|
+
collect_result response, url
|
39
|
+
display_result response, url
|
40
|
+
end
|
41
|
+
|
42
|
+
def check_urls_from_csv
|
43
|
+
threads = []
|
44
|
+
CSV.foreach(file_path) do |row|
|
45
|
+
url = row[0]
|
46
|
+
threads << Thread.new { check_url url } if url.match?(/http/)
|
47
|
+
end
|
48
|
+
threads.each(&:join)
|
49
|
+
end
|
50
|
+
|
51
|
+
def collect_result(response, url)
|
52
|
+
line = ["#{response.code} #{response.message}", url]
|
53
|
+
results << line
|
54
|
+
end
|
55
|
+
|
56
|
+
def display_result(response, url)
|
57
|
+
msg = " #{response.code} #{response.message} #{url}"
|
58
|
+
case response
|
59
|
+
when Net::HTTPSuccess, Net::HTTPRedirection
|
60
|
+
puts msg.green
|
61
|
+
else
|
62
|
+
@num_issues += 1
|
63
|
+
puts msg.red
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def display_summary(run_time)
|
68
|
+
num_checked = results.length - 1
|
69
|
+
msg = " #{num_checked} URLs checked with #{num_issues} issue(s) in #{run_time.round 2} s."
|
70
|
+
num_issues.positive? ? puts(msg.yellow) : puts(msg.green)
|
71
|
+
end
|
72
|
+
|
73
|
+
def write_results
|
74
|
+
time = Time.now.strftime('%Y-%m-%d-%H:%M:%S')
|
75
|
+
@results_file_path = file_path.gsub('.csv', "_results_#{time}.csv")
|
76
|
+
puts " Results saved to #{results_file_path}"
|
77
|
+
CSV.open(results_file_path, 'wb') do |csv|
|
78
|
+
results.each { |r| csv << r }
|
79
|
+
end
|
80
|
+
results_file_path
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: url-checker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McCormack
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -24,15 +24,19 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.8'
|
27
|
-
description: A Ruby CLI for checking a list of URLs from a CSV file
|
27
|
+
description: A Ruby CLI for checking a list of URLs from a CSV file.
|
28
28
|
email: HarlemSquirrel@gmail.com
|
29
29
|
executables:
|
30
30
|
- checkurls
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- README.md
|
34
35
|
- bin/checkurls
|
36
|
+
- config/environment.rb
|
35
37
|
- lib/url_checker.rb
|
38
|
+
- lib/url_checker/url_checker.rb
|
39
|
+
- lib/url_checker/version.rb
|
36
40
|
homepage: https://github.com/HarlemSquirrel/url-checker
|
37
41
|
licenses:
|
38
42
|
- MIT
|
@@ -56,5 +60,5 @@ rubyforge_project:
|
|
56
60
|
rubygems_version: 2.6.10
|
57
61
|
signing_key:
|
58
62
|
specification_version: 4
|
59
|
-
summary:
|
63
|
+
summary: Check URLs from a CSV
|
60
64
|
test_files: []
|