Class: Rage::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/response.rb

Constant Summary collapse

ETAG_HEADER =
"etag"
LAST_MODIFIED_HEADER =
"last-modified"

Instance Method Summary collapse

Instance Method Details

#bodyString

Returns the content of the response as a string. This contains the contents of any calls to render.

Returns:

  • (String)


23
24
25
# File 'lib/rage/response.rb', line 23

def body
  @controller.__body[0] || ""
end

#etagString?

Returns ETag response header or nil if it’s empty.

Returns:

  • (String, nil)


36
37
38
# File 'lib/rage/response.rb', line 36

def etag
  headers[Rage::Response::ETAG_HEADER]
end

#etag=(etag) ⇒ Object

Note:

ETag will be always Weak since no strong validation is implemented.

Note:

ArgumentError is raised if ETag value is neither String, nor nil

Sets ETag header to the response. Additionally, it will hashify the value using Digest::SHA1.hexdigest. Pass nil for resetting it.

Parameters:

  • etag (String, nil)

    The etag of the resource in the response.

Raises:

  • (ArgumentError)


44
45
46
47
48
# File 'lib/rage/response.rb', line 44

def etag=(etag)
  raise ArgumentError, "Expected `String` but `#{etag.class}` is received" unless etag.is_a?(String) || etag.nil?

  headers[Rage::Response::ETAG_HEADER] = etag.nil? ? nil : %(W/"#{Digest::SHA1.hexdigest(etag)}")
end

#headersHash

Returns the headers for the response.

Returns:

  • (Hash)


29
30
31
# File 'lib/rage/response.rb', line 29

def headers
  @controller.__headers
end

#last_modifiedString?

Returns Last-Modified response header or nil if it’s empty.

Returns:

  • (String, nil)


53
54
55
# File 'lib/rage/response.rb', line 53

def last_modified
  headers[Rage::Response::LAST_MODIFIED_HEADER]
end

#last_modified=(last_modified) ⇒ Object

Note:

ArgumentError is raised if last_modified is not a Time object instance

Sets Last-Modified header to the response by calling httpdate on the argument.

Parameters:

  • last_modified (Time, nil)

    The last modified time of the resource in the response.

Raises:

  • (ArgumentError)


60
61
62
63
64
# File 'lib/rage/response.rb', line 60

def last_modified=(last_modified)
  raise ArgumentError, "Expected `Time` but `#{last_modified.class}` is received" unless last_modified.is_a?(Time) || last_modified.nil?

  headers[Rage::Response::LAST_MODIFIED_HEADER] = last_modified&.httpdate
end

#statusInteger

Returns the HTTP status code of the response.

Returns:

  • (Integer)


17
18
19
# File 'lib/rage/response.rb', line 17

def status
  @controller.__status
end