Class: Rack::HostRedirect
- Inherits:
-
Object
- Object
- Rack::HostRedirect
- Defined in:
- lib/rack/host_redirect.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, host_mapping = nil, &block) ⇒ HostRedirect
constructor
A new instance of HostRedirect.
Constructor Details
#initialize(app, host_mapping = nil, &block) ⇒ HostRedirect
Returns a new instance of HostRedirect.
5 6 7 8 9 |
# File 'lib/rack/host_redirect.rb', line 5 def initialize(app, host_mapping = nil, &block) @app = app @host_mapping = downcase_keys(host_mapping) if host_mapping @block = block end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rack/host_redirect.rb', line 11 def call(env) request = Rack::Request.new(env) host = request.host.downcase # downcase for case-insensitive matching updated_host = (@host_mapping && @host_mapping[host]) || (@block && @block.call(host, env)) if updated_host && updated_host.downcase != host location = replace_host(request.url, updated_host) [301, {'Location' => location}, []] else @app.call(env) end end |