Class: Webmachine::Response
- Inherits:
-
Object
- Object
- Webmachine::Response
- Defined in:
- lib/webmachine/response.rb
Overview
Represents an HTTP response from Webmachine.
Defined Under Namespace
Classes: HeaderHash
Instance Attribute Summary collapse
-
#body ⇒ String, #each
The response body.
-
#code ⇒ Fixnum
The HTTP status code of the response.
-
#error ⇒ String
The error message when responding with an error code.
-
#headers ⇒ HeaderHash
readonly
Response headers that will be sent to the client.
-
#redirect ⇒ true, false
(also: #is_redirect?)
Whether the response is a redirect.
-
#trace ⇒ Array
readonly
The list of states that were traversed.
Instance Method Summary collapse
-
#do_redirect(location = nil) ⇒ Object
(also: #redirect_to)
Indicate that the response should be a redirect.
-
#initialize ⇒ Response
constructor
Creates a new Response object with the appropriate defaults.
-
#set_cookie(name, value, attributes = {}) ⇒ Object
Set a cookie for the response.
Constructor Details
#initialize ⇒ Response
Creates a new Response object with the appropriate defaults.
24 25 26 27 28 29 |
# File 'lib/webmachine/response.rb', line 24 def initialize @headers = HeaderHash.new @trace = [] self.code = 200 self.redirect = false end |
Instance Attribute Details
#body ⇒ String, #each
Returns The response body.
11 12 13 |
# File 'lib/webmachine/response.rb', line 11 def body @body end |
#code ⇒ Fixnum
Returns The HTTP status code of the response.
8 9 10 |
# File 'lib/webmachine/response.rb', line 8 def code @code end |
#error ⇒ String
Returns The error message when responding with an error code.
21 22 23 |
# File 'lib/webmachine/response.rb', line 21 def error @error end |
#headers ⇒ HeaderHash (readonly)
Returns Response headers that will be sent to the client.
5 6 7 |
# File 'lib/webmachine/response.rb', line 5 def headers @headers end |
#redirect ⇒ true, false Also known as: is_redirect?
Returns Whether the response is a redirect.
14 15 16 |
# File 'lib/webmachine/response.rb', line 14 def redirect @redirect end |
#trace ⇒ Array (readonly)
Returns the list of states that were traversed.
17 18 19 |
# File 'lib/webmachine/response.rb', line 17 def trace @trace end |
Instance Method Details
#do_redirect(location = nil) ⇒ Object Also known as: redirect_to
Indicate that the response should be a redirect. This is only used when processing a POST request in Webmachine::Resource::Callbacks#process_post to indicate that the client should request another resource using GET. Either pass the URI of the target resource, or manually set the Location header using #headers.
38 39 40 41 |
# File 'lib/webmachine/response.rb', line 38 def do_redirect(location=nil) headers['Location'] = location.to_s if location self.redirect = true end |
#set_cookie(name, value, attributes = {}) ⇒ Object
Set a cookie for the response.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/webmachine/response.rb', line 47 def (name, value, attributes = {}) = Webmachine::Cookie.new(name, value, attributes).to_s case headers['Set-Cookie'] when nil headers['Set-Cookie'] = when String headers['Set-Cookie'] = [headers['Set-Cookie'], ] when Array headers['Set-Cookie'] = headers['Set-Cookie'] + end end |