url-checker 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/checkurls +6 -0
  3. data/lib/url_checker.rb +83 -0
  4. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 520fdbebbffff313fced72bdff28b582b954c226
4
+ data.tar.gz: cf5b0d0c5d1ffb6446db835312f524c0c5f47036
5
+ SHA512:
6
+ metadata.gz: f7bb7f5c95da1a5e90eba3706f35874dcf6a6c1ef26154ff5b1d66dc06a81f4bd155421b784ab1aafba5764c22cc8ea08f184414f15639e739627d575ce37a90
7
+ data.tar.gz: 98dab8c5b611986b7215e1d1d0bfff84b29a9552823ac48b8d94c7f18688a80cac44680ae7f2433b85288ee7a5c7aa26757f181bb7627650a5fc6f75c755e96a
data/bin/checkurls ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/url_checker'
5
+
6
+ UrlChecker.call
@@ -0,0 +1,83 @@
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
+
34
+ attr_accessor :num_issues, :results
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
73
+
74
+ def write_results
75
+ time = Time.now.strftime('%Y-%m-%d-%H:%M:%S')
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
83
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url-checker
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Kevin McCormack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ description: A Ruby CLI for checking a list of URLs from a CSV file
28
+ email: HarlemSquirrel@gmail.com
29
+ executables:
30
+ - checkurls
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - bin/checkurls
35
+ - lib/url_checker.rb
36
+ homepage: https://github.com/HarlemSquirrel/url-checker
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.4.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.6.10
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: A Ruby CLI for checking a list of URLs from a CSV file
60
+ test_files: []