Class: Rack::EY::Solo::DomainRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-redirect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) {|_self| ... } ⇒ DomainRedirect

Returns a new instance of DomainRedirect.

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
# File 'lib/rack-redirect.rb', line 8

def initialize(app, &block)
  @app = app
  @prefix = nil
  yield self if block_given?
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



7
8
9
# File 'lib/rack-redirect.rb', line 7

def prefix
  @prefix
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack-redirect.rb', line 14

def call(env)
  parts = env['SERVER_NAME'].split('.')
  suffix, chunk, prefix = parts.pop, parts.pop, parts.pop

  if prefix == @prefix
    @app.call(env)
  else
    prefix = @prefix ? "#{@prefix}." : ''
    destination  = "#{env['rack.url_scheme']}://#{prefix}#{chunk}.#{suffix}"
    destination << "#{env['PATH_INFO']}"
    destination << "?#{env['QUERY_STRING']}" unless env['QUERY_STRING'].empty?

    [301, {'Location' => destination}, ['See Ya!']] 
  end
end