Class: Bluepill::ProcessConditions::Http

Inherits:
ProcessCondition show all
Defined in:
lib/bluepill/process_conditions/http.rb

Instance Method Summary collapse

Methods inherited from ProcessCondition

#format_value

Constructor Details

#initialize(options = {}) ⇒ Http

Returns a new instance of Http.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bluepill/process_conditions/http.rb', line 8

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

Instance Method Details

#check(value) ⇒ Object



39
40
41
42
43
44
# File 'lib/bluepill/process_conditions/http.rb', line 39

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

#run(pid) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bluepill/process_conditions/http.rb', line 21

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