Sha256: 8bdf454ce1767421c941deb98f004040dfdf693b10047695a5537ec9197f66fd

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

# -*- encoding: utf-8 -*-

require 'net/http'
require 'uri'

require 'evented_bluepill/process_conditions/process_condition'

# really this needs reworking
module EventedBluepill
  module ProcessConditions
    class Http < ProcessCondition
      def initialize(name, process, 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
        
        super
      end

      def run
        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

3 entries across 3 versions & 1 rubygems

Version Path
evented_bluepill-0.0.52 lib/evented_bluepill/process_conditions/http.rb
evented_bluepill-0.0.51 lib/evented_bluepill/process_conditions/http.rb
evented_bluepill-0.0.50 lib/evented_bluepill/process_conditions/http.rb