Class: Response::Redirect

Inherits:
Response show all
Includes:
AbstractType
Defined in:
lib/response/redirect.rb

Overview

Abstract redirect response

Direct Known Subclasses

Found, Permanent

Defined Under Namespace

Classes: Found, Permanent

Constant Summary

Constants inherited from Response

TEXT_PLAIN, Undefined

Instance Attribute Summary

Attributes inherited from Response

#body, #headers, #status

Class Method Summary collapse

Methods inherited from Response

#cache_control, #content_type, #last_modified, #merge_headers, #rack_array, #to_rack_response, #valid?, #with_body, #with_headers, #with_status

Class Method Details

.build(location) ⇒ Array

Build redirect response

Examples:


# 302 response
response = Response::Redirect::Found.build('http://example.com')
response.status # => 302
response.headers # => { "Location" => "http://example.com", "Content-Type" => "text/plain" }
response.body # => "You are beeing redirected to: http://example.com"

# 301 response
response = Response::Redirect::Permanent.build('http://example.com')
response.status # => 301
response.headers # => { "Location" => "http://example.com", "Content-Type" => "text/plain" }
response.body # => "You are beeing redirected to: http://example.com"

# Overriding defaults

response = Response::Redirect::Found.build('http://example.com') do |response|
  response.with_body("Redirection")
end

response[2] # => "Redirection"

Parameters:

  • location (String)

    the location to redirect to

Returns:

  • (Array)

    rack compatible array



38
39
40
# File 'lib/response/redirect.rb', line 38

def self.build(location)
  super(self::STATUS, {'Location' => location, 'Content-Type' => TEXT_PLAIN}, "You are beeing redirected to: #{location}")
end