ping-my-site 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env_schema +1 -0
- data/.gitignore +1 -0
- data/.rvmrc +2 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +33 -0
- data/README.md +59 -0
- data/bin/ping-my-site +10 -0
- data/lib/notifier.rb +26 -0
- data/lib/ping-my-site.rb +28 -0
- data/lib/pinger.rb +45 -0
- data/spec/ping-my-site.rb +21 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 33f3e581efc5cf1ac95fde19e698ca1e5548dfa7
|
4
|
+
data.tar.gz: f40a0044ef2567824d7ddf4be94d2ba0aa9e1d8d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0bf1d58c5da7a04259a570a1bdedd1e0e1d6c46d99b96e8e1147bd79df779db044ce607e9c9cbf547b56175fa8aede13a0cd30acdfa991f0ec4cab2bebf2bec0
|
7
|
+
data.tar.gz: 15e3d017ae2c2c3577fdf335e8e61b133f6697d6706b836ecf442e866b5d8316bea8331e536581ebabcefeae7c6b8e898ef09e38ff380bc7c87bd448b2865700
|
data/.env_schema
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
SLACK_WEBHOOK_URL:
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.env
|
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
curb (0.9.0)
|
5
|
+
diff-lcs (1.2.5)
|
6
|
+
dotenv (2.0.2)
|
7
|
+
dotenv-schema (0.0.1)
|
8
|
+
dotenv
|
9
|
+
rspec (3.4.0)
|
10
|
+
rspec-core (~> 3.4.0)
|
11
|
+
rspec-expectations (~> 3.4.0)
|
12
|
+
rspec-mocks (~> 3.4.0)
|
13
|
+
rspec-core (3.4.1)
|
14
|
+
rspec-support (~> 3.4.0)
|
15
|
+
rspec-expectations (3.4.0)
|
16
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
17
|
+
rspec-support (~> 3.4.0)
|
18
|
+
rspec-mocks (3.4.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.4.0)
|
21
|
+
rspec-support (3.4.1)
|
22
|
+
slack-notifier (1.5.1)
|
23
|
+
thor (0.19.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
curb (~> 0.9.0)
|
30
|
+
dotenv-schema (~> 0.0.1)
|
31
|
+
rspec
|
32
|
+
slack-notifier (~> 1.5, >= 1.5.1)
|
33
|
+
thor (~> 0.19.1)
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Ping My Site
|
2
|
+
|
3
|
+
A simple CLI Pingdom-like written in ruby.
|
4
|
+
|
5
|
+
## How to use
|
6
|
+
|
7
|
+
### Git clone
|
8
|
+
|
9
|
+
> git clone https://github.com/cedricbousmanne/PingMySite.git
|
10
|
+
|
11
|
+
### Install required Gem
|
12
|
+
|
13
|
+
> bundle
|
14
|
+
|
15
|
+
### dotenv (.env)
|
16
|
+
|
17
|
+
Copy the ```.env_schema```file to ```.env```and fill it.
|
18
|
+
> cp .env_schema .env
|
19
|
+
|
20
|
+
### Run
|
21
|
+
|
22
|
+
> bin/ping-my-site ping http://google.com --expected-status-code 200 --follow-location false
|
23
|
+
|
24
|
+
### Options
|
25
|
+
|
26
|
+
* ```--expected-status-code XXX``` : define the expected HTTP status code (default: ```200```)
|
27
|
+
* ```--follow-location true```: allow CURL to follow HTTP redirection (default: false)
|
28
|
+
|
29
|
+
## Usecase
|
30
|
+
|
31
|
+
### Test HTTP status
|
32
|
+
|
33
|
+
> bin/ping-my-site ping http://google.com --expected-status-code 200
|
34
|
+
|
35
|
+
### Test HTTP redirection
|
36
|
+
|
37
|
+
> bin/ping-my-site ping http://mysite.be --expected-status-code 301 --follow-location false --expected-redirect-url http://www.mysite.be
|
38
|
+
|
39
|
+
## Troubleshooting
|
40
|
+
|
41
|
+
### Dotenv::Schema::ValidationError
|
42
|
+
|
43
|
+
Something is missing in your ```.env```file. Compare it to ```.env_schema```and be sure every constant is defined.
|
44
|
+
|
45
|
+
## Use with crontab
|
46
|
+
|
47
|
+
> * * * * * /bin/bash -l -c 'cd /path/to/ping-my-site/ && bundle exec bin/ping-my-site ping http://www.6x7asbl.be --expected-status-code 200 --follow-location false'
|
48
|
+
|
49
|
+
## Technologies
|
50
|
+
|
51
|
+
* [Dotenv Schema](https://github.com/mirakui/dotenv-schema)
|
52
|
+
* [Thor](http://whatisthor.com/)
|
53
|
+
* [Curb](https://github.com/taf2/curb)
|
54
|
+
* [Slack-Notifier](https://github.com/stevenosloan/slack-notifier)
|
55
|
+
|
56
|
+
## Foot Note
|
57
|
+
|
58
|
+
Coded with ♥, [Ruby](https://www.ruby-lang.org/) and [Vim](http://www.vim.org/). Made in Belgium.
|
59
|
+
|
data/bin/ping-my-site
ADDED
data/lib/notifier.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class Notifier
|
2
|
+
|
3
|
+
def initialize(message)
|
4
|
+
@message = message
|
5
|
+
end
|
6
|
+
|
7
|
+
def notifier
|
8
|
+
@notifier ||= Slack::Notifier.new ENV['SLACK_WEBHOOK_URL']
|
9
|
+
@notifier.username = "Ping My Site"
|
10
|
+
@notifier
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform
|
14
|
+
unless $TESTING
|
15
|
+
notification = notifier.ping(@message)
|
16
|
+
|
17
|
+
if notification.response.code.to_i == 200
|
18
|
+
puts "Successfully notified to Slack"
|
19
|
+
else
|
20
|
+
puts "Something went wrong when trying to notify Slack"
|
21
|
+
end
|
22
|
+
else
|
23
|
+
return "Successfully notified to Slack"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/ping-my-site.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#require "ping-my-site/version"
|
2
|
+
require "dotenv-schema"
|
3
|
+
require "thor"
|
4
|
+
require "curb"
|
5
|
+
require "slack-notifier"
|
6
|
+
require "notifier"
|
7
|
+
require "pinger"
|
8
|
+
|
9
|
+
$TESTING = false
|
10
|
+
|
11
|
+
module PingMySite
|
12
|
+
class Cli < Thor
|
13
|
+
option :follow_location, type: :boolean, default: false, desc: "Allow CURL to follow HTTP redirection"
|
14
|
+
option :expected_status_code, type: :numeric, default: 200, desc: "A valid HTTP status code"
|
15
|
+
option :expected_redirect_url, type: :string, desc: "A valid redirection url"
|
16
|
+
|
17
|
+
desc "ping URL", "ping to URL"
|
18
|
+
def ping(url)
|
19
|
+
pinger = Pinger.new
|
20
|
+
pinger.url = url
|
21
|
+
pinger.expected_status_code = options[:expected_status_code]
|
22
|
+
pinger.expected_redirect_url = options[:expected_redirect_url]
|
23
|
+
pinger.follow_location = options[:follow_location]
|
24
|
+
pinger.perform
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/lib/pinger.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
class Pinger
|
2
|
+
|
3
|
+
attr_accessor :url,
|
4
|
+
:expected_status_code,
|
5
|
+
:expected_redirect_url,
|
6
|
+
:follow_location
|
7
|
+
|
8
|
+
def perform
|
9
|
+
if response_code.to_i == @expected_status_code
|
10
|
+
# Everything's fine
|
11
|
+
puts "Sucessfully ping #{url} with expected status code #{@expected_status_code}"
|
12
|
+
if @expected_status_code == 301 and @expected_redirect_url
|
13
|
+
if redirect_url == @expected_redirect_url
|
14
|
+
# Everything's fine
|
15
|
+
puts "Sucessfully redirect #{url} to #{@expected_redirect_url} with status #{response_code}"
|
16
|
+
else
|
17
|
+
message = "Unable to redirect #{url} to #{@expected_redirect_url} with expected status code #{@expected_status_code} - Received location #{redirect_url} with #{response_code} instead"
|
18
|
+
puts "Something went wrong, we must notify someone"
|
19
|
+
Notifier.new(message).perform
|
20
|
+
end
|
21
|
+
end
|
22
|
+
else
|
23
|
+
message = "Unable to ping #{url} with expected status code #{@expected_status_code} - Received #{response_code} instead"
|
24
|
+
puts "Something went wrong, we must notify someone"
|
25
|
+
Notifier.new(message).perform
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def response_code
|
31
|
+
request.response_code
|
32
|
+
end
|
33
|
+
|
34
|
+
def redirect_url
|
35
|
+
request.redirect_url
|
36
|
+
end
|
37
|
+
|
38
|
+
def request
|
39
|
+
@request ||= Curl::Easy.new(@url) do |curl|
|
40
|
+
curl.follow_location = @follow_location
|
41
|
+
end
|
42
|
+
@request.perform
|
43
|
+
@request
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require './lib/ping-my-site'
|
2
|
+
$TESTING = true # Override $TESTING - this line must stay below the ```require '.lib/ping-my-site'``
|
3
|
+
|
4
|
+
describe PingMySite do
|
5
|
+
|
6
|
+
let(:shell){ Thor::Base.shell.new }
|
7
|
+
|
8
|
+
describe "output" do
|
9
|
+
# Dummy test
|
10
|
+
specify { expect { print('foo') }.to output('foo').to_stdout }
|
11
|
+
|
12
|
+
# HTTP 200 successful test
|
13
|
+
specify { expect { PingMySite.start(%w[ping http://www.greenlab-coworking.com], shell: shell) }.to output("Sucessfully ping http://www.greenlab-coworking.com with expected status code 200\n").to_stdout }
|
14
|
+
|
15
|
+
# HTTP 404 unsuccessful test
|
16
|
+
specify { expect { PingMySite.start(%w[ping http://www.greenlab-coworking.com --expected-status-code 404], shell: shell) }.to output("Something went wrong, we must notify someone\n").to_stdout }
|
17
|
+
|
18
|
+
# HTTP 301 successful test
|
19
|
+
specify { expect { PingMySite.start(%w[ping http://13pixels.be --expected-status-code 301 --follow-location false --expected-redirect-url http://www.13pixels.be], shell: shell) }.to output("Sucessfully ping http://13pixels.be with expected status code 301\nSucessfully redirect http://13pixels.be to http://www.13pixels.be with status 301\n").to_stdout }
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ping-my-site
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cédric Bousmanne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ''
|
14
|
+
email:
|
15
|
+
- i@cedricbousmanne.com
|
16
|
+
executables:
|
17
|
+
- ping-my-site
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".env_schema"
|
22
|
+
- ".gitignore"
|
23
|
+
- ".rvmrc"
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- README.md
|
27
|
+
- bin/ping-my-site
|
28
|
+
- lib/notifier.rb
|
29
|
+
- lib/ping-my-site.rb
|
30
|
+
- lib/pinger.rb
|
31
|
+
- spec/ping-my-site.rb
|
32
|
+
homepage: http://cedric.io
|
33
|
+
licenses: []
|
34
|
+
metadata: {}
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 2.4.5
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: ''
|
55
|
+
test_files:
|
56
|
+
- spec/ping-my-site.rb
|