Class: Apartment::Elevators::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/apartment/elevators/generic.rb

Overview

Provides a rack based tenant switching solution based on request

Direct Known Subclasses

Domain, Header, Host, HostHash, Subdomain

Instance Method Summary collapse

Constructor Details

#initialize(app, processor = nil, **_options) ⇒ Generic

Returns a new instance of Generic.



11
12
13
14
# File 'lib/apartment/elevators/generic.rb', line 11

def initialize(app, processor = nil, **_options)
  @app = app
  @processor = processor || method(:parse_tenant_name)
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/apartment/elevators/generic.rb', line 16

def call(env)
  request = Rack::Request.new(env)

  begin
    database = @processor.call(request)
  rescue Apartment::TenantNotFound => e
    # HostHash and similar raise during resolution; route through the
    # same handler. The rescue is narrow — it does NOT wrap @app.call,
    # so a TenantNotFound raised by the application is never swallowed.
    # Prefer the exception's own tenant; fall back to the host.
    return handle_tenant_not_found(e.tenant || request.host, request)
  end

  return @app.call(env) if database.nil?
  return handle_tenant_not_found(database, request) unless tenant_valid?(database)

  switch_with_failsafe(database, request) { @app.call(env) }
end

#parse_tenant_name(_request) ⇒ Object

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/apartment/elevators/generic.rb', line 35

def parse_tenant_name(_request)
  raise(NotImplementedError, "#{self.class}#parse_tenant_name must be implemented")
end