Class: Utopia::Redirection::Moved

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

Overview

Rewrite requests that match the given pattern to a new prefix.

Instance Attribute Summary

Attributes inherited from ClientRedirect

#max_age, #status

Instance Method Summary collapse

Methods inherited from ClientRedirect

#cache_control, #call, #make_headers, #redirect

Constructor Details

#initialize(app, pattern, prefix, status: 301, flatten: false) ⇒ Moved

Initialize prefix redirection behavior.



18
19
20
21
22
23
24
# File 'lib/utopia/redirection/moved.rb', line 18

def initialize(app, pattern, prefix, status: 301, flatten: false)
	@pattern = pattern
	@prefix = prefix
	@flatten = flatten
	
	super(app, status: status)
end

Instance Method Details

#[](path) ⇒ Object

Redirect a matching path to the configured prefix.



41
42
43
44
45
46
47
48
49
# File 'lib/utopia/redirection/moved.rb', line 41

def [] path
	if path.start_with?(@pattern)
		if @flatten
			return redirect(@prefix)
		else
			return redirect(path.sub(@pattern, @prefix))
		end
	end
end

#freezeObject

Freeze this object and its internal state.



28
29
30
31
32
33
34
35
36
# File 'lib/utopia/redirection/moved.rb', line 28

def freeze
	return self if frozen?
	
	@pattern.freeze
	@prefix.freeze
	@flatten.freeze
	
	return super
end