Class: Multitenancy::Middleware

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, header = 'HTTP_HOST') ⇒ Middleware

Returns a new instance of Middleware.



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

def initialize(app, header = 'HTTP_HOST')
  @app    = app
  @header = header
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/multitenancy/middleware.rb', line 3

def app
  @app
end

#headerObject (readonly)

Returns the value of attribute header.



3
4
5
# File 'lib/multitenancy/middleware.rb', line 3

def header
  @header
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/multitenancy/middleware.rb', line 10

def call(env)
  subdomain = env[header].to_s.sub(/^http(s)?:\/\//, '')
  subdomain = subdomain.sub(/\.(inteli|patrol)guide\.(net|com)/, '').downcase
  subdomain = env.fetch('TENANT', 'localhost') if development?(subdomain)

  if subdomain.length > 0
    Tenant.load(subdomain) or return [404, { 'Content-Type' => 'text/html', 'Content-Length' => '9' }, ['Not Found']]
  end

  @app.call(env)
ensure
  Tenant.current = nil
end