Class: Utopia::Redirection::ClientRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/redirection.rb

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.



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

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.



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

def max_age
  @max_age
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#[](path) ⇒ Object



111
112
113
# File 'lib/utopia/redirection.rb', line 111

def [] path
	false
end

#cache_controlObject



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

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

#call(env) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/utopia/redirection.rb', line 115

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

#freezeObject



88
89
90
91
92
93
# File 'lib/utopia/redirection.rb', line 88

def freeze
	@status.freeze
	@max_age.freeze
	
	super
end

#headers(location) ⇒ Object



103
104
105
# File 'lib/utopia/redirection.rb', line 103

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

#redirect(location) ⇒ Object



107
108
109
# File 'lib/utopia/redirection.rb', line 107

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