Class: Multiplicity::Middleware::Subdomain

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Subdomain.



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

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

  unless defined?(Multiplicity::Adapters)
    raise RuntimeError, "You must require an adapter to use Multiplicity"
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/multiplicity/middleware/subdomain.rb', line 4

def app
  @app
end

#headerObject (readonly)

Returns the value of attribute header.



4
5
6
# File 'lib/multiplicity/middleware/subdomain.rb', line 4

def header
  @header
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/multiplicity/middleware/subdomain.rb', line 15

def call(env)
  subdomain = env[header].to_s.sub(/^http(s)?:\/\//, '').sub(/:[0-9]+$/, '')
  subdomain = subdomain.split('.')[0..-3].join('.').downcase if subdomain.split('.').length > 2
  subdomain = env.fetch('TENANT', 'localhost') if development?(subdomain)

  if subdomain.length > 0
    ::Multiplicity::Tenant.load(subdomain) or return not_found
  else
    return not_found
  end

  return gone if ::Multiplicity::Tenant.current.archived?

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

#goneObject



37
38
39
# File 'lib/multiplicity/middleware/subdomain.rb', line 37

def gone
  [410, { 'Content-Type' => 'text/plain', 'Content-Length' => '15' }, ['Tenant archived']]
end

#not_foundObject



33
34
35
# File 'lib/multiplicity/middleware/subdomain.rb', line 33

def not_found
  [404, { 'Content-Type' => 'text/plain', 'Content-Length' => '9' }, ['Not Found']]
end