Class: Utopia::Redirection::ClientRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/redirection.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: DEFAULT_MAX_AGE) ⇒ ClientRedirect

Returns a new instance of ClientRedirect.



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

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

Instance Attribute Details

#max_ageObject (readonly)

Returns the value of attribute max_age.



85
86
87
# File 'lib/utopia/redirection.rb', line 85

def max_age
  @max_age
end

#statusObject (readonly)

Returns the value of attribute status.



84
85
86
# File 'lib/utopia/redirection.rb', line 84

def status
  @status
end

Instance Method Details

#[](path) ⇒ Object



100
101
102
# File 'lib/utopia/redirection.rb', line 100

def [] path
	false
end

#cache_controlObject



87
88
89
90
# File 'lib/utopia/redirection.rb', line 87

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

#call(env) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/utopia/redirection.rb', line 104

def call(env)
	path = env[Rack::PATH_INFO]
	
	if redirection = self[path]
		return redirection
	end
	
	return @app.call(env)
end

#freezeObject



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

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

#headers(location) ⇒ Object



92
93
94
# File 'lib/utopia/redirection.rb', line 92

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

#redirect(location) ⇒ Object



96
97
98
# File 'lib/utopia/redirection.rb', line 96

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