Class: Webmachine::Response

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

Overview

Represents an HTTP response from Webmachine.

Defined Under Namespace

Classes: HeaderHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

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

#bodyString, #each

Returns The response body.

Returns:

  • (String, #each)

    The response body



11
12
13
# File 'lib/webmachine/response.rb', line 11

def body
  @body
end

#codeInteger

Returns The HTTP status code of the response.

Returns:

  • (Integer)

    The HTTP status code of the response



8
9
10
# File 'lib/webmachine/response.rb', line 8

def code
  @code
end

#errorString

Returns The error message when responding with an error code.

Returns:

  • (String)

    The error message when responding with an error code



21
22
23
# File 'lib/webmachine/response.rb', line 21

def error
  @error
end

#headersHeaderHash (readonly)

Returns Response headers that will be sent to the client.

Returns:

  • (HeaderHash)

    Response headers that will be sent to the client



5
6
7
# File 'lib/webmachine/response.rb', line 5

def headers
  @headers
end

#redirecttrue, false Also known as: is_redirect?

Returns Whether the response is a redirect.

Returns:

  • (true, false)

    Whether the response is a redirect



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

def redirect
  @redirect
end

#traceArray (readonly)

Returns the list of states that were traversed.

Returns:

  • (Array)

    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.

Parameters:

  • location (String, URI) (defaults to: nil)

    the target of the redirection



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 a cookie for the response.

Parameters:

  • name (String, Symbol)

    the name of the cookie

  • value (String)

    the value of the cookie

  • attributes (Hash) (defaults to: {})

    for the cookie. See RFC2109.



47
48
49
50
51
52
53
54
55
# File 'lib/webmachine/response.rb', line 47

def set_cookie(name, value, attributes = {})
  cookie = Webmachine::Cookie.new(name, value, attributes).to_s
  case headers['Set-Cookie']
  when nil
    headers['Set-Cookie'] = [cookie]
  when Array
    headers['Set-Cookie'] << cookie
  end
end