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)


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

def body
  @body[0]
end

#etagString?

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

Returns:

  • (String, nil)


31
32
33
# File 'lib/rage/response.rb', line 31

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)


39
40
41
42
43
# File 'lib/rage/response.rb', line 39

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)


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

def headers
  @headers
end

#last_modifiedString?

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

Returns:

  • (String, nil)


48
49
50
# File 'lib/rage/response.rb', line 48

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)


55
56
57
58
59
# File 'lib/rage/response.rb', line 55

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