Class: Rack::CanonicalHost::Redirect

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/canonical_host/redirect.rb

Constant Summary collapse

HTML_TEMPLATE =
<<-HTML.gsub(/^\s+/, '')
  <!DOCTYPE html>
  <html lang="en-US">
    <head><title>301 Moved Permanently</title></head>
    <body>
      <h1>Moved Permanently</h1>
      <p>The document has moved <a href="%s">here</a>.</p>
    </body>
  </html>
HTML

Instance Method Summary collapse

Constructor Details

#initialize(env, host, options = {}) ⇒ Redirect

Returns a new instance of Redirect.



17
18
19
20
21
22
23
# File 'lib/rack/canonical_host/redirect.rb', line 17

def initialize(env, host, options={})
  @env = env
  @host = host
  @force_ssl = options[:force_ssl]
  @ignore = Array(options[:ignore])
  @if = Array(options[:if])
end

Instance Method Details

#canonical?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/rack/canonical_host/redirect.rb', line 25

def canonical?
  (known? && ssl?) || ignored? || !conditions_match?
end

#responseObject



29
30
31
32
# File 'lib/rack/canonical_host/redirect.rb', line 29

def response
  headers = { 'Location' => new_url, 'Content-Type' => 'text/html' }
  [301, headers, [HTML_TEMPLATE % new_url]]
end