Class: Tenantify::Middleware::Strategies::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/tenantify/middleware/strategies/host.rb

Overview

Strategy to get the tenant from the request host. It expect the tenant name from configuration.

Examples:

Using Host strategy:

config = {
  :tenant_1 => ["www.domain_a.com", "www.domain_b.com"],
  :tenant_2 => ["www.domain_c.com"]
}
strategy = Tenantify::Middleware::Strategies::Host.new(config)

matching_env = {"SERVER_NAME" => "www.domain_b.com"}
strategy.tenant_for(matching_env) # => :tenant_1

not_matching_env = {"SERVER_NAME" => "www.another_domain.com"}
strategy.tenant_for(not_matching_env) # => nil

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Host

Constructor. It receives a hash with tenants as keys and arrays of hosts as values.

Parameters:

  • the ({Symbol => <String>})

    strategy configuration.



24
25
26
# File 'lib/tenantify/middleware/strategies/host.rb', line 24

def initialize config
  @config = config
end

Instance Method Details

#tenant_for(env) ⇒ Symbol?

Finds a tenant for the given env.

Parameters:

  • the (rack_environment)

    rack environment.

Returns:

  • (Symbol, nil)

    the found tenant of nil.



32
33
34
35
36
# File 'lib/tenantify/middleware/strategies/host.rb', line 32

def tenant_for env
  host = env["SERVER_NAME"]

  correspondence[host]
end