Class: Rack::Rewrite::RuleSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/rewrite/rule.rb

Overview

Internal collection that holds the ordered list of rewrite/redirect
rules a given Rack::Rewrite instance evaluates against an
incoming request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_options = {}) ⇒ RuleSet

:nodoc:



12
13
14
15
# File 'lib/rack/rewrite/rule.rb', line 12

def initialize(_options = {}) # :nodoc:
  @rules = []
  @block_options = {}
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



10
11
12
# File 'lib/rack/rewrite/rule.rb', line 10

def rules
  @rules
end

Instance Method Details

#r301Array<Rack::Rewrite::Rule> (protected) Also known as: moved_permanently, p

Creates a redirect rule that will send a 301 when matching.

r301 '/wiki/John_Trupiano', '/john'
r301 '/contact-us.php', '/contact-us'

You can use +moved_permanently+ or just +p+ instead of +r301+.

Returns:



41
42
43
# File 'lib/rack/rewrite/rule.rb', line 41

def r301(*)
  add_rule(:r301, *)
end

#r302Array<Rack::Rewrite::Rule> (protected) Also known as: found

Creates a redirect rule that will send a 302 when matching.

r302 '/wiki/John_Trupiano', '/john'
r302 '/wiki/(.*)', 'http://www.google.com/?q=$1'

You can use +found+ instead of +r302+.

Returns:



55
56
57
# File 'lib/rack/rewrite/rule.rb', line 55

def r302(*)
  add_rule(:r302, *)
end

#r303Array<Rack::Rewrite::Rule> (protected) Also known as: see_other

Creates a redirect rule that will send a 303 when matching.

r303 '/wiki/John_Trupiano', '/john'
r303 '/wiki/(.*)', 'http://www.google.com/?q=$1'

You can use +see_other+ instead of +r303+.

Returns:



68
69
70
# File 'lib/rack/rewrite/rule.rb', line 68

def r303(*)
  add_rule(:r303, *)
end

#r307Array<Rack::Rewrite::Rule> (protected) Also known as: temporary_redirect, t

Creates a redirect rule that will send a 307 when matching.

r307 '/wiki/John_Trupiano', '/john'
r307 '/wiki/(.*)', 'http://www.google.com/?q=$1'

You can use +temporary_redirect+ or +t+ instead of +r307+.

Returns:



81
82
83
# File 'lib/rack/rewrite/rule.rb', line 81

def r307(*)
  add_rule(:r307, *)
end

#rewriteArray<Rack::Rewrite::Rule> (protected)

Creates a rewrite rule that will simply rewrite the REQUEST_URI,
PATH_INFO, and QUERY_STRING headers of the Rack environment. The
user's browser will continue to show the initially requested URL.

rewrite '/wiki/John_Trupiano', '/john'
rewrite %r{/wiki/(\w+)_\w+}, '/$1'
rewrite %rRack::Rewrite::RuleSet.((.*), '/maintenance.html', :if => lambda { File.exist?('maintenance.html') }

Returns:



30
31
32
# File 'lib/rack/rewrite/rule.rb', line 30

def rewrite(*)
  add_rule(:rewrite, *)
end

#send_dataArray<Rack::Rewrite::Rule> (protected)

Creates a rule taht will render the raw data if matched
send_data /*/, 'public/system/maintenance.html',
:if => Proc.new { File.exist?('public/system/maintenance.html') }

Returns:



111
112
113
# File 'lib/rack/rewrite/rule.rb', line 111

def send_data(*)
  add_rule(:send_data, *)
end

#send_fileArray<Rack::Rewrite::Rule> (protected)

Creates a rule that will render a file if matched.

send_file /*/, 'public/system/maintenance.html',
:if => Proc.new { File.exist?('public/system/maintenance.html') }

Returns:



93
94
95
# File 'lib/rack/rewrite/rule.rb', line 93

def send_file(*)
  add_rule(:send_file, *)
end

#with_options(options) ⇒ Object (protected)

Returns the block's return value.

Parameters:

  • options (Hash)

    rule options merged over the block defaults for the duration of the block

Options Hash (options):

  • method (Array<String>)

    restrict matching to these HTTP methods

  • if (Proc)

    guard evaluated against the rack env

Returns:

  • (Object)

    the block's return value



119
120
121
122
123
124
# File 'lib/rack/rewrite/rule.rb', line 119

def with_options(options, &)
  old_options = @block_options
  @block_options = @block_options.merge(options)
  yield
  @block_options = old_options
end

#x_send_fileArray<Rack::Rewrite::Rule> (protected)

Creates a rule that will render a file using x-send-file
if matched.

x_send_file /*/, 'public/system/maintenance.html',
:if => Proc.new { File.exist?('public/system/maintenance.html') }

Returns:



103
104
105
# File 'lib/rack/rewrite/rule.rb', line 103

def x_send_file(*)
  add_rule(:x_send_file, *)
end