Class: Rack::NormalizeDomain

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

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initialize(app, canned_strategy = nil, &block) ⇒ NormalizeDomain

Returns a new instance of NormalizeDomain.



5
6
7
8
# File 'lib/rack/normalize-domain.rb', line 5

def initialize(app, canned_strategy=nil, &block)
  @app = app
  @normalizer = get_normalizer(canned_strategy, block)
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rack/normalize-domain.rb', line 26

def call(env)
  request = Request.new(@normalizer, env)

  if request.needs_normalization?
    [301, { 'Location' => request.normalized_url }, '']
  else
    @app.call(env)
  end
end

#get_canned_normalizer(strategy) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/rack/normalize-domain.rb', line 17

def get_canned_normalizer(strategy)
  case strategy
  when nil, :strip_www
    lambda { |host| host.sub(/^www\./, '') }
  else
    fail "Unknown strategy: #{strategy}"
  end
end

#get_normalizer(canned_strategy, block) ⇒ Object



10
11
12
13
14
15
# File 'lib/rack/normalize-domain.rb', line 10

def get_normalizer(canned_strategy, block)
  canned_normalizer = get_canned_normalizer(canned_strategy)
  custom_normalizer = block || lambda { |host| host }

  lambda { |host| custom_normalizer[canned_normalizer[host]] }
end