Sha256: db3673ad0ce2dfd46bb39ade04c89ec6c98b2de347456401c15a2c69a3108028

Contents?: true

Size: 1.74 KB

Versions: 22

Compression:

Stored size: 1.74 KB

Contents

# -*- encoding: utf-8 -*-
require 'net/http'
require 'uri'

module Bluepill
  module ProcessConditions
    class Http < ProcessCondition
      def initialize(options = {})
        @uri = URI.parse(options[:url])
        @kind = case options[:kind]
                  when Fixnum then Net::HTTPResponse::CODE_TO_OBJ[options[:kind].to_s]
                  when String, Symbol then Net.const_get("HTTP#{options[:kind].to_s.camelize}")
                else
                  Net::HTTPSuccess
                end
        @pattern = options[:pattern] || nil
        @open_timeout = (options[:open_timeout] || options[:timeout] || 5).to_i
        @read_timeout = (options[:read_timeout] || options[:timeout] || 5).to_i
      end

      def run(pid)
        session = Net::HTTP.new(@uri.host, @uri.port)
        if @uri.scheme == 'https'
          require 'net/https'
          session.use_ssl=true
          session.verify_mode = OpenSSL::SSL::VERIFY_NONE
        end
        session.open_timeout = @open_timeout
        session.read_timeout = @read_timeout
        hide_net_http_bug do
          session.start do |http|
            http.get(@uri.path)
          end
        end
      rescue
        $!
      end

      def check(value)
        return false unless value.kind_of?(@kind)
        return true  unless @pattern
        return false unless value.class.body_permitted?
        @pattern === value.body
      end

    private
      def hide_net_http_bug
        yield
      rescue NoMethodError => e
        if e.to_s =~ /#{Regexp.escape(%q|undefined method `closed?' for nil:NilClass|)}/
          raise Errno::ECONNREFUSED, "Connection refused attempting to contact #{@uri.scheme}://#{@uri.host}:#{@uri.port}"
        else
          raise
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 5 rubygems

Version Path
bluepill-rwgps-0.0.62 lib/bluepill/process_conditions/http.rb
bluepill-rwgps-0.0.61 lib/bluepill/process_conditions/http.rb
bluepill-rwgps-0.0.60 lib/bluepill/process_conditions/http.rb
kostya-bluepill-0.0.60.3 lib/bluepill/process_conditions/http.rb
kostya-bluepill-0.0.60.2 lib/bluepill/process_conditions/http.rb
kostya-bluepill-0.0.60.1 lib/bluepill/process_conditions/http.rb
skalar-bluepill-0.0.60.1 lib/bluepill/process_conditions/http.rb
bluepill-0.0.60 lib/bluepill/process_conditions/http.rb
bluepill-0.0.59 lib/bluepill/process_conditions/http.rb
bluepill-0.0.58 lib/bluepill/process_conditions/http.rb
bluepill-0.0.57 lib/bluepill/process_conditions/http.rb
bluepill-0.0.56 lib/bluepill/process_conditions/http.rb
bluepill-0.0.55 lib/bluepill/process_conditions/http.rb
bluepill-0.0.54 lib/bluepill/process_conditions/http.rb
bluepill-0.0.53 lib/bluepill/process_conditions/http.rb
bluepill-0.0.52 lib/bluepill/process_conditions/http.rb
ryansch-bluepill-0.0.53 lib/bluepill/process_conditions/http.rb
bluepill-0.0.51 lib/bluepill/process_conditions/http.rb
bluepill-0.0.50 lib/bluepill/process_conditions/http.rb
bluepill-0.0.49 lib/bluepill/process_conditions/http.rb