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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2a62701d6a1e337a4c0e935b21b4d473a3e9638
4
- data.tar.gz: 8481f5d62baa23090ca2102bdcf98a05a692ccd4
3
+ metadata.gz: 248d0ed100cde2cf0fd888d29ec17b1864cf8dd6
4
+ data.tar.gz: 94d908e18651aae85fed0cb94474bc25fa2d4c24
5
5
  SHA512:
6
- metadata.gz: 18bc5c1833f1ebc27da79ee1069c0ba700c362c390f561441c921334ab30cd530642ba6f90117f46119e3b25f85542f734e6d6fe2b9ab9a61564e34098245260
7
- data.tar.gz: 62916752aa4b3f17a5fb051fccd473b14dd6df0ed9dc1972bcc46ceacf6321cbeb6092691bb61aedf0ad10fce20ca1ed469dcaf038122fe11d4c4866e8e18e0b
6
+ metadata.gz: 812c325af69997a1d11881df6b9b0f9617c1a095243f4c467d93355f7caa92890fd700419c84892bb2c6b16da745dc91a09baa58e2de4bbf7d01ca88a2052ff1
7
+ data.tar.gz: 0663bc343c58fcc3079b526e4e756e7be4e6561a26ea4ce92fd42d474d426e6d3825f98ca7bee37d6d7b6db815dc5a8171c71bd0edb94d105c24887ba869b6fa
data/README.md CHANGED
@@ -32,7 +32,7 @@ Or install it yourself as:
32
32
 
33
33
  require 'http_status_checker'
34
34
 
35
- HttpStatusChecker.check 'http://morizyun.github.io
35
+ HttpStatusChecker.check ['http://morizyun.github.io', 'http://www.yahoo.co.jp']
36
36
 
37
37
  ## Contributing
38
38
 
@@ -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(WAIT_SEC) if urls.count != idx
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
- { url => { code: result.code, is_alive: false, error: e.message } }
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)
@@ -1,3 +1,3 @@
1
1
  module HttpStatusChecker
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -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 '.get_header' do
9
- context 'when get http valid url' do
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 get http redirect url' do
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 get http invalid url' do
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 get 404 error' do
38
- let!(:invalid_url) { 'http://morizyun.github.io/404/' }
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(invalid_url)
41
- expect(response.first[invalid_url][:is_alive]).to eq(false)
42
- expect(response.first[invalid_url][:redirect_url]).to be_nil
43
- expect(response.first[invalid_url][:error]).not_to be_nil
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
- end
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
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_status_checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - morizyun