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.



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

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.



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

def max_age
  @max_age
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#[](path) ⇒ Object



119
120
121
# File 'lib/utopia/redirection.rb', line 119

def [] path
  false
end

#cache_controlObject



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

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

#call(env) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/utopia/redirection.rb', line 123

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

#freezeObject



94
95
96
97
98
99
100
101
# File 'lib/utopia/redirection.rb', line 94

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

#headers(location) ⇒ Object



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

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

#redirect(location) ⇒ Object



115
116
117
# File 'lib/utopia/redirection.rb', line 115

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