Sha256: 4b929ef1deffa4628e1266d510fdcf92921c2c8ed0fefcdd203379318187d0b3

Contents?: true

Size: 1.55 KB

Versions: 14

Compression:

Stored size: 1.55 KB

Contents

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)
        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

14 entries across 14 versions & 5 rubygems

Version Path
ra-bluepill-0.0.48 lib/bluepill/process_conditions/http.rb
ra-bluepill-0.0.47 lib/bluepill/process_conditions/http.rb
ra-bluepill-0.0.46 lib/bluepill/process_conditions/http.rb
ra-bluepill-0.0.45 lib/bluepill/process_conditions/http.rb
ra-bluepill-0.0.44 lib/bluepill/process_conditions/http.rb
evented_bluepill-0.0.46 lib/bluepill/process_conditions/http.rb
bluepill-0.0.44 lib/bluepill/process_conditions/http.rb
sumskyi-bluepill-0.0.42.1 lib/bluepill/process_conditions/http.rb
sumskyi-bluepill-0.0.42.0 lib/bluepill/process_conditions/http.rb
bluepill-0.0.43 lib/bluepill/process_conditions/http.rb
bluepill-0.0.42 lib/bluepill/process_conditions/http.rb
bluepill-0.0.40 lib/bluepill/process_conditions/http.rb
dylanvaughn-bluepill-0.0.40 lib/bluepill/process_conditions/http.rb
bluepill-0.0.39 lib/bluepill/process_conditions/http.rb