http_status_checker 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +1 -1
- data/lib/http_status_checker/base.rb +4 -6
- data/lib/http_status_checker/version.rb +1 -1
- data/spec/http_status_checker/base_spec.rb +55 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 248d0ed100cde2cf0fd888d29ec17b1864cf8dd6
|
4
|
+
data.tar.gz: 94d908e18651aae85fed0cb94474bc25fa2d4c24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 812c325af69997a1d11881df6b9b0f9617c1a095243f4c467d93355f7caa92890fd700419c84892bb2c6b16da745dc91a09baa58e2de4bbf7d01ca88a2052ff1
|
7
|
+
data.tar.gz: 0663bc343c58fcc3079b526e4e756e7be4e6561a26ea4ce92fd42d474d426e6d3825f98ca7bee37d6d7b6db815dc5a8171c71bd0edb94d105c24887ba869b6fa
|
data/README.md
CHANGED
@@ -3,19 +3,18 @@ module HttpStatusChecker
|
|
3
3
|
THREAD_LIMIT = 5.freeze
|
4
4
|
REDIRECT_MAX = 5.freeze
|
5
5
|
RETRY_MAX = 1.freeze
|
6
|
-
WAIT_SEC = 1.freeze
|
7
6
|
|
8
7
|
class InvalidResponseError < StandardError; end
|
9
8
|
class InvalidRedirectError < StandardError; end
|
10
9
|
|
11
|
-
def check(urls)
|
10
|
+
def check(urls, wait_sec = 1)
|
12
11
|
results = []
|
13
12
|
|
14
13
|
host_hash = to_host_hash(urls)
|
15
14
|
Parallel.each(host_hash, in_threads: host_hash.keys.count) do |_, urls|
|
16
15
|
urls.map.with_index(1) do |url, idx|
|
17
16
|
results << get_response(url)
|
18
|
-
sleep(
|
17
|
+
sleep(wait_sec) if urls.count != idx
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
@@ -24,7 +23,6 @@ module HttpStatusChecker
|
|
24
23
|
|
25
24
|
private
|
26
25
|
|
27
|
-
# TODO codeをちゃんと取得できるようにする
|
28
26
|
def get_response(url, redirect_url = nil, redirect_count = 0, retry_count = 0, result = nil)
|
29
27
|
result = get_header(redirect_url || url)
|
30
28
|
if result.is_a?(Net::HTTPRedirection) # redirect
|
@@ -32,7 +30,6 @@ module HttpStatusChecker
|
|
32
30
|
raise InvalidRedirectError if redirect_url.nil? || redirect_count > REDIRECT_MAX
|
33
31
|
get_response(url, redirect_url, redirect_count + 1, 0)
|
34
32
|
elsif result.is_a?(Net::HTTPOK)
|
35
|
-
binding.pry
|
36
33
|
{ url => { code: result.code, is_alive: true, redirect_url: redirect_url } }
|
37
34
|
else
|
38
35
|
raise InvalidResponseError, "Unknown class #{result.class} : #{result.to_s}"
|
@@ -42,7 +39,8 @@ module HttpStatusChecker
|
|
42
39
|
retry_count += 1
|
43
40
|
retry
|
44
41
|
end
|
45
|
-
|
42
|
+
code = result ? result.code : nil
|
43
|
+
{ url => { code: code, is_alive: false, error: e.message } }
|
46
44
|
end
|
47
45
|
|
48
46
|
def to_host_hash(urls)
|
@@ -3,10 +3,9 @@ require 'spec_helper'
|
|
3
3
|
describe HttpStatusChecker::Connection do
|
4
4
|
let!(:valid_url) { 'http://www.yahoo.co.jp/' }
|
5
5
|
let!(:redirect_url) { 'http://yahoo.co.jp/' }
|
6
|
-
let!(:morizyun_404) { 'http://morizyun.github.io/404/' }
|
7
6
|
|
8
|
-
describe '.
|
9
|
-
context 'when
|
7
|
+
describe '.check' do
|
8
|
+
context 'when set valid url' do
|
10
9
|
it 'returns is_alive = true, redirect = nil, error = nil' do
|
11
10
|
response = HttpStatusChecker.check(valid_url)
|
12
11
|
expect(response.first[valid_url][:is_alive]).to eq(true)
|
@@ -15,7 +14,37 @@ describe HttpStatusChecker::Connection do
|
|
15
14
|
end
|
16
15
|
end
|
17
16
|
|
18
|
-
context 'when
|
17
|
+
context 'when set 2 urls' do
|
18
|
+
let!(:morizyun_css) { 'http://morizyun.github.io/blog/css3-html-front-coding-book-review/' }
|
19
|
+
let!(:morizyun_js) { 'http://morizyun.github.io/blog/javascript-learning-tech-yourself_01/' }
|
20
|
+
it 'returns is_alive = true, error = nil' do
|
21
|
+
response, time = measure_time do
|
22
|
+
HttpStatusChecker.check([morizyun_css, morizyun_js])
|
23
|
+
end
|
24
|
+
expect(parse(response, morizyun_css)[:is_alive]).to eq(true)
|
25
|
+
expect(parse(response, morizyun_css)[:error]).to be_nil
|
26
|
+
expect(parse(response, morizyun_js)[:is_alive]).to eq(true)
|
27
|
+
expect(parse(response, morizyun_js)[:error]).to be_nil
|
28
|
+
expect(time).to be >= 1.0
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when set 2 urls & wait_sec = 2' do
|
33
|
+
let!(:morizyun_css) { 'http://morizyun.github.io/blog/css3-html-front-coding-book-review/' }
|
34
|
+
let!(:morizyun_js) { 'http://morizyun.github.io/blog/javascript-learning-tech-yourself_01/' }
|
35
|
+
it 'returns is_alive = true, error = nil' do
|
36
|
+
response, time = measure_time do
|
37
|
+
HttpStatusChecker.check([morizyun_css, morizyun_js], wait_sec = 2)
|
38
|
+
end
|
39
|
+
expect(parse(response, morizyun_css)[:is_alive]).to eq(true)
|
40
|
+
expect(parse(response, morizyun_css)[:error]).to be_nil
|
41
|
+
expect(parse(response, morizyun_js)[:is_alive]).to eq(true)
|
42
|
+
expect(parse(response, morizyun_js)[:error]).to be_nil
|
43
|
+
expect(time).to be >= 2.0
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when set http redirect url' do
|
19
48
|
it 'returns is_alive = true, redirect = valid_url, error = nil' do
|
20
49
|
response = HttpStatusChecker.check(redirect_url)
|
21
50
|
expect(response.first[redirect_url][:is_alive]).to eq(true)
|
@@ -24,7 +53,7 @@ describe HttpStatusChecker::Connection do
|
|
24
53
|
end
|
25
54
|
end
|
26
55
|
|
27
|
-
context 'when
|
56
|
+
context 'when set http invalid url' do
|
28
57
|
let!(:invalid_url) { 'http://www.nothing-dummy.com/' }
|
29
58
|
it 'returns is_alive = false, redirect = nil, error present' do
|
30
59
|
response = HttpStatusChecker.check(invalid_url)
|
@@ -34,13 +63,13 @@ describe HttpStatusChecker::Connection do
|
|
34
63
|
end
|
35
64
|
end
|
36
65
|
|
37
|
-
context 'when
|
38
|
-
let!(:
|
66
|
+
context 'when set 404 error' do
|
67
|
+
let!(:morizyun_404) { 'http://morizyun.github.io/404/' }
|
39
68
|
it 'returns is_alive = false, redirect = nil, error present' do
|
40
|
-
response = HttpStatusChecker.check(
|
41
|
-
expect(response.first[
|
42
|
-
expect(response.first[
|
43
|
-
expect(response.first[
|
69
|
+
response = HttpStatusChecker.check(morizyun_404)
|
70
|
+
expect(response.first[morizyun_404][:is_alive]).to eq(false)
|
71
|
+
expect(response.first[morizyun_404][:redirect_url]).to be_nil
|
72
|
+
expect(response.first[morizyun_404][:error]).not_to be_nil
|
44
73
|
end
|
45
74
|
end
|
46
75
|
end
|
@@ -54,4 +83,18 @@ describe HttpStatusChecker::Connection do
|
|
54
83
|
end
|
55
84
|
end
|
56
85
|
end
|
57
|
-
|
86
|
+
|
87
|
+
def parse(response, search_url)
|
88
|
+
response.each do |hash|
|
89
|
+
return hash.values.first if hash.keys.first == search_url
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def measure_time(&block)
|
94
|
+
start_at = Time.now
|
95
|
+
result = block.call
|
96
|
+
finish_at = Time.now
|
97
|
+
return [result, finish_at - start_at]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|