Class: Houser::Middleware

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
11
12
13
# File 'lib/houser/middleware.rb', line 7

def initialize(app, options={})
  @options = options
  @options[:subdomain_column] ||= "subdomain"
  @options[:class] = Object.const_get(options[:class_name])
  @options[:tld_length] ||= 1
  @app = app
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/houser/middleware.rb', line 5

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/houser/middleware.rb', line 15

def call(env)
  domain_parts = env['HTTP_HOST'].split('.')

  if domain_parts.length > 1 + options[:tld_length]
    domain_name = domain_parts[(-options[:tld_length] - 1)..-1]
    subdomain = (domain_parts - domain_name).join('.')
    find_tenant(env, subdomain)
  end

  @app.call(env)
end