Parent

Included Modules

HTTP::Response

Attributes

code[R]
headers[R]
status[R]
status_code[R]

Public Class Methods

new(status = nil, version = "1.1", headers = {}, body = nil, &body_proc) click to toggle source
# 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

Public Instance Methods

[](name) click to toggle source

Get a header value

# File lib/http/response.rb, line 107
def [](name)
  @headers[name] || @headers[canonicalize_header(name)]
end
[]=(name, value) click to toggle source

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
body() click to toggle source

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() click to toggle source

Inspect a response

# File lib/http/response.rb, line 144
def inspect
  "#<#{self.class}/#{@version} #{status} #{reason} @headers=#{@headers.inspect}>"
end
parse_body() click to toggle source

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
reason() click to toggle source

Obtain the 'Reason-Phrase' for the response

# File lib/http/response.rb, line 101
def reason
  # FIXME: should get the real reason phrase from the parser
  STATUS_CODES[@status]
end
to_a() click to toggle source

Returns an Array ala Rack: `[status, headers, body]`

# File lib/http/response.rb, line 139
def to_a
  [status, headers, parse_body]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.