Class: Utopia::Redirection::ClientRedirect

Inherits:
Protocol::HTTP::Middleware
  • Object
show all
Defined in:
lib/utopia/redirection/client_redirect.rb

Overview

A basic client-side redirect.

Direct Known Subclasses

DirectoryIndex, Moved, Rewrite

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, status: 307, max_age: MAX_AGE) ⇒ ClientRedirect

Initialize client-side redirection behavior.



21
22
23
24
25
26
# File 'lib/utopia/redirection/client_redirect.rb', line 21

def initialize(app, status: 307, max_age: MAX_AGE)
	super(app)
	
	@status = status
	@max_age = max_age
end

Instance Attribute Details

#max_ageObject (readonly)

Returns the value of attribute max_age.



40
41
42
# File 'lib/utopia/redirection/client_redirect.rb', line 40

def max_age
  @max_age
end

#statusObject (readonly)

Returns the value of attribute status.



39
40
41
# File 'lib/utopia/redirection/client_redirect.rb', line 39

def status
  @status
end

Instance Method Details

#[](path) ⇒ Object

Resolve a normalized request path to a redirect response.



69
70
71
# File 'lib/utopia/redirection/client_redirect.rb', line 69

def [] path
	false
end

#cache_controlObject

Build the cache control header value.



44
45
46
47
# File 'lib/utopia/redirection/client_redirect.rb', line 44

def cache_control
	# http://jacquesmattheij.com/301-redirects-a-dangerous-one-way-street
	"max-age=#{self.max_age}"
end

#call(request) ⇒ Object

Redirect a normalized request path when it matches, otherwise invoke the application.



76
77
78
79
80
81
82
83
84
# File 'lib/utopia/redirection/client_redirect.rb', line 76

def call(request)
	path = request.url.path.encoded
	
	if redirection = self[path]
		return redirection
	end
	
	return @delegate.call(request)
end

#freezeObject

Freeze this object and its internal state.



30
31
32
33
34
35
36
37
# File 'lib/utopia/redirection/client_redirect.rb', line 30

def freeze
	return self if frozen?
	
	@status.freeze
	@max_age.freeze
	
	super
end

#make_headers(location) ⇒ Object

Build headers for a client redirect.



52
53
54
55
56
57
# File 'lib/utopia/redirection/client_redirect.rb', line 52

def make_headers(location)
	{
		HTTP::LOCATION => location,
		HTTP::CACHE_CONTROL => self.cache_control
	}
end

#redirect(location) ⇒ Object

Build a redirect response for the given location.



62
63
64
# File 'lib/utopia/redirection/client_redirect.rb', line 62

def redirect(location)
	return Response[self.status, self.make_headers(location), []]
end