Class: LitmusPaper::Dependency::HTTP
- Inherits:
-
Object
- Object
- LitmusPaper::Dependency::HTTP
- Defined in:
- lib/litmus_paper/dependency/http.rb
Instance Method Summary collapse
- #_body_matches?(response) ⇒ Boolean
- #_make_request ⇒ Object
- #_successful_response?(response) ⇒ Boolean
- #_verify_ssl_certificate(preverify_ok, ssl_context) ⇒ Object
- #available? ⇒ Boolean
-
#initialize(uri, options = {}) ⇒ HTTP
constructor
A new instance of HTTP.
- #to_s ⇒ Object
Constructor Details
#initialize(uri, options = {}) ⇒ HTTP
Returns a new instance of HTTP.
4 5 6 7 8 9 10 |
# File 'lib/litmus_paper/dependency/http.rb', line 4 def initialize(uri, = {}) @uri = uri @expected_content = Regexp.new(.fetch(:content, '.*')) @method = .fetch(:method, 'GET') @ca_file = [:ca_file] @timeout = .fetch(:timeout_seconds, 2) end |
Instance Method Details
#_body_matches?(response) ⇒ Boolean
55 56 57 |
# File 'lib/litmus_paper/dependency/http.rb', line 55 def _body_matches?(response) (response.body =~ @expected_content) ? true : false end |
#_make_request ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/litmus_paper/dependency/http.rb', line 29 def _make_request uri = URI.parse(@uri) request = Net::HTTP.const_get(@method.capitalize).new(uri.normalize.path) request["User-Agent"] = "Litmus Paper/#{LitmusPaper::VERSION}" request.set_form_data({}) request.basic_auth(uri.user, uri.password) if uri.user || uri.password connection = Net::HTTP.new(uri.host, uri.port) connection.open_timeout = @timeout connection.read_timeout = @timeout if uri.scheme == "https" connection.use_ssl = true connection.verify_mode = OpenSSL::SSL::VERIFY_PEER connection.ca_file = @ca_file unless @ca_file.nil? connection.verify_callback = proc { |preverify_ok, ssl_context| _verify_ssl_certificate(preverify_ok, ssl_context) } end connection.start do |http| http.request(request) end end |
#_successful_response?(response) ⇒ Boolean
51 52 53 |
# File 'lib/litmus_paper/dependency/http.rb', line 51 def _successful_response?(response) response.is_a? Net::HTTPSuccess end |
#_verify_ssl_certificate(preverify_ok, ssl_context) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/litmus_paper/dependency/http.rb', line 59 def _verify_ssl_certificate(preverify_ok, ssl_context) if preverify_ok != true || ssl_context.error != 0 err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})" LitmusPaper.logger.info err_msg return false end true end |
#available? ⇒ Boolean
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/litmus_paper/dependency/http.rb', line 12 def available? response = _make_request success = _successful_response?(response) matches = _body_matches?(response) LitmusPaper.logger.info("Available check to #{@uri} failed with status #{response.code}") unless success LitmusPaper.logger.info("Available check to #{@uri} did not match #{@expected_content}") unless matches success && matches rescue Timeout::Error LitmusPaper.logger.info("Timeout fetching #{@uri}") false rescue => e LitmusPaper.logger.info("Available check to #{@uri} failed with #{e.}") false end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/litmus_paper/dependency/http.rb', line 68 def to_s "Dependency::HTTP(#{@uri})" end |