http_status_checker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2a62701d6a1e337a4c0e935b21b4d473a3e9638
4
+ data.tar.gz: 8481f5d62baa23090ca2102bdcf98a05a692ccd4
5
+ SHA512:
6
+ metadata.gz: 18bc5c1833f1ebc27da79ee1069c0ba700c362c390f561441c921334ab30cd530642ba6f90117f46119e3b25f85542f734e6d6fe2b9ab9a61564e34098245260
7
+ data.tar.gz: 62916752aa4b3f17a5fb051fccd473b14dd6df0ed9dc1972bcc46ceacf6321cbeb6092691bb61aedf0ad10fce20ca1ed469dcaf038122fe11d4c4866e8e18e0b
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.iml
15
+ mkmf.log
16
+ /vendor/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.5
6
+ bundler_args: --jobs=2
7
+ script: bundle exec rspec
8
+ branches:
9
+ only:
10
+ - master
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in scouter.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ gem 'codeclimate-test-reporter', require: false
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 morizyun
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # HttpStatusChecker
2
+
3
+ Easily Checking http status by http header with Multi-threaded
4
+
5
+ ## Features
6
+
7
+ * Get http status by only http header
8
+ * A threaded (fast) per host name
9
+ * Warnings for links that redirect to valid links
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'http_status_checker'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install http_status_checker
26
+
27
+ ## Usage Command Line
28
+
29
+ $ http_status http://morizyun.github.io
30
+
31
+ ## Usage Ruby Program
32
+
33
+ require 'http_status_checker'
34
+
35
+ HttpStatusChecker.check 'http://morizyun.github.io
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/morizyun/http_status_checker/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ #coding: utf-8
3
+
4
+ require 'thor'
5
+ require 'http_status_checker'
6
+ require 'pry'
7
+
8
+ class Command < Thor
9
+ default_command :http_status_checker
10
+
11
+ # コマンドラインがちゃんとうごくようにする
12
+ desc 'http_status_checker', 'Checking whether can access url'
13
+ option :url, :type => :string, :aliases => :'-u', :desc => 'url for checking'
14
+ def http_status_checker
15
+ results = HttpStatusChecker.check(options[:url])
16
+ results.each do |result|
17
+ result.each do |url, res|
18
+ puts "url: #{url}"
19
+ puts "response: #{res}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ Command.start
@@ -0,0 +1,71 @@
1
+ module HttpStatusChecker
2
+ module Base
3
+ THREAD_LIMIT = 5.freeze
4
+ REDIRECT_MAX = 5.freeze
5
+ RETRY_MAX = 1.freeze
6
+ WAIT_SEC = 1.freeze
7
+
8
+ class InvalidResponseError < StandardError; end
9
+ class InvalidRedirectError < StandardError; end
10
+
11
+ def check(urls)
12
+ results = []
13
+
14
+ host_hash = to_host_hash(urls)
15
+ Parallel.each(host_hash, in_threads: host_hash.keys.count) do |_, urls|
16
+ urls.map.with_index(1) do |url, idx|
17
+ results << get_response(url)
18
+ sleep(WAIT_SEC) if urls.count != idx
19
+ end
20
+ end
21
+
22
+ return results
23
+ end
24
+
25
+ private
26
+
27
+ # TODO codeをちゃんと取得できるようにする
28
+ def get_response(url, redirect_url = nil, redirect_count = 0, retry_count = 0, result = nil)
29
+ result = get_header(redirect_url || url)
30
+ if result.is_a?(Net::HTTPRedirection) # redirect
31
+ redirect_url = result['location']
32
+ raise InvalidRedirectError if redirect_url.nil? || redirect_count > REDIRECT_MAX
33
+ get_response(url, redirect_url, redirect_count + 1, 0)
34
+ elsif result.is_a?(Net::HTTPOK)
35
+ binding.pry
36
+ { url => { code: result.code, is_alive: true, redirect_url: redirect_url } }
37
+ else
38
+ raise InvalidResponseError, "Unknown class #{result.class} : #{result.to_s}"
39
+ end
40
+ rescue => e
41
+ if retry_count < RETRY_MAX
42
+ retry_count += 1
43
+ retry
44
+ end
45
+ { url => { code: result.code, is_alive: false, error: e.message } }
46
+ end
47
+
48
+ def to_host_hash(urls)
49
+ urls = to_array(urls)
50
+ raise ArgumentError, "#{urls} is not String and Array" unless urls.is_a?(Array)
51
+
52
+ host_hash = {}
53
+ urls.each do |url|
54
+ host = URI.parse(url).host.to_sym
55
+ host_hash[host] = host_hash[host].nil? ? [url] : host_hash[host] << url
56
+ end
57
+ return host_hash
58
+ end
59
+
60
+ def to_array(item)
61
+ case item.class.to_s
62
+ when 'Array'
63
+ item
64
+ when 'String'
65
+ [item]
66
+ else
67
+ nil
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,9 @@
1
+ module HttpStatusChecker
2
+ module Connection
3
+ def get_header(url)
4
+ uri = URI.parse(url)
5
+ http = Net::HTTP.new(uri.host)
6
+ http.head(uri.request_uri)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module HttpStatusChecker
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'net/http'
2
+ require 'parallel'
3
+
4
+ require 'http_status_checker/version'
5
+
6
+ require 'http_status_checker/connection'
7
+ require 'http_status_checker/base'
8
+
9
+ module HttpStatusChecker
10
+ extend HttpStatusChecker::Connection
11
+ extend HttpStatusChecker::Base
12
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe HttpStatusChecker::Connection do
4
+ let!(:valid_url) { 'http://www.yahoo.co.jp/' }
5
+ let!(:redirect_url) { 'http://yahoo.co.jp/' }
6
+ let!(:morizyun_404) { 'http://morizyun.github.io/404/' }
7
+
8
+ describe '.get_header' do
9
+ context 'when get http valid url' do
10
+ it 'returns is_alive = true, redirect = nil, error = nil' do
11
+ response = HttpStatusChecker.check(valid_url)
12
+ expect(response.first[valid_url][:is_alive]).to eq(true)
13
+ expect(response.first[valid_url][:redirect_url]).to be_nil
14
+ expect(response.first[valid_url][:error]).to be_nil
15
+ end
16
+ end
17
+
18
+ context 'when get http redirect url' do
19
+ it 'returns is_alive = true, redirect = valid_url, error = nil' do
20
+ response = HttpStatusChecker.check(redirect_url)
21
+ expect(response.first[redirect_url][:is_alive]).to eq(true)
22
+ expect(response.first[redirect_url][:redirect_url]).to eq(valid_url)
23
+ expect(response.first[redirect_url][:error]).to be_nil
24
+ end
25
+ end
26
+
27
+ context 'when get http invalid url' do
28
+ let!(:invalid_url) { 'http://www.nothing-dummy.com/' }
29
+ it 'returns is_alive = false, redirect = nil, error present' do
30
+ response = HttpStatusChecker.check(invalid_url)
31
+ expect(response.first[invalid_url][:is_alive]).to eq(false)
32
+ expect(response.first[invalid_url][:redirect_url]).to be_nil
33
+ expect(response.first[invalid_url][:error]).not_to be_nil
34
+ end
35
+ end
36
+
37
+ context 'when get 404 error' do
38
+ let!(:invalid_url) { 'http://morizyun.github.io/404/' }
39
+ 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
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '.to_host_hash' do
49
+ context 'when post 2urls' do
50
+ it 'returns host with urls array in hash' do
51
+ host_hash = HttpStatusChecker.__send__(:to_host_hash, [valid_url, redirect_url])
52
+ expect(host_hash['www.yahoo.co.jp'.to_sym]).to eq([valid_url])
53
+ expect(host_hash['yahoo.co.jp'.to_sym]).to eq([redirect_url])
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe HttpStatusChecker::Connection do
4
+ describe '.get_header' do
5
+ let!(:valid_url) { 'http://www.yahoo.co.jp/' }
6
+ let!(:redirect_url) { 'http://yahoo.co.jp/' }
7
+
8
+ context 'when get http valid url' do
9
+ it 'return Net::HTTPOK response' do
10
+ response = HttpStatusChecker.get_header(valid_url)
11
+ expect(response.is_a?(Net::HTTPOK)).to be == true
12
+ end
13
+ end
14
+
15
+ context 'when get http redirect url' do
16
+ it 'return Net::HTTPRedirection response' do
17
+ response = HttpStatusChecker.get_header(redirect_url)
18
+ expect(response.is_a?(Net::HTTPRedirection)).to be == true
19
+ expect(response['location']).to eq(valid_url)
20
+ end
21
+ end
22
+
23
+ context 'when get http invalid url' do
24
+ let!(:invalid_url) { 'http://www.nothing-dummy.com/' }
25
+ it 'raise SocketError' do
26
+ expect{ HttpStatusChecker.get_header(invalid_url) }.to raise_error(SocketError)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe HttpStatusChecker do
4
+ it 'has a version number' do
5
+ expect(HttpStatusChecker::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ if ENV['CI']
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+ require 'codeclimate-test-reporter'
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
+ Coveralls::SimpleCov::Formatter,
7
+ CodeClimate::TestReporter::Formatter
8
+ ]
9
+ SimpleCov.start 'test_frameworks'
10
+ CodeClimate::TestReporter.start
11
+ end
12
+
13
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
14
+ require 'http_status_checker'
15
+ require 'rspec'
16
+ require 'fakeweb'
17
+ require 'pry'
18
+
19
+ def fixture(path)
20
+ File.read("#{File.dirname(__FILE__)}/fixtures/#{path}")
21
+ end
22
+
23
+ def stub_get(path, fixture_path, options={})
24
+ opts = {
25
+ :body => fixture(fixture_path),
26
+ :content_type => 'application/json; charset=utf-8'
27
+ }.merge(options)
28
+ FakeWeb.register_uri(:get, "#{path}", opts)
29
+ end
30
+
31
+ RSpec.configure do |config|
32
+ config.order = 'random'
33
+ end
data/url_alive.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'http_status_checker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'http_status_checker'
8
+ spec.version = HttpStatusChecker::VERSION
9
+ spec.authors = ['morizyun']
10
+ spec.email = ['merii.ken@gmail.com']
11
+ spec.summary = %q{Easily Checking http status by http header with Multi-threaded}
12
+ spec.description = %q{Easily Checking http status by http header with Multi-threaded}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+ spec.add_dependency 'thor'
23
+ spec.add_dependency 'parallel'
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'fakeweb'
28
+ spec.add_development_dependency 'rspec'
29
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_status_checker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - morizyun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: parallel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: fakeweb
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Easily Checking http status by http header with Multi-threaded
112
+ email:
113
+ - merii.ken@gmail.com
114
+ executables:
115
+ - http_status_checker
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/http_status_checker
127
+ - lib/http_status_checker.rb
128
+ - lib/http_status_checker/base.rb
129
+ - lib/http_status_checker/connection.rb
130
+ - lib/http_status_checker/version.rb
131
+ - spec/http_status_checker/base_spec.rb
132
+ - spec/http_status_checker/connection_spec.rb
133
+ - spec/http_status_cheker_spec.rb
134
+ - spec/spec_helper.rb
135
+ - url_alive.gemspec
136
+ homepage: ''
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: 1.9.3
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.5
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Easily Checking http status by http header with Multi-threaded
160
+ test_files:
161
+ - spec/http_status_checker/base_spec.rb
162
+ - spec/http_status_checker/connection_spec.rb
163
+ - spec/http_status_cheker_spec.rb
164
+ - spec/spec_helper.rb