class Tenantify::Middleware::Strategies::Host
Strategy to get the tenant from the request host. It expect the tenant name from configuration.
@example 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
Attributes
config[R]
Public Class Methods
new(config)
click to toggle source
Constructor. It receives a hash with tenants as keys and arrays of hosts as values.
@param [{Symbol => <String>}] the strategy configuration.
# File lib/tenantify/middleware/strategies/host.rb, line 24 def initialize config @config = config end
Public Instance Methods
tenant_for(env)
click to toggle source
Finds a tenant for the given env.
@param [rack_environment] the rack environment. @return [Symbol, nil] the found tenant of nil.
# File lib/tenantify/middleware/strategies/host.rb, line 32 def tenant_for env host = env["SERVER_NAME"] correspondence[host] end
Private Instance Methods
correspondence()
click to toggle source
# File lib/tenantify/middleware/strategies/host.rb, line 42 def correspondence @correspondence ||= config.reduce Hash.new do |result, (tenant, domains)| result.merge! correspondence_for(tenant, domains) end end
correspondence_for(tenant, domains)
click to toggle source
# File lib/tenantify/middleware/strategies/host.rb, line 48 def correspondence_for tenant, domains domains.reduce Hash.new do |result, domain| result.merge! domain => tenant end end