# File lib/http/response.rb, line 74 def initialize(status = nil, version = "1.1", headers = {}, body = nil, &body_proc) @status, @version, @body, @body_proc = status, version, body, body_proc @headers = {} headers.each do |field, value| @headers[canonicalize_header(field)] = value end end
Get a header value
# File lib/http/response.rb, line 107 def [](name) @headers[name] || @headers[canonicalize_header(name)] end
Set a header
# File lib/http/response.rb, line 84 def []=(name, value) # If we have a canonical header, we're done key = name[CANONICAL_HEADER] # Convert to canonical capitalization key ||= canonicalize_header(name) # Check if the header has already been set and group old_value = @headers[key] if old_value @headers[key] = [old_value].flatten << key else @headers[key] = value end end
Obtain the response body
# File lib/http/response.rb, line 112 def body @body ||= begin raise "no body available for this response" unless @body_proc body = "" unless block_given? while (chunk = @body_proc.call) if block_given? yield chunk else body << chunk end end body unless block_given? end end
Inspect a response
# File lib/http/response.rb, line 144 def inspect "#<#{self.class}/#{@version} #{status} #{reason} @headers=#{@headers.inspect}>" end
Parse the response body according to its content type
# File lib/http/response.rb, line 129 def parse_body if @headers['Content-Type'] mime_type = MimeType[@headers['Content-Type'].split(/;\s*/).first] return mime_type.parse(body) if mime_type end body end
Generated with the Darkfish Rdoc Generator 2.