Class: HTTP::Response

Inherits:
Object
  • Object
show all
Includes:
Header
Defined in:
lib/http/response.rb,
lib/http/response_parser.rb

Defined Under Namespace

Classes: BodyDelegator, Parser

Constant Summary collapse

STATUS_CODES =
{
  100 => 'Continue',
  101 => 'Switching Protocols',
  102 => 'Processing',
  200 => 'OK',
  201 => 'Created',
  202 => 'Accepted',
  203 => 'Non-Authoritative Information',
  204 => 'No Content',
  205 => 'Reset Content',
  206 => 'Partial Content',
  207 => 'Multi-Status',
  226 => 'IM Used',
  300 => 'Multiple Choices',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  305 => 'Use Proxy',
  306 => 'Reserved',
  307 => 'Temporary Redirect',
  400 => 'Bad Request',
  401 => 'Unauthorized',
  402 => 'Payment Required',
  403 => 'Forbidden',
  404 => 'Not Found',
  405 => 'Method Not Allowed',
  406 => 'Not Acceptable',
  407 => 'Proxy Authentication Required',
  408 => 'Request Timeout',
  409 => 'Conflict',
  410 => 'Gone',
  411 => 'Length Required',
  412 => 'Precondition Failed',
  413 => 'Request Entity Too Large',
  414 => 'Request-URI Too Long',
  415 => 'Unsupported Media Type',
  416 => 'Requested Range Not Satisfiable',
  417 => 'Expectation Failed',
  418 => "I'm a Teapot",
  422 => 'Unprocessable Entity',
  423 => 'Locked',
  424 => 'Failed Dependency',
  426 => 'Upgrade Required',
  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Timeout',
  505 => 'HTTP Version Not Supported',
  506 => 'Variant Also Negotiates',
  507 => 'Insufficient Storage',
  510 => 'Not Extended'
}
SYMBOL_TO_STATUS_CODE =
Hash[STATUS_CODES.map { |code, msg| [msg.downcase.gsub(/\s|-/, '_').to_sym, code]

Constants included from Header

Header::CANONICAL_HEADER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Header

#canonicalize_header

Constructor Details

#initialize(status = nil, version = "1.1", headers = {}, body = nil, &body_proc) ⇒ Response

Returns a new instance of Response.



74
75
76
77
78
79
80
81
# 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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



68
69
70
# File 'lib/http/response.rb', line 68

def headers
  @headers
end

#statusObject (readonly) Also known as: code, status_code

Returns the value of attribute status.



67
68
69
# File 'lib/http/response.rb', line 67

def status
  @status
end

Instance Method Details

#[](name) ⇒ Object

Get a header value



107
108
109
# File 'lib/http/response.rb', line 107

def [](name)
  @headers[name] || @headers[canonicalize_header(name)]
end

#[]=(name, value) ⇒ Object

Set a header



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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

#bodyObject

Obtain the response body



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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

#inspectObject

Inspect a response



144
145
146
# File 'lib/http/response.rb', line 144

def inspect
  "#<#{self.class}/#{@version} #{status} #{reason} @headers=#{@headers.inspect}>"
end

#parse_bodyObject

Parse the response body according to its content type



129
130
131
132
133
134
135
136
# 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

#reasonObject

Obtain the ‘Reason-Phrase’ for the response



101
102
103
104
# File 'lib/http/response.rb', line 101

def reason
  # FIXME: should get the real reason phrase from the parser
  STATUS_CODES[@status]
end

#to_aObject

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



139
140
141
# File 'lib/http/response.rb', line 139

def to_a
  [status, headers, parse_body]
end