class Rack::DomainDirector

Constants

VERSION

Public Class Methods

new(app, opts = {}) click to toggle source
# File lib/rack/domain_director.rb, line 5
def initialize(app, opts = {})
  @app             = app
  @to              = opts.fetch(:to)
  @from            = opts.fetch(:from)
  @status          = opts.fetch(:status, 301)
  @before_redirect = opts.fetch(:before_redirect, ->(req) {})
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/domain_director.rb, line 13
def call(env)
  req = Rack::DomainDirector::Request.new(env)

  if redirectable?(req)
    redirect(req)
  else
    @app.call(env)
  end
end

Private Instance Methods

redirect(req) click to toggle source
# File lib/rack/domain_director.rb, line 25
def redirect(req)
  @before_redirect.call(req)
  req.host = req.host.sub(%r{#{ Regexp.escape(@from) }$}, @to)
  [@status, {'Location' => req.url}, []]
end
redirectable?(req) click to toggle source
# File lib/rack/domain_director.rb, line 31
def redirectable?(req)
  req.host.end_with?(@from)
end