HttpPing 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dortbucukkilo@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in HttpPing.gemspec
6
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ HttpPing (0.1.0)
5
+ thor
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ rake (10.5.0)
12
+ rspec (3.7.0)
13
+ rspec-core (~> 3.7.0)
14
+ rspec-expectations (~> 3.7.0)
15
+ rspec-mocks (~> 3.7.0)
16
+ rspec-core (3.7.1)
17
+ rspec-support (~> 3.7.0)
18
+ rspec-expectations (3.7.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-mocks (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-support (3.7.1)
25
+ thor (0.20.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ HttpPing!
32
+ bundler (~> 1.16)
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 whyarkadas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # HttpPing
2
+
3
+ Welcome to http_ping gem! This gem http ping to given url and return a result. Result also include ping duration. I have used net-ping gem and trim this code for only http ping.
4
+
5
+ In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/http_ping`. To experiment with that code, run `bin/console` for an interactive prompt. Also you can use this gem by cli interface.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'HttpPing'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install http_ping
22
+
23
+ ## Usage
24
+
25
+ Require this gem 'http_ping' and use ping method.
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/http_ping. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the HttpPing project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/http_ping/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "http_ping"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ require_relative '../lib/http_ping'
2
+
3
+ url = 'https://gitlab.com'
4
+
5
+ puts "HTTP Ping to --> " + url
6
+
7
+ h_ping = HttpPing::H_PING.new(url)
8
+ puts "Can ping : " + h_ping.ping?.to_s
9
+ puts "Duration : " + h_ping.duration.to_s
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/HttpPing/cli'
3
+ HttpPing::CLI.start
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "HttpPing/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "HttpPing"
8
+ spec.version = HttpPing::VERSION
9
+ spec.authors = ["whyarkadas"]
10
+ spec.email = ["dortbucukkilo@gmail.com"]
11
+
12
+ spec.summary = "ruby ping gem for only http pings"
13
+ spec.description = "ruby ping gem for only http pings and also returns duration"
14
+ spec.homepage = "https://github.com/whyarkadas/http_ping"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ #end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+
37
+ spec.add_dependency 'thor'
38
+ end
@@ -0,0 +1,14 @@
1
+ require 'thor'
2
+ require_relative '../http_ping'
3
+
4
+
5
+ module HttpPing
6
+ class CLI < Thor
7
+ desc "ping URL", "HTTP ping to given url and return duration "
8
+ def ping(url)
9
+ h_ping = HttpPing::H_PING.new(url)
10
+ puts "Can ping : " + h_ping.ping?.to_s
11
+ puts "Duration : " + h_ping.duration.to_s
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,195 @@
1
+ #require File.join(File.dirname(__FILE__), 'ping')
2
+ require_relative 'ping'
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'uri'
6
+ require 'open-uri'
7
+
8
+ # Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
9
+ # Windows because it (ironically) causes blocking problems.
10
+ unless File::ALT_SEPARATOR or RUBY_VERSION >= "1.9.3"
11
+ require 'resolv-replace'
12
+ end
13
+
14
+ # The Net module serves as a namespace only.
15
+ module HttpPing
16
+
17
+ # The Ping::HTTP class encapsulates methods for HTTP pings.
18
+
19
+ class H_PING < Ping
20
+
21
+ # By default an http ping will follow a redirect and give you the result
22
+ # of the final URI. If this value is set to false, then it will not
23
+ # follow a redirect and will return false immediately on a redirect.
24
+ #
25
+ attr_accessor :follow_redirect
26
+
27
+ # The maximum number of redirects allowed. The default is 5.
28
+ attr_accessor :redirect_limit
29
+
30
+ # The user agent used for the HTTP request. The default is nil.
31
+ attr_accessor :user_agent
32
+
33
+ # OpenSSL certificate verification mode. The default is VERIFY_NONE.
34
+ attr_accessor :ssl_verify_mode
35
+
36
+ # Use GET request instead HEAD. The default is false.
37
+ attr_accessor :get_request
38
+
39
+ # was this ping proxied?
40
+ attr_accessor :proxied
41
+
42
+ # For unsuccessful requests that return a server error, it is
43
+ # useful to know the HTTP status code of the response.
44
+ attr_reader :code
45
+
46
+ attr_reader :duration
47
+
48
+ # Creates and returns a new Ping::HTTP object. The default port is the
49
+ # port associated with the URI or 80. The default timeout is 5 seconds.
50
+ #
51
+ def initialize(uri=nil, port=nil, timeout=5)
52
+ @follow_redirect = true
53
+ @redirect_limit = 5
54
+ @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
55
+ @get_request = false
56
+ @code = nil
57
+ @duration = 0
58
+
59
+
60
+ port ||= URI.parse(uri).port if uri
61
+ port ||= 80
62
+
63
+ @port = port
64
+
65
+ super(uri, port, timeout)
66
+ end
67
+
68
+ # Looks for an HTTP response from the URI passed to the constructor.
69
+ # If the result is a kind of Net::HTTPSuccess then the ping was
70
+ # successful and true is returned. Otherwise, false is returned
71
+ # and the Ping::HTTP#exception method should contain a string
72
+ # indicating what went wrong.
73
+ #
74
+ # If the HTTP#follow_redirect accessor is set to true (which it is
75
+ # by default) and a redirect occurs during the ping, then the
76
+ # HTTP#warning attribute is set to the redirect message, but the
77
+ # return result is still true. If it's set to false then a redirect
78
+ # response is considered a failed ping.
79
+ #
80
+ # If no file or path is specified in the URI, then '/' is assumed.
81
+ # If no scheme is present in the URI, then 'http' is assumed.
82
+ #
83
+ def ping(host = @host)
84
+ super(host)
85
+ bool = false
86
+
87
+ # See https://bugs.ruby-lang.org/issues/8645
88
+ host = "http://#{host}" unless /\A(http(s)?:\/\/)/.match(host)
89
+
90
+ uri = URI.parse(host)
91
+
92
+ # A port provided here via the host argument overrides anything
93
+ # provided in constructor.
94
+ #
95
+ port = URI.split(host)[3] || URI.parse(host).port || @port
96
+ port = port.to_i
97
+
98
+ start_time = Time.now
99
+
100
+ response = do_ping(uri, port)
101
+
102
+ if response.is_a?(Net::HTTPSuccess)
103
+ bool = true
104
+ elsif redirect?(response) # Check code, HTTPRedirection does not always work
105
+ if @follow_redirect
106
+ @warning = response.message
107
+ rlimit = 0
108
+
109
+ while redirect?(response)
110
+ if rlimit >= redirect_limit
111
+ @exception = "Redirect limit exceeded"
112
+ break
113
+ end
114
+ redirect = URI.parse(response['location'])
115
+ port = redirect.port
116
+ redirect = uri + redirect if redirect.relative?
117
+
118
+ start_time = Time.now
119
+ response = do_ping(redirect, port)
120
+ rlimit += 1
121
+ end
122
+
123
+ if response.is_a?(Net::HTTPSuccess)
124
+ bool = true
125
+ else
126
+ @warning = nil
127
+ @exception ||= response.message
128
+ end
129
+
130
+ else
131
+ @exception = response.message
132
+ end
133
+ else
134
+ @exception ||= response.message
135
+ end
136
+
137
+ # There is no duration if the ping failed
138
+ @duration = Time.now - start_time if bool
139
+
140
+ bool
141
+ end
142
+
143
+ alias follow_redirect? follow_redirect
144
+ alias uri host
145
+ alias uri= host=
146
+
147
+ private
148
+
149
+ def redirect?(response)
150
+ response && response.code.to_i >= 300 && response.code.to_i < 400
151
+ end
152
+
153
+ def do_ping(uri, port)
154
+ response = nil
155
+ proxy = uri.find_proxy || URI.parse("")
156
+
157
+ begin
158
+ uri_path = uri.path.empty? ? '/' : uri.path
159
+
160
+ headers = {}
161
+ headers["User-Agent"] = user_agent if user_agent
162
+
163
+ http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new(uri.host, port)
164
+
165
+ http.open_timeout = timeout
166
+ http.read_timeout = timeout
167
+
168
+ @proxied = http.proxy?
169
+
170
+ if @get_request == true
171
+ request = Net::HTTP::Get.new(uri_path)
172
+ else
173
+ request = Net::HTTP::Head.new(uri_path)
174
+ end
175
+
176
+ if uri.scheme == 'https'
177
+ http.use_ssl = true
178
+ http.verify_mode = @ssl_verify_mode
179
+ end
180
+
181
+ response = http.start{ |h|
182
+ h.open_timeout = timeout
183
+ h.read_timeout = timeout
184
+ h.request(request)
185
+ }
186
+ rescue Exception => err
187
+ @exception = err.message
188
+ end
189
+
190
+ @code = response.code if response
191
+
192
+ response
193
+ end
194
+ end
195
+ end