Module: Puppet::HTTP::ResponseConverter

Included in:
Network::HTTP::Connection
Defined in:
lib/puppet/http/response_converter.rb

Class Method Summary collapse

Class Method Details

.to_ruby_response(response) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/puppet/http/response_converter.rb', line 5

def to_ruby_response(response)
  str_code = response.code.to_s

  # Copied from Net::HTTPResponse because it is private there.
  clazz = Net::HTTPResponse::CODE_TO_OBJ[str_code] or
    Net::HTTPResponse::CODE_CLASS_TO_OBJ[str_code[0,1]] or
    Net::HTTPUnknownResponse
  result = clazz.new(nil, str_code, nil)
  result.body = response.body
  # This is nasty, nasty.  But apparently there is no way to create
  # an instance of Net::HttpResponse from outside of the library and have
  # the body be readable, unless you do stupid things like this.
  result.instance_variable_set(:@read, true)
  response.each_header do |k,v|
    result[k] = v
  end
  result
end