class Tenantify::Middleware::Strategies::Header

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

@example Using Header strategy:

config   = {:name => "X-Tenant"}
strategy = Tenantify::Middleware::Strategies::Header.new(config)

matching_env = {"X-Tenant" => "a_tenant"}
strategy.tenant_for(matching_env) # => :a_tenant

not_matching_env = {"X-Another-Header" => "something"}
strategy.tenant_for(not_matching_env) # => nil

Constants

NoHeaderNameError

No header name provided.

Attributes

config[R]

Public Class Methods

new(config) click to toggle source

Constructor.

@param [Hash] the strategy configuration. @option :name the name of the header.

# File lib/tenantify/middleware/strategies/header.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/header.rb, line 32
def tenant_for env
  env[header_name]
end

Private Instance Methods

header_name() click to toggle source
# File lib/tenantify/middleware/strategies/header.rb, line 40
def header_name
  @header_name ||= config.fetch(:name) { raise_error }
end
raise_error() click to toggle source
# File lib/tenantify/middleware/strategies/header.rb, line 44
def raise_error
  raise NoHeaderNameError
end