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)


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

def accepted?;            status == 202;                        end

#add_header(key, value) ⇒ 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

Raises:

  • (ArgumentError)


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rack/response.rb', line 211

def add_header(key, value)
  raise ArgumentError unless key.is_a?(String)

  if value.nil?
    return get_header(key)
  end

  value = value.to_s

  if header = get_header(key)
    if header.is_a?(Array)
      header << value
    else
      set_header(key, [header, value])
    end
  else
    set_header(key, value)
  end
end

#bad_request?Boolean

Returns:

  • (Boolean)


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

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”.



299
300
301
302
303
304
# File 'lib/rack/response.rb', line 299

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



282
283
284
# File 'lib/rack/response.rb', line 282

def cache_control
  get_header CACHE_CONTROL
end

#cache_control=(value) ⇒ Object



286
287
288
# File 'lib/rack/response.rb', line 286

def cache_control=(value)
  set_header CACHE_CONTROL, value
end

#client_error?Boolean

Returns:

  • (Boolean)


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

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

#content_lengthObject



249
250
251
252
# File 'lib/rack/response.rb', line 249

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

#content_typeObject

Get the content type of the response.



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

def content_type
  get_header CONTENT_TYPE
end

#content_type=(content_type) ⇒ Object

Set the content type of the response.



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

def content_type=(content_type)
  set_header CONTENT_TYPE, content_type
end

#created?Boolean

Returns:

  • (Boolean)


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

def created?;             status == 201;                        end


266
267
268
269
270
271
272
# File 'lib/rack/response.rb', line 266

def delete_cookie(key, value = {})
  set_header(SET_COOKIE,
    Utils.delete_set_cookie_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.



291
292
293
294
# File 'lib/rack/response.rb', line 291

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

#etagObject



306
307
308
# File 'lib/rack/response.rb', line 306

def etag
  get_header ETAG
end

#etag=(value) ⇒ Object



310
311
312
# File 'lib/rack/response.rb', line 310

def etag=(value)
  set_header ETAG, value
end

#forbidden?Boolean

Returns:

  • (Boolean)


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

def forbidden?;           status == 403;                        end

#include?(header) ⇒ Boolean

Returns:

  • (Boolean)


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

def include?(header)
  has_header?(header)
end

#informational?Boolean

Returns:

  • (Boolean)


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

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

#invalid?Boolean

Returns:

  • (Boolean)


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

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

#locationObject



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

def location
  get_header "location"
end

#location=(location) ⇒ Object



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

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

#media_typeObject



241
242
243
# File 'lib/rack/response.rb', line 241

def media_type
  MediaType.type(content_type)
end

#media_type_paramsObject



245
246
247
# File 'lib/rack/response.rb', line 245

def media_type_params
  MediaType.params(content_type)
end

#method_not_allowed?Boolean

Returns:

  • (Boolean)


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

def method_not_allowed?;  status == 405;                        end

#moved_permanently?Boolean

Returns:

  • (Boolean)


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

def moved_permanently?;   status == 301;                        end

#no_content?Boolean

Returns:

  • (Boolean)


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

def no_content?;          status == 204;                        end

#not_acceptable?Boolean

Returns:

  • (Boolean)


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

def not_acceptable?;      status == 406;                        end

#not_found?Boolean

Returns:

  • (Boolean)


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

def not_found?;           status == 404;                        end

#ok?Boolean

Returns:

  • (Boolean)


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

def ok?;                  status == 200;                        end

#precondition_failed?Boolean

Returns:

  • (Boolean)


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

def precondition_failed?; status == 412;                        end

#redirect?Boolean

Returns:

  • (Boolean)


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

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

#redirection?Boolean

Returns:

  • (Boolean)


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

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

#request_timeout?Boolean

Returns:

  • (Boolean)


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

def request_timeout?;     status == 408;                        end

#server_error?Boolean

Returns:

  • (Boolean)


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

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


262
263
264
# File 'lib/rack/response.rb', line 262

def set_cookie(key, value)
  add_header SET_COOKIE, Utils.set_cookie_header(key, value)
end


274
275
276
# File 'lib/rack/response.rb', line 274

def set_cookie_header
  get_header SET_COOKIE
end


278
279
280
# File 'lib/rack/response.rb', line 278

def set_cookie_header=(value)
  set_header SET_COOKIE, value
end

#successful?Boolean

Returns:

  • (Boolean)


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

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

#unauthorized?Boolean

Returns:

  • (Boolean)


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

def unauthorized?;        status == 401;                        end

#unprocessable?Boolean

Returns:

  • (Boolean)


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

def unprocessable?;       status == 422;                        end