Class: Rack::DomainRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/domain_redirect.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, hosts = []) ⇒ DomainRedirect

Returns a new instance of DomainRedirect.



7
8
9
10
11
# File 'lib/domain_redirect.rb', line 7

def initialize(app, hosts = [])
  @app = app
  @hosts = hosts
  @hosts[0] = 'www.google.com' if @hosts.empty?
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/domain_redirect.rb', line 13

def call(env)
  req = Rack::Request.new(env)
  
  if @hosts.include?(req.host)
    @app.call(env)
  else
    url = "http://#{@hosts[0]}"
    url << ":#{req.port}" unless req.port == 80
    url << "#{req.path}"
    url << "?#{req.query_string}" unless req.query_string.empty?
    res = Rack::Response.new
    res.redirect(url)
    res.finish
  end
end