Module: Roda::RodaPlugins::Base::ResponseMethods

Defined in:
lib/roda/response.rb

Overview

Instance methods for RodaResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject (readonly)

The body for the current response.



50
51
52
# File 'lib/roda/response.rb', line 50

def body
  @body
end

#headersObject (readonly)

The hash of response headers for the current response.



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

def headers
  @headers
end

#statusObject

The status code to use for the response. If none is given, will use 200 code for non-empty responses and a 404 code for empty responses.



57
58
59
# File 'lib/roda/response.rb', line 57

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object

Return the response header with the given key. Example:

response['Content-Type'] # => 'text/html'


69
70
71
# File 'lib/roda/response.rb', line 69

def [](key)
  @headers[key]
end

#[]=(key, value) ⇒ Object

Set the response header with the given key to the given value.

response['Content-Type'] = 'application/json'


76
77
78
# File 'lib/roda/response.rb', line 76

def []=(key, value)
  @headers[key] = value
end

#default_headersObject

The default headers to use for responses.



81
82
83
# File 'lib/roda/response.rb', line 81

def default_headers
  DEFAULT_HEADERS
end

#default_statusObject

Return the default response status to be used when the body has been written to. This is split out to make overriding easier in plugins.



142
143
144
# File 'lib/roda/response.rb', line 142

def default_status
  200
end

#empty?Boolean

Whether the response body has been written to yet. Note that writing an empty string to the response body marks the response as not empty. Example:

response.empty? # => true
response.write('a')
response.empty? # => false

Returns:

  • (Boolean)


92
93
94
# File 'lib/roda/response.rb', line 92

def empty?
  @body.empty?
end

#finishObject

Return the rack response array of status, headers, and body for the current response. If the status has not been set, uses the return value of default_status if the body has been written to, otherwise uses a 404 status. Adds the Content-Length header to the size of the response body.

Example:

response.finish
#  => [200,
#      {'Content-Type'=>'text/html', 'Content-Length'=>'0'},
#      []]


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/roda/response.rb', line 108

def finish
  b = @body
  set_default_headers
  h = @headers

  if b.empty?
    s = @status || 404
    if (s == 304 || s == 204 || (s >= 100 && s <= 199))
      h.delete(RodaResponseHeaders::CONTENT_TYPE)
    elsif s == 205
      empty_205_headers(h)
    else
      h[RodaResponseHeaders::CONTENT_LENGTH] ||= '0'
    end
  else
    s = @status || default_status
    h[RodaResponseHeaders::CONTENT_LENGTH] ||= @length.to_s
  end

  [s, h, b]
end

#finish_with_body(body) ⇒ Object

Return the rack response array using a given body. Assumes a 200 response status unless status has been explicitly set, and doesn’t add the Content-Length header or use the existing body.



134
135
136
137
# File 'lib/roda/response.rb', line 134

def finish_with_body(body)
  set_default_headers
  [@status || default_status, @headers, body]
end

#initializeObject

Set the default headers when creating a response.



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

def initialize
  @headers = _initialize_headers
  @body    = []
  @length  = 0
end

#inspectObject

Show response class, status code, response headers, and response body



147
148
149
# File 'lib/roda/response.rb', line 147

def inspect
  "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>"
end

#redirect(path, status = 302) ⇒ Object

Set the Location header to the given path, and the status to the given status. Example:

response.redirect('foo', 301)
response.redirect('bar')


156
157
158
159
160
# File 'lib/roda/response.rb', line 156

def redirect(path, status = 302)
  @headers[RodaResponseHeaders::LOCATION] = path
  @status  = status
  nil
end

#roda_classObject

Return the Roda class related to this response.



163
164
165
# File 'lib/roda/response.rb', line 163

def roda_class
  self.class.roda_class
end

#write(str) ⇒ Object

Write to the response body. Returns nil.

response.write('foo')


170
171
172
173
174
175
# File 'lib/roda/response.rb', line 170

def write(str)
  s = str.to_s
  @length += s.bytesize
  @body << s
  nil
end