http_status_checker 0.0.3 → 0.0.4

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: d9acb8d925d1ee90d4005641d43fd5fa5494299d
4
- data.tar.gz: 479235114bfff51eb94e279f187941685507f38f
3
+ metadata.gz: 05033eced256b186dbc266e52867b44d9d74d053
4
+ data.tar.gz: c584834e0dfb5c2bb4d319b5c8ddd6c77bb45535
5
5
  SHA512:
6
- metadata.gz: c122dc69546548ec6b3183ae84134ab36b13ee65182931ab031efd1b998641a65cd42e0c69a9641ff979f13fd2d0fed77894b7e6e42bd498c52003929d32cc7e
7
- data.tar.gz: 29eeb1438142591460edb954ffd2653552e155e6c2d0ac50e1a4f81b099825b085eb7fed400e011dbee83f5f97bc20faa04e41220750bbc30fbe00f5e67a3af2
6
+ metadata.gz: b481d34c22d0540621537f7d972fa647d514381933ad87cc1bb03d4acd4097130f7d305afe3167cc68021d770e9feedbc589325a78f0305dd7ba27be25e4fbd2
7
+ data.tar.gz: 1e1d63f8509d18b32d698bdc172b1d14442bd9d531f0834de0095d449d6f7d37401229efe254f3aedea1d3f119ece61151de1db9b878596e4adf414c84b37f65
@@ -3,10 +3,10 @@ require 'parallel'
3
3
 
4
4
  require 'http_status_checker/version'
5
5
 
6
+ require 'http_status_checker/error'
6
7
  require 'http_status_checker/connection'
7
8
  require 'http_status_checker/base'
8
9
 
9
10
  module HttpStatusChecker
10
- extend HttpStatusChecker::Connection
11
11
  extend HttpStatusChecker::Base
12
12
  end
@@ -4,9 +4,6 @@ module HttpStatusChecker
4
4
  REDIRECT_MAX = 5.freeze
5
5
  RETRY_MAX = 1.freeze
6
6
 
7
- class InvalidResponseError < StandardError; end
8
- class InvalidRedirectError < StandardError; end
9
-
10
7
  def check(urls, wait_sec = 1)
11
8
  results = []
12
9
 
@@ -23,13 +20,13 @@ module HttpStatusChecker
23
20
 
24
21
  private
25
22
 
26
- def get_response(url, redirect_url = nil, redirect_count = 0, retry_count = 0, result = nil)
27
- result = get_header(redirect_url || url)
28
- if result.is_a?(Net::HTTPRedirection) # redirect
29
- redirect_url = result['location']
30
- raise InvalidRedirectError if redirect_url.nil? || redirect_count > REDIRECT_MAX
31
- get_response(url, redirect_url, redirect_count + 1, 0)
32
- elsif result.is_a?(Net::HTTPOK)
23
+ def get_response(url, redirect_url = nil, redirect_count = 0, retry_count = 0)
24
+ result = HttpStatusChecker::Connection.get_header(redirect_url || url)
25
+ location_url = result['location']
26
+ if !location_url.nil? && (redirect_url || url) != location_url
27
+ raise InvalidRedirectError if redirect_count > REDIRECT_MAX
28
+ get_response(url, location_url, redirect_count + 1, 0)
29
+ elsif result.code =~ /^(2|3)[0-9]{2}$/
33
30
  { url => { code: result.code, is_alive: true, redirect_url: redirect_url } }
34
31
  else
35
32
  raise InvalidResponseError, "Unknown class #{result.class} : #{result.to_s}"
@@ -1,6 +1,6 @@
1
1
  module HttpStatusChecker
2
2
  module Connection
3
- def get_header(url)
3
+ def self.get_header(url)
4
4
  uri = URI.parse(url)
5
5
  http = Net::HTTP.new(uri.host)
6
6
  http.head(uri.request_uri)
@@ -0,0 +1,7 @@
1
+ module HttpStatusChecker
2
+
3
+ class InvalidResponseError < StandardError; end
4
+
5
+ class InvalidRedirectError < StandardError; end
6
+
7
+ end
@@ -1,3 +1,3 @@
1
1
  module HttpStatusChecker
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -14,6 +14,17 @@ describe HttpStatusChecker::Connection do
14
14
  end
15
15
  end
16
16
 
17
+ context 'when set valid url with invalid http status' do
18
+ let!(:atnd_http_url) { 'http://atnd.org/events/14386' }
19
+ let!(:atnd_https_url) { 'https://atnd.org/events/14386' }
20
+ it 'returns is_alive = true, redirect = nil, error = nil' do
21
+ response = HttpStatusChecker.check(atnd_http_url)
22
+ expect(response.first[atnd_http_url][:is_alive]).to eq(true)
23
+ expect(response.first[atnd_http_url][:redirect_url]).to eq(atnd_https_url)
24
+ expect(response.first[atnd_http_url][:error]).to be_nil
25
+ end
26
+ end
27
+
17
28
  context 'when set 2 urls' do
18
29
  let!(:morizyun_css) { 'http://morizyun.github.io/blog/css3-html-front-coding-book-review/' }
19
30
  let!(:morizyun_js) { 'http://morizyun.github.io/blog/javascript-learning-tech-yourself_01/' }
@@ -4,26 +4,37 @@ describe HttpStatusChecker::Connection do
4
4
  describe '.get_header' do
5
5
  let!(:valid_url) { 'http://www.yahoo.co.jp/' }
6
6
  let!(:redirect_url) { 'http://yahoo.co.jp/' }
7
+ let!(:atnd_http_url) { 'http://atnd.org/events/14386' }
8
+ let!(:atnd_https_url) { 'https://atnd.org/events/14386' }
7
9
 
8
10
  context 'when get http valid url' do
9
11
  it 'return Net::HTTPOK response' do
10
- response = HttpStatusChecker.get_header(valid_url)
12
+ response = HttpStatusChecker::Connection.get_header(valid_url)
11
13
  expect(response.is_a?(Net::HTTPOK)).to be == true
14
+ expect(response['location']).to be_nil
12
15
  end
13
16
  end
14
17
 
15
18
  context 'when get http redirect url' do
16
19
  it 'return Net::HTTPRedirection response' do
17
- response = HttpStatusChecker.get_header(redirect_url)
20
+ response = HttpStatusChecker::Connection.get_header(redirect_url)
18
21
  expect(response.is_a?(Net::HTTPRedirection)).to be == true
19
22
  expect(response['location']).to eq(valid_url)
20
23
  end
21
24
  end
22
25
 
26
+ context 'when get invalid http status(atnd.org)' do
27
+ it 'return Net::HTTPRedirection response' do
28
+ response = HttpStatusChecker::Connection.get_header(atnd_https_url)
29
+ expect(response.is_a?(Net::HTTPRedirection)).to be == true
30
+ expect(response['location']).to eq(atnd_https_url)
31
+ end
32
+ end
33
+
23
34
  context 'when get http invalid url' do
24
35
  let!(:invalid_url) { 'http://www.nothing-dummy.com/' }
25
36
  it 'raise SocketError' do
26
- expect{ HttpStatusChecker.get_header(invalid_url) }.to raise_error(SocketError)
37
+ expect{ HttpStatusChecker::Connection.get_header(invalid_url) }.to raise_error(SocketError)
27
38
  end
28
39
  end
29
40
  end
data/spec/spec_helper.rb CHANGED
@@ -14,7 +14,6 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
14
14
  require 'http_status_checker'
15
15
  require 'rspec'
16
16
  require 'fakeweb'
17
- require 'pry'
18
17
 
19
18
  def fixture(path)
20
19
  File.read("#{File.dirname(__FILE__)}/fixtures/#{path}")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_status_checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - morizyun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-25 00:00:00.000000000 Z
11
+ date: 2014-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -113,6 +113,7 @@ files:
113
113
  - lib/http_status_checker.rb
114
114
  - lib/http_status_checker/base.rb
115
115
  - lib/http_status_checker/connection.rb
116
+ - lib/http_status_checker/error.rb
116
117
  - lib/http_status_checker/version.rb
117
118
  - spec/http_status_checker/base_spec.rb
118
119
  - spec/http_status_checker/connection_spec.rb