Sha256: bd7a318746eb4703090ed4d5ef887bc56b3654f0f848d3f10586040d13954d64

Contents?: true

Size: 1.25 KB

Versions: 13

Compression:

Stored size: 1.25 KB

Contents

require 'net/http'

module AgileNotifier
  module Servable
    class PrivateMethods
      def is_https_request?(url)
        regex = /https:\/\//
        regex.match(url) ? true : false
      end

      def is_http_server_available?(url)
        uri = URI(url)
        begin
          res = Net::HTTP.get_response(uri)
        rescue Errno::ECONNREFUSED # server shutdown
          return false
        end
        return is_response_ok?(res)
      end

      def is_https_server_available?(url)
        uri = URI(url)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        req = Net::HTTP::Get.new(uri.request_uri)
        begin
          res = http.request(req)
        rescue
          return false
        end
        return is_response_ok?(res)
      end

      def is_response_ok?(res)
        status_code = res.code.to_i
        if status_code == 200
          return true
        else # e.g. 503 (Service Unavailable)
          return false
        end
      end
    end

    def is_available?(url)
      pm = PrivateMethods.new
      pm.instance_eval do
        if is_https_request?(url)
          return is_https_server_available?(url)
        else
          return is_http_server_available?(url)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
agile_notifier-3.1.2 lib/agile_notifier/servable.rb
agile_notifier-3.0.2 lib/agile_notifier/servable.rb
agile_notifier-3.0.1 lib/agile_notifier/servable.rb
agile_notifier-3.0.0 lib/agile_notifier/servable.rb
agile_notifier-2.1.4 lib/agile_notifier/servable.rb
agile_notifier-2.1.3 lib/agile_notifier/servable.rb
agile_notifier-2.1.2 lib/agile_notifier/servable.rb
agile_notifier-2.1.1 lib/agile_notifier/servable.rb
agile_notifier-2.1 lib/agile_notifier/servable.rb
agile_notifier-2.0 lib/agile_notifier/servable.rb
agile_notifier-1.1.1 lib/agile_notifier/servable.rb
agile_notifier-1.1 lib/agile_notifier/servable.rb
agile_notifier-1.0 lib/agile_notifier/servable.rb