url-checker 1.0.1 → 1.1.0

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: bc8f608df4641ec27291dca1cc2170b2dde74210
4
- data.tar.gz: 157d02f42cb51fd56272580b93ac5e87b6e3ad2b
3
+ metadata.gz: 5a3976012ff91534b59a8a2ed70550a453602fda
4
+ data.tar.gz: c09fdcd490bc26047cc0d9b86537b0b0b809a9c8
5
5
  SHA512:
6
- metadata.gz: 54c89e9bd0322c92b915c8613ae60a3e65d414b1b349b468691a970ffc3a5e1bc5570fe2775f292da86bcb13305c6dbc6f6fc36c862590b73b34aa2756bc1fdd
7
- data.tar.gz: 792b7708b2652eece3adfb76a354ca8156bd88fdf40178557bec69e7555f088941f8f2aaba5f6e8e3927bb55c63b30b230f5104314656d4a9b09ec50e0520601
6
+ metadata.gz: 537be50b970fcc977e920ed16e25bebf4c30815afa210ba977662daf35f8d45be309c1d9dd3b32cbeb558bb4074631a1ecb42b81134d00f8e731d6ec2ce338b5
7
+ data.tar.gz: c7c17c67f0f671e0c6bd2f23a1aaf2f841c4a00efad18759b24961a92cc203ecf797134bbcfb440d840248d730d844a360a4037ce8ca19b35af472e22bd81d7d
@@ -3,4 +3,4 @@
3
3
 
4
4
  require_relative '../lib/url_checker'
5
5
 
6
- UrlChecker::UrlChecker.call
6
+ UrlChecker::Cli.call
@@ -6,5 +6,7 @@ require 'csv'
6
6
  require 'net/http'
7
7
 
8
8
  require_relative '../lib/url_checker'
9
- require_relative '../lib/url_checker/url_checker'
9
+ require_relative '../lib/url_checker/cli'
10
+ require_relative '../lib/url_checker/rescued_response'
11
+ require_relative '../lib/url_checker/single_checker'
10
12
  require_relative '../lib/url_checker/version'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module UrlChecker
4
4
  # The main class
5
- class UrlChecker
5
+ class Cli
6
6
  BAD_CALL_MSG = 'Please call with one CSV file with URLs in the first column'
7
7
  RESULTS_HEADERS = %w(Response URL).freeze
8
8
 
@@ -33,28 +33,27 @@ module UrlChecker
33
33
  attr_accessor :num_issues, :results
34
34
 
35
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
36
+ response = UrlChecker::SingleChecker.new(url: url).call
37
+ collect_result response
38
+ display_result response
40
39
  end
41
40
 
42
41
  def check_urls_from_csv
43
42
  threads = []
44
43
  CSV.foreach(file_path) do |row|
45
44
  url = row[0]
46
- threads << Thread.new { check_url url } if url.match?(/http/)
45
+ threads << Thread.new { check_url url } if url.match?(/\Ahttp/)
47
46
  end
48
47
  threads.each(&:join)
49
48
  end
50
49
 
51
- def collect_result(response, url)
52
- line = ["#{response.code} #{response.message}", url]
50
+ def collect_result(response)
51
+ line = ["#{response.code} #{response.message}", response.uri.to_s]
53
52
  results << line
54
53
  end
55
54
 
56
- def display_result(response, url)
57
- msg = " #{response.code} #{response.message} #{url}"
55
+ def display_result(response)
56
+ msg = " #{response.code} #{response.message} #{response.uri.to_s}"
58
57
  case response
59
58
  when Net::HTTPSuccess, Net::HTTPRedirection
60
59
  puts msg.green
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UrlChecker
4
+ # Checks a single URL and returns the result
5
+ class RecuedResponse
6
+ attr_reader :code, :message, :uri
7
+
8
+ def initialize(code:, message:, uri:)
9
+ raise TypeError, 'uri is not a URI' unless uri.is_a? URI
10
+ @code = code
11
+ @message = message
12
+ @uri = uri
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UrlChecker
4
+ # Checks a single URL and returns the result
5
+ class SingleChecker
6
+ attr_reader :url_string, :uri
7
+
8
+ def initialize(url:)
9
+ @url_string = url
10
+ end
11
+
12
+ def call
13
+ check_url
14
+ end
15
+
16
+ private
17
+
18
+ def check_url
19
+ @uri = URI(url_string)
20
+ return invalid_uri_response unless valid_url?
21
+ response = Net::HTTP.get_response uri
22
+ response.uri ||= uri # sometimes the uri is not set
23
+ response
24
+ rescue Errno::ECONNREFUSED => e
25
+ rescued_response('Connection refused', e.message)
26
+ rescue Errno::EINVAL => e
27
+ rescued_response('Invalid argument', e.message)
28
+ end
29
+
30
+ def invalid_uri_response
31
+ rescued_response('Invalid URL', 'URL must begin with http')
32
+ end
33
+
34
+ def rescued_response(code, message)
35
+ UrlChecker::RecuedResponse.new(
36
+ code: code,
37
+ message: message,
38
+ uri: uri
39
+ )
40
+ end
41
+
42
+ def valid_url?
43
+ uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UrlChecker
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url-checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McCormack
@@ -35,7 +35,9 @@ files:
35
35
  - bin/checkurls
36
36
  - config/environment.rb
37
37
  - lib/url_checker.rb
38
- - lib/url_checker/url_checker.rb
38
+ - lib/url_checker/cli.rb
39
+ - lib/url_checker/rescued_response.rb
40
+ - lib/url_checker/single_checker.rb
39
41
  - lib/url_checker/version.rb
40
42
  homepage: https://github.com/HarlemSquirrel/url-checker
41
43
  licenses: