Class: Reel::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/reel/response.rb,
lib/reel/response/writer.rb

Direct Known Subclasses

StreamResponse

Defined Under Namespace

Classes: Writer

Constant Summary collapse

CONTENT_LENGTH =
'Content-Length'.freeze
TRANSFER_ENCODING =
'Transfer-Encoding'.freeze
CHUNKED =
'chunked'.freeze
STATUS_CODES =

Use status code tables from the HTTP gem

HTTP::Response::Status::REASONS
SYMBOL_TO_STATUS_CODE =
Hash[STATUS_CODES.map { |k, v| [v.downcase.gsub(/\s|-/, '_').to_sym, k] }].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, body_or_headers = nil, body = nil) ⇒ Response

Returns a new instance of Response.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/reel/response.rb', line 17

def initialize(status, body_or_headers = nil, body = nil)
  self.status = status

  if body_or_headers.is_a?(Hash)
    headers = body_or_headers.dup
    @body = body
  else
    headers = {}
    @body = body_or_headers
  end

  case @body
  when String
    headers[CONTENT_LENGTH] ||= @body.bytesize
  when IO
    headers[CONTENT_LENGTH] ||= @body.stat.size
  when Enumerable
    headers[TRANSFER_ENCODING] ||= CHUNKED
  when NilClass
  else raise TypeError, "can't render #{@body.class} as a response body"
  end

  @headers = HTTP::Headers.coerce(headers)
  @version = http_version
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



15
16
17
# File 'lib/reel/response.rb', line 15

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



15
16
17
# File 'lib/reel/response.rb', line 15

def headers
  @headers
end

#reasonObject

Reason can be set explicitly if desired



14
15
16
# File 'lib/reel/response.rb', line 14

def reason
  @reason
end

#statusObject

Status has a special setter to coerce symbol names



13
14
15
# File 'lib/reel/response.rb', line 13

def status
  @status
end

#versionObject (readonly)

Returns the value of attribute version.



15
16
17
# File 'lib/reel/response.rb', line 15

def version
  @version
end

Instance Method Details

#chunked?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/reel/response.rb', line 43

def chunked?
  headers[TRANSFER_ENCODING].to_s == CHUNKED
end