class EnsureDomain::Middleware

Public Class Methods

new(app) click to toggle source
# File lib/ensure_domain/middleware.rb, line 3
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/ensure_domain/middleware.rb, line 7
def call(env)
  if ENV['APP_DOMAIN'] && env['HTTP_HOST'] != ENV['APP_DOMAIN']
    [301, {'Location' => new_location(env), 'Content-Type' => 'text/html', 'Content-Length' => '0'}, []]
  else
    @app.call(env)
  end
end

Private Instance Methods

new_location(env) click to toggle source
# File lib/ensure_domain/middleware.rb, line 17
def new_location(env)
  scheme = env['rack.url_scheme']
  domain = ENV['APP_DOMAIN']
  path = env['PATH_INFO']
  query_string = env['QUERY_STRING'].to_s

  location = "#{scheme}://#{domain}#{path}"
  location << "?#{query_string}" unless query_string.empty?

  location
end