web_checker 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +26 -0
- data/.rakeTasks +7 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +11 -0
- data/bin/web_checker +6 -0
- data/lib/web_checker/cli.rb +81 -0
- data/lib/web_checker/http.rb +56 -0
- data/lib/web_checker/notifications.rb +65 -0
- data/lib/web_checker/version.rb +3 -0
- data/lib/web_checker/workflow.rb +39 -0
- data/lib/web_checker.rb +5 -0
- data/spec/spec_helper.rb +83 -0
- data/spec/web_checker/http_spec.rb +45 -0
- data/spec/web_checker/notifications_spec.rb +59 -0
- data/spec/web_checker/workflow_spec.rb +31 -0
- data/web_checker.gemspec +34 -0
- metadata +238 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6ec766bbc5f268eaa3fbe28cb5ca67e0a96e75e5
|
4
|
+
data.tar.gz: fc6c5e15d36e1927a5271bf8aafdd2bbc9740b7f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8f7ef4fc9073f0690e3e099dae50fbf01dda218399ac8b51bac6dba9e37251a77b44816aeb1280d10e8dbc5b7a89fb1586f4ba2a905b5849db8290305b917f1a
|
7
|
+
data.tar.gz: 4822b3d62adb470fcb8d850da744a5c019cbf9dc8525cb19c9555fef7262aa1857485d50019c6c1cc78d67c02ef1662d1ba44f7f51a57e4f3d0f16879dee341f
|
data/.gitignore
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
web_checker.iml
|
24
|
+
web_checker.ipr
|
25
|
+
web_checker.iws
|
26
|
+
atlassian-ide-plugin.xml
|
data/.rakeTasks
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<Settings><!--This file was automatically generated by Ruby plugin.
|
3
|
+
You are allowed to:
|
4
|
+
1. Remove rake task
|
5
|
+
2. Add existing rake tasks
|
6
|
+
To add existing rake tasks automatically delete this file and reload the project.
|
7
|
+
--><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build web_checker-0.0.2.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Build and install web_checker-0.0.2.gem into system gems" fullCmd="install" taksId="install" /><RakeTask description="Create tag v0.0.2 and build and push web_checker-0.0.2.gem to Rubygems" fullCmd="release" taksId="release" /><RakeTask description="Run RSpec code examples" fullCmd="spec" taksId="spec" /><RakeTask description="" fullCmd="default" taksId="default" /></RakeGroup></Settings>
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
web-checker
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.2
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Vitaliy V. Shopov
|
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,44 @@
|
|
1
|
+
# WebChecker
|
2
|
+
|
3
|
+
A simple service for checking a availability of web resource
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'web_checker'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install web_checker
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
web_checker start --email=Email for notifications --url=Url for monitoring
|
22
|
+
|
23
|
+
Options:
|
24
|
+
--url=Url for monitoring
|
25
|
+
--email=Email for notifications
|
26
|
+
[--delivery-method=Delivery email method]
|
27
|
+
# Default: sendmail
|
28
|
+
[--twilio=Use twilio for SMS notifications], [--no-twilio]
|
29
|
+
[--phone=Mobile phone number for SMS notifications]
|
30
|
+
[--twilio-sid=Twilio sid value]
|
31
|
+
[--twilio-token=Twilio token value]
|
32
|
+
[--twilio-from=Twilio from value]
|
33
|
+
[--threshold=Attempts threshold for notifications]
|
34
|
+
# Default: ["5", "10", "50", "100", "500"]
|
35
|
+
[--refresh-rate=Refresh rate for checking in seconds]
|
36
|
+
# Default: 5
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it ( https://github.com/unitymind/web_checker/fork )
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
# Default directory to look in is `/specs`
|
5
|
+
# Run with `rake spec`
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
7
|
+
task.rspec_opts = ['--format', 'documentation']
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :spec
|
11
|
+
|
data/bin/web_checker
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'web_checker/workflow'
|
3
|
+
require 'eventmachine'
|
4
|
+
|
5
|
+
module WebChecker
|
6
|
+
class CLI < Thor
|
7
|
+
default_task :start
|
8
|
+
|
9
|
+
desc 'start', 'Start the web-checker service'
|
10
|
+
|
11
|
+
method_option :url,
|
12
|
+
type: :string,
|
13
|
+
required: true,
|
14
|
+
banner: 'Url for monitoring'
|
15
|
+
|
16
|
+
method_option :email,
|
17
|
+
type: :string,
|
18
|
+
required: true,
|
19
|
+
banner: 'Email for notifications'
|
20
|
+
|
21
|
+
method_option :delivery_method,
|
22
|
+
type: :string,
|
23
|
+
default: 'sendmail',
|
24
|
+
banner: 'Delivery email method'
|
25
|
+
|
26
|
+
method_option :twilio,
|
27
|
+
type: :boolean,
|
28
|
+
default: false,
|
29
|
+
banner: 'Use twilio for SMS notifications'
|
30
|
+
|
31
|
+
method_option :phone,
|
32
|
+
type: :string,
|
33
|
+
banner: 'Mobile phone number for SMS notifications'
|
34
|
+
|
35
|
+
method_option :twilio_sid,
|
36
|
+
type: :string,
|
37
|
+
banner: 'Twilio sid value'
|
38
|
+
|
39
|
+
method_option :twilio_token,
|
40
|
+
type: :string,
|
41
|
+
banner: 'Twilio token value'
|
42
|
+
|
43
|
+
method_option :twilio_from,
|
44
|
+
type: :string,
|
45
|
+
banner: 'Twilio from value'
|
46
|
+
|
47
|
+
method_option :threshold,
|
48
|
+
type: :array,
|
49
|
+
default: %w{5 10 50 100 500},
|
50
|
+
banner: 'Attempts threshold for notifications'
|
51
|
+
|
52
|
+
method_option :refresh_rate,
|
53
|
+
type: :numeric,
|
54
|
+
default: 5,
|
55
|
+
banner: 'Refresh rate for checking in seconds'
|
56
|
+
|
57
|
+
def start
|
58
|
+
trap("INT") { self.stop }
|
59
|
+
|
60
|
+
begin
|
61
|
+
@checker = WebChecker::Workflow.new(options)
|
62
|
+
rescue WebChecker::NotHttpURIError, WebChecker::InvalidHostError => e
|
63
|
+
puts "--url error. #{e.message}"
|
64
|
+
exit
|
65
|
+
end
|
66
|
+
|
67
|
+
EventMachine.run do
|
68
|
+
@checker.run
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
no_commands do
|
73
|
+
def stop
|
74
|
+
@checker.cancel
|
75
|
+
EventMachine.stop
|
76
|
+
exit
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'whois'
|
3
|
+
|
4
|
+
module WebChecker
|
5
|
+
|
6
|
+
class NotHttpURIError < StandardError; end
|
7
|
+
class InvalidHostError < StandardError; end
|
8
|
+
|
9
|
+
class Http
|
10
|
+
def initialize(uri_str, limit = 10)
|
11
|
+
@uri_str, @limit = uri_str, limit
|
12
|
+
@uri_str = "http://#{@uri_str}" unless @uri_str.include?('://')
|
13
|
+
@uri = URI(@uri_str)
|
14
|
+
raise NotHttpURIError, "Not HTTP URI: #{@uri_str}" unless @uri.scheme.include?('http')
|
15
|
+
raise InvalidHostError, "Domain or host does not exists: #{@uri.host}" unless domain_exists?(@uri.host)
|
16
|
+
end
|
17
|
+
|
18
|
+
def accessible?
|
19
|
+
return false if @limit == 0
|
20
|
+
begin
|
21
|
+
Net::HTTP.start(@uri.host, @uri.port, :use_ssl => @uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
|
22
|
+
request = Net::HTTP::Get.new(@uri.request_uri)
|
23
|
+
response = http.request(request)
|
24
|
+
case response
|
25
|
+
when Net::HTTPSuccess then true
|
26
|
+
when Net::HTTPClientError then false
|
27
|
+
when Net::HTTPServerError then false
|
28
|
+
when Net::HTTPRedirection
|
29
|
+
location = response['location']
|
30
|
+
unless location.include?('://')
|
31
|
+
host_with_protocol = @uri_str[/^[^\:]+\:\/\/[^\/]+/, 0]
|
32
|
+
location = host_with_protocol + location
|
33
|
+
end
|
34
|
+
self.class.new(location, @limit - 1).accessible?
|
35
|
+
else
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
rescue SocketError
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def domain_exists?(domain)
|
46
|
+
begin
|
47
|
+
Whois::Client.new.lookup(domain)
|
48
|
+
rescue Whois::ServerNotFound
|
49
|
+
return false
|
50
|
+
rescue Whois::ConnectionError
|
51
|
+
return true
|
52
|
+
end
|
53
|
+
true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'twilio-ruby'
|
3
|
+
require 'mail'
|
4
|
+
require 'socket'
|
5
|
+
require 'mail'
|
6
|
+
|
7
|
+
module WebChecker
|
8
|
+
class Notifications
|
9
|
+
include Singleton
|
10
|
+
attr_reader :twilio
|
11
|
+
|
12
|
+
def setup(options)
|
13
|
+
@options = options
|
14
|
+
@twilio = @options['twilio'] ? Twilio::REST::Client.new(@options['twilio_sid'], @options['twilio_token']) : nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def notify
|
18
|
+
message = "Your url #{@options['url']} is active now"
|
19
|
+
results = []
|
20
|
+
results.push(notify_via_email(message))
|
21
|
+
results.push(notify_via_twilio(message)) if @twilio
|
22
|
+
results.include?(false) ? false : true
|
23
|
+
end
|
24
|
+
|
25
|
+
def notify_broken(count)
|
26
|
+
results = []
|
27
|
+
results.push(notify_via_email(build_broken_message(count)))
|
28
|
+
results.push(notify_via_twilio(build_broken_message(count))) if @twilio
|
29
|
+
results.include?(false) ? false : true
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def notify_via_email(message)
|
34
|
+
begin
|
35
|
+
mail = Mail.new
|
36
|
+
mail.body = message
|
37
|
+
mail.from = "no-reply@#{Socket.gethostname}"
|
38
|
+
mail.to = @options['email']
|
39
|
+
mail.subject = "[Web checker] notification about #{@options['url']}"
|
40
|
+
mail.delivery_method(@options['delivery_method'].to_sym) if @options.has_key?('delivery_method')
|
41
|
+
mail.deliver
|
42
|
+
true
|
43
|
+
rescue StandardError
|
44
|
+
false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def notify_via_twilio(message)
|
49
|
+
begin
|
50
|
+
@twilio.account.messages.create({
|
51
|
+
:from => @options['twilio_from'],
|
52
|
+
:to => @options['phone'],
|
53
|
+
:body => message
|
54
|
+
})
|
55
|
+
true
|
56
|
+
rescue StandardError
|
57
|
+
false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def build_broken_message(count)
|
62
|
+
"Your url #{@options['url']} is broken after #{count} attempts"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'web_checker/http'
|
2
|
+
require 'web_checker/notifications'
|
3
|
+
require 'eventmachine'
|
4
|
+
|
5
|
+
module WebChecker
|
6
|
+
class Workflow
|
7
|
+
attr_reader :checker, :attempts
|
8
|
+
|
9
|
+
def initialize(options)
|
10
|
+
@options = options.dup
|
11
|
+
WebChecker::Notifications.instance.setup(@options)
|
12
|
+
@options['threshold'] = @options['threshold'].map(&:to_i)
|
13
|
+
@checker = WebChecker::Http.new(options['url'])
|
14
|
+
@attempts = 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
if @checker.accessible?
|
19
|
+
WebChecker::Notifications.instance.notify if @attempts > 0
|
20
|
+
@attempts = 0
|
21
|
+
else
|
22
|
+
@attempts += 1
|
23
|
+
if @options['threshold'].include?(@attempts)
|
24
|
+
WebChecker::Notifications.instance.notify_broken(@attempts)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
if EventMachine.reactor_running?
|
29
|
+
@timer = EventMachine.add_timer(@options['refresh_rate']) do
|
30
|
+
run
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def cancel
|
36
|
+
EventMachine.cancel_timer(@timer) if self.instance_variable_defined?(:@timer) && EventMachine.reactor_running?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/web_checker.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
5
|
+
|
6
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
7
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
8
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
9
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
10
|
+
#
|
11
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
12
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
13
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
14
|
+
# individual file that may not need all of that loaded. Instead, make a
|
15
|
+
# separate helper file that requires this one and then use it only in the specs
|
16
|
+
# that actually need it.
|
17
|
+
#
|
18
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
19
|
+
# users commonly want.
|
20
|
+
#
|
21
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
22
|
+
RSpec.configure do |config|
|
23
|
+
# The settings below are suggested to provide a good initial experience
|
24
|
+
# with RSpec, but feel free to customize to your heart's content.
|
25
|
+
=begin
|
26
|
+
# These two settings work together to allow you to limit a spec run
|
27
|
+
# to individual examples or groups you care about by tagging them with
|
28
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
29
|
+
# get run.
|
30
|
+
config.filter_run :focus
|
31
|
+
config.run_all_when_everything_filtered = true
|
32
|
+
|
33
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
34
|
+
# file, and it's useful to allow more verbose output when running an
|
35
|
+
# individual spec file.
|
36
|
+
if config.files_to_run.one?
|
37
|
+
# Use the documentation formatter for detailed output,
|
38
|
+
# unless a formatter has already been configured
|
39
|
+
# (e.g. via a command-line flag).
|
40
|
+
config.default_formatter = 'doc'
|
41
|
+
end
|
42
|
+
|
43
|
+
# Print the 10 slowest examples and example groups at the
|
44
|
+
# end of the spec run, to help surface which specs are running
|
45
|
+
# particularly slow.
|
46
|
+
config.profile_examples = 10
|
47
|
+
|
48
|
+
# Run specs in random order to surface order dependencies. If you find an
|
49
|
+
# order dependency and want to debug it, you can fix the order by providing
|
50
|
+
# the seed, which is printed after each run.
|
51
|
+
# --seed 1234
|
52
|
+
config.order = :random
|
53
|
+
|
54
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
55
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
56
|
+
# test failures related to randomization by passing the same `--seed` value
|
57
|
+
# as the one that triggered the failure.
|
58
|
+
Kernel.srand config.seed
|
59
|
+
|
60
|
+
# rspec-expectations config goes here. You can use an alternate
|
61
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
62
|
+
# assertions if you prefer.
|
63
|
+
config.expect_with :rspec do |expectations|
|
64
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
65
|
+
# For more details, see:
|
66
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
67
|
+
expectations.syntax = :expect
|
68
|
+
end
|
69
|
+
|
70
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
71
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
72
|
+
config.mock_with :rspec do |mocks|
|
73
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
74
|
+
# For more details, see:
|
75
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
76
|
+
mocks.syntax = :expect
|
77
|
+
|
78
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
79
|
+
# a real object. This is generally recommended.
|
80
|
+
mocks.verify_partial_doubles = true
|
81
|
+
end
|
82
|
+
=end
|
83
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'web_checker/http'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
describe WebChecker::Http do
|
5
|
+
|
6
|
+
context 'initialize' do
|
7
|
+
it 'raise WebChecker::NotHttpURIError if uri not http(s)' do
|
8
|
+
expect { WebChecker::Http.new('ftp://yandex.ru') }.to raise_error(WebChecker::NotHttpURIError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'raise WebChecker::InvalidHostError if uri contains invalid host' do
|
12
|
+
expect { WebChecker::Http.new('http://yandex.fake') }.to raise_error(WebChecker::InvalidHostError)
|
13
|
+
expect { WebChecker::Http.new('10.0.1') }.to raise_error(WebChecker::InvalidHostError)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'accessible?' do
|
18
|
+
it 'true for 2xx responses' do
|
19
|
+
expect_any_instance_of(Net::HTTP).to receive(:request).and_return(Net::HTTPSuccess.new('1.1', '200', 'Ok'))
|
20
|
+
expect(WebChecker::Http.new('http://yandex.ru').accessible?).to be_truthy
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'handle redirects correctly' do
|
24
|
+
expect(WebChecker::Http.new('http://yandex.ru', 0).accessible?).to be_falsy
|
25
|
+
expect(WebChecker::Http.new('http://yandex.ru', 1).accessible?).to be_falsy
|
26
|
+
expect(WebChecker::Http.new('http://yandex.ru', 2).accessible?).to be_truthy
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'false for 4xx responses' do
|
30
|
+
expect_any_instance_of(Net::HTTP).to receive(:request).and_return(Net::HTTPClientError.new('1.1', '404', 'Not found'))
|
31
|
+
expect(WebChecker::Http.new('http://yandex.ru/fake').accessible?).to be_falsy
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'false for 5xx responses' do
|
35
|
+
expect_any_instance_of(Net::HTTP).to receive(:request).and_return(Net::HTTPServerError.new('1.1', '500', 'Internal Server Error'))
|
36
|
+
expect(WebChecker::Http.new('http://yandex.ru/fake').accessible?).to be_falsy
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'false if network down' do
|
40
|
+
expect_any_instance_of(Net::HTTP).to receive(:connect).and_raise(SocketError)
|
41
|
+
expect(WebChecker::Http.new('http://yandex.ru').accessible?).to be_falsy
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'web_checker/notifications'
|
2
|
+
require 'mail'
|
3
|
+
|
4
|
+
describe WebChecker::Notifications do
|
5
|
+
Mail.defaults do
|
6
|
+
delivery_method :test
|
7
|
+
end
|
8
|
+
|
9
|
+
options =
|
10
|
+
{
|
11
|
+
"twilio"=>true,
|
12
|
+
"url"=>"http://yandex.ru/",
|
13
|
+
"email"=>"devops@cleawing.com",
|
14
|
+
"phone"=>ENV['TWILIO_PHONE'],
|
15
|
+
"twilio_sid"=>ENV['TWILIO_SID'],
|
16
|
+
"twilio_token"=>ENV['TWILIO_TOKEN'],
|
17
|
+
"twilio_from"=>ENV['TWILIO_FROM']
|
18
|
+
}
|
19
|
+
|
20
|
+
context 'singleton' do
|
21
|
+
it 'no public new' do
|
22
|
+
expect { WebChecker::Notifications.new }.to raise_error(NoMethodError)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'return the same instance' do
|
26
|
+
expect(WebChecker::Notifications.instance).to be WebChecker::Notifications.instance
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'setup' do
|
31
|
+
it 'check setup of twilio client' do
|
32
|
+
WebChecker::Notifications.instance.setup(options)
|
33
|
+
expect(WebChecker::Notifications.instance.instance_variable_get(:@twilio)).to be_truthy
|
34
|
+
WebChecker::Notifications.instance.setup(options.merge({"twilio" => false}))
|
35
|
+
expect(WebChecker::Notifications.instance.instance_variable_get(:@twilio)).to be_falsey
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'notify' do
|
41
|
+
before(:all) do
|
42
|
+
WebChecker::Notifications.instance.setup(options)
|
43
|
+
end
|
44
|
+
|
45
|
+
before(:each) do
|
46
|
+
Mail::TestMailer.deliveries.clear
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should sent mail and sms' do
|
50
|
+
expect(Mail::TestMailer.deliveries.length).to eql 0
|
51
|
+
expect(WebChecker::Notifications.instance.twilio.account.messages).to receive(:create).twice
|
52
|
+
expect(WebChecker::Notifications.instance.notify).to be_truthy
|
53
|
+
expect(Mail::TestMailer.deliveries.length).to eql 1
|
54
|
+
expect(WebChecker::Notifications.instance.notify_broken(5)).to be_truthy
|
55
|
+
expect(Mail::TestMailer.deliveries.length).to eql 2
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'web_checker/workflow'
|
2
|
+
require 'web_checker/notifications'
|
3
|
+
|
4
|
+
describe WebChecker::Workflow do
|
5
|
+
|
6
|
+
options = {
|
7
|
+
"twilio" => false,
|
8
|
+
"url" => "http://yandex.ru/",
|
9
|
+
"email" => "devops@cleawing.com",
|
10
|
+
"threshold"=>["5", "10", "50", "100", "500"]
|
11
|
+
}
|
12
|
+
|
13
|
+
|
14
|
+
let(:workflow) { WebChecker::Workflow.new(options) }
|
15
|
+
|
16
|
+
|
17
|
+
it 'call notify_broken 5 times if totally 404' do
|
18
|
+
max_attempts = options['threshold'].map(&:to_i).max
|
19
|
+
expect(workflow.checker).to receive(:accessible?).and_return(false).exactly(max_attempts + 100).times
|
20
|
+
expect(WebChecker::Notifications.instance).to receive(:notify_broken).exactly(options['threshold'].size).times
|
21
|
+
(max_attempts + 100).times { workflow.run }
|
22
|
+
expect(workflow.attempts).to eql (max_attempts + 100)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'call notify once if attempts > 0 and url is OK' do
|
26
|
+
workflow.instance_variable_set(:@attempts, 1)
|
27
|
+
expect(workflow.checker).to receive(:accessible?).and_return(true).twice
|
28
|
+
expect(WebChecker::Notifications.instance).to receive(:notify).once
|
29
|
+
2.times { workflow.run }
|
30
|
+
end
|
31
|
+
end
|
data/web_checker.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'web_checker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'web_checker'
|
8
|
+
spec.version = WebChecker::VERSION
|
9
|
+
spec.authors = ['Vitaliy V. Shopov']
|
10
|
+
spec.email = ['vitaliy.shopov@cleawing.com']
|
11
|
+
spec.summary = %q{A simple service for checking a availability of web resource}
|
12
|
+
spec.description = %q{A simple service for checking a availability of web resource}
|
13
|
+
spec.homepage = 'https://github.com/unitymind/web_checker'
|
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.add_dependency 'thor'
|
22
|
+
spec.add_dependency 'whois'
|
23
|
+
spec.add_dependency 'twilio-ruby'
|
24
|
+
spec.add_dependency 'eventmachine'
|
25
|
+
spec.add_dependency 'mail'
|
26
|
+
spec.add_dependency 'notify'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
29
|
+
spec.add_development_dependency 'rake'
|
30
|
+
spec.add_development_dependency 'pry'
|
31
|
+
spec.add_development_dependency 'pry-remote'
|
32
|
+
spec.add_development_dependency 'pry-nav'
|
33
|
+
spec.add_development_dependency 'rspec'
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,238 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: web_checker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vitaliy V. Shopov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-01 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: whois
|
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: twilio-ruby
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: eventmachine
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
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: mail
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
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: notify
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
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: bundler
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.6'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.6'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rake
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: pry-remote
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: pry-nav
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: rspec
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
description: A simple service for checking a availability of web resource
|
182
|
+
email:
|
183
|
+
- vitaliy.shopov@cleawing.com
|
184
|
+
executables:
|
185
|
+
- web_checker
|
186
|
+
extensions: []
|
187
|
+
extra_rdoc_files: []
|
188
|
+
files:
|
189
|
+
- ".gitignore"
|
190
|
+
- ".rakeTasks"
|
191
|
+
- ".rspec"
|
192
|
+
- ".ruby-gemset"
|
193
|
+
- ".ruby-version"
|
194
|
+
- Gemfile
|
195
|
+
- LICENSE.txt
|
196
|
+
- README.md
|
197
|
+
- Rakefile
|
198
|
+
- bin/web_checker
|
199
|
+
- lib/web_checker.rb
|
200
|
+
- lib/web_checker/cli.rb
|
201
|
+
- lib/web_checker/http.rb
|
202
|
+
- lib/web_checker/notifications.rb
|
203
|
+
- lib/web_checker/version.rb
|
204
|
+
- lib/web_checker/workflow.rb
|
205
|
+
- spec/spec_helper.rb
|
206
|
+
- spec/web_checker/http_spec.rb
|
207
|
+
- spec/web_checker/notifications_spec.rb
|
208
|
+
- spec/web_checker/workflow_spec.rb
|
209
|
+
- web_checker.gemspec
|
210
|
+
homepage: https://github.com/unitymind/web_checker
|
211
|
+
licenses:
|
212
|
+
- MIT
|
213
|
+
metadata: {}
|
214
|
+
post_install_message:
|
215
|
+
rdoc_options: []
|
216
|
+
require_paths:
|
217
|
+
- lib
|
218
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
219
|
+
requirements:
|
220
|
+
- - ">="
|
221
|
+
- !ruby/object:Gem::Version
|
222
|
+
version: '0'
|
223
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
224
|
+
requirements:
|
225
|
+
- - ">="
|
226
|
+
- !ruby/object:Gem::Version
|
227
|
+
version: '0'
|
228
|
+
requirements: []
|
229
|
+
rubyforge_project:
|
230
|
+
rubygems_version: 2.2.2
|
231
|
+
signing_key:
|
232
|
+
specification_version: 4
|
233
|
+
summary: A simple service for checking a availability of web resource
|
234
|
+
test_files:
|
235
|
+
- spec/spec_helper.rb
|
236
|
+
- spec/web_checker/http_spec.rb
|
237
|
+
- spec/web_checker/notifications_spec.rb
|
238
|
+
- spec/web_checker/workflow_spec.rb
|