Module: Rack::Response::Helpers

Included in:
Rack::Response, Raw
Defined in:
lib/rack/response.rb

Instance Method Summary collapse

Instance Method Details

#accepted?Boolean

Returns:

  • (Boolean)


145
# File 'lib/rack/response.rb', line 145

def accepted?;            status == 202;                        end

#add_header(key, v) ⇒ Object

Add a header that may have multiple values.

Example:

response.add_header 'Vary', 'Accept-Encoding'
response.add_header 'Vary', 'Cookie'

assert_equal 'Accept-Encoding,Cookie', response.get_header('Vary')

www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2



171
172
173
174
175
176
177
178
179
# File 'lib/rack/response.rb', line 171

def add_header(key, v)
  if v.nil?
    get_header key
  elsif has_header? key
    set_header key, "#{get_header key},#{v}"
  else
    set_header key, v
  end
end

#bad_request?Boolean

Returns:

  • (Boolean)


148
# File 'lib/rack/response.rb', line 148

def bad_request?;         status == 400;                        end

#cache!(duration = 3600, directive: "public") ⇒ Object

Specify that the content should be cached.

Parameters:

  • duration (Integer) (defaults to: 3600)

    The number of seconds until the cache expires.

  • directive (Hash) (defaults to: "public")

    a customizable set of options

Options Hash (directive:):

  • The (String)

    cache control directive, one of “public”, “private”, “no-cache” or “no-store”.



246
247
248
249
250
251
# File 'lib/rack/response.rb', line 246

def cache!(duration = 3600, directive: "public")
  unless headers[CACHE_CONTROL] =~ /no-cache/
    set_header CACHE_CONTROL, "#{directive}, max-age=#{duration}"
    set_header EXPIRES, (Time.now + duration).httpdate
  end
end

#cache_controlObject



229
230
231
# File 'lib/rack/response.rb', line 229

def cache_control
  get_header CACHE_CONTROL
end

#cache_control=(v) ⇒ Object



233
234
235
# File 'lib/rack/response.rb', line 233

def cache_control=(v)
  set_header CACHE_CONTROL, v
end

#client_error?Boolean

Returns:

  • (Boolean)


140
# File 'lib/rack/response.rb', line 140

def client_error?;        status >= 400 && status < 500;        end

#content_lengthObject



199
200
201
202
# File 'lib/rack/response.rb', line 199

def content_length
  cl = get_header CONTENT_LENGTH
  cl ? cl.to_i : cl
end

#content_typeObject

Get the content type of the response.



182
183
184
# File 'lib/rack/response.rb', line 182

def content_type
  get_header CONTENT_TYPE
end

#content_type=(content_type) ⇒ Object

Set the content type of the response.



187
188
189
# File 'lib/rack/response.rb', line 187

def content_type=(content_type)
  set_header CONTENT_TYPE, content_type
end

#created?Boolean

Returns:

  • (Boolean)


144
# File 'lib/rack/response.rb', line 144

def created?;             status == 201;                        end


217
218
219
# File 'lib/rack/response.rb', line 217

def delete_cookie(key, value = {})
  set_header SET_COOKIE, ::Rack::Utils.add_remove_cookie_to_header(get_header(SET_COOKIE), key, value)
end

#do_not_cache!Object

Specifies that the content shouldn’t be cached. Overrides ‘cache!` if already called.



238
239
240
241
# File 'lib/rack/response.rb', line 238

def do_not_cache!
  set_header CACHE_CONTROL, "no-cache, must-revalidate"
  set_header EXPIRES, Time.now.httpdate
end

#etagObject



253
254
255
# File 'lib/rack/response.rb', line 253

def etag
  get_header ETAG
end

#etag=(v) ⇒ Object



257
258
259
# File 'lib/rack/response.rb', line 257

def etag=(v)
  set_header ETAG, v
end

#forbidden?Boolean

Returns:

  • (Boolean)


150
# File 'lib/rack/response.rb', line 150

def forbidden?;           status == 403;                        end

#include?(header) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/rack/response.rb', line 158

def include?(header)
  has_header? header
end

#informational?Boolean

Returns:

  • (Boolean)


137
# File 'lib/rack/response.rb', line 137

def informational?;       status >= 100 && status < 200;        end

#invalid?Boolean

Returns:

  • (Boolean)


135
# File 'lib/rack/response.rb', line 135

def invalid?;             status < 100 || status >= 600;        end

#locationObject



204
205
206
# File 'lib/rack/response.rb', line 204

def location
  get_header "Location"
end

#location=(location) ⇒ Object



208
209
210
# File 'lib/rack/response.rb', line 208

def location=(location)
  set_header "Location", location
end

#media_typeObject



191
192
193
# File 'lib/rack/response.rb', line 191

def media_type
  MediaType.type(content_type)
end

#media_type_paramsObject



195
196
197
# File 'lib/rack/response.rb', line 195

def media_type_params
  MediaType.params(content_type)
end

#method_not_allowed?Boolean

Returns:

  • (Boolean)


152
# File 'lib/rack/response.rb', line 152

def method_not_allowed?;  status == 405;                        end

#moved_permanently?Boolean

Returns:

  • (Boolean)


147
# File 'lib/rack/response.rb', line 147

def moved_permanently?;   status == 301;                        end

#no_content?Boolean

Returns:

  • (Boolean)


146
# File 'lib/rack/response.rb', line 146

def no_content?;          status == 204;                        end

#not_found?Boolean

Returns:

  • (Boolean)


151
# File 'lib/rack/response.rb', line 151

def not_found?;           status == 404;                        end

#ok?Boolean

Returns:

  • (Boolean)


143
# File 'lib/rack/response.rb', line 143

def ok?;                  status == 200;                        end

#precondition_failed?Boolean

Returns:

  • (Boolean)


153
# File 'lib/rack/response.rb', line 153

def precondition_failed?; status == 412;                        end

#redirect?Boolean

Returns:

  • (Boolean)


156
# File 'lib/rack/response.rb', line 156

def redirect?;            [301, 302, 303, 307, 308].include? status; end

#redirection?Boolean

Returns:

  • (Boolean)


139
# File 'lib/rack/response.rb', line 139

def redirection?;         status >= 300 && status < 400;        end

#server_error?Boolean

Returns:

  • (Boolean)


141
# File 'lib/rack/response.rb', line 141

def server_error?;        status >= 500 && status < 600;        end


212
213
214
215
# File 'lib/rack/response.rb', line 212

def set_cookie(key, value)
  cookie_header = get_header SET_COOKIE
  set_header SET_COOKIE, ::Rack::Utils.add_cookie_to_header(cookie_header, key, value)
end


221
222
223
# File 'lib/rack/response.rb', line 221

def set_cookie_header
  get_header SET_COOKIE
end


225
226
227
# File 'lib/rack/response.rb', line 225

def set_cookie_header=(v)
  set_header SET_COOKIE, v
end

#successful?Boolean

Returns:

  • (Boolean)


138
# File 'lib/rack/response.rb', line 138

def successful?;          status >= 200 && status < 300;        end

#unauthorized?Boolean

Returns:

  • (Boolean)


149
# File 'lib/rack/response.rb', line 149

def unauthorized?;        status == 401;                        end

#unprocessable?Boolean

Returns:

  • (Boolean)


154
# File 'lib/rack/response.rb', line 154

def unprocessable?;       status == 422;                        end