Class: Tenantify::Middleware::Strategies::Header

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

Overview

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

Examples:

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

Constant Summary collapse

NoHeaderNameError =

No header name provided.

Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Header

Constructor.

Parameters:

  • the (Hash)

    strategy configuration.

  • :name (Hash)

    a customizable set of options



24
25
26
# File 'lib/tenantify/middleware/strategies/header.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
# File 'lib/tenantify/middleware/strategies/header.rb', line 32

def tenant_for env
  env[header_name]
end