Class: Rack::MultiTenant::TenantStrategies::Subdomain

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/multitenant/tenant_strategies/subdomain.rb

Instance Method Summary collapse

Constructor Details

#initialize(domain, &getter) ⇒ Subdomain

Gets a tenant by subdomain.

Optionally takes a block that transforms the tenant string into a proper tenant.

>> Sub =  Rack::MultiTenant::TenantStrategies::Subdomain
>> require "ostruct"

>> foo_req = OpenStruct.new(host: "foo.example.com")
>> strat = Sub.new(".example.com")
>> strat.call(foo_req)
=> "foo"

>> strat_with_getter = Sub.new("example.com", &:to_sym)
>> strat_with_getter.call(foo_req)
=> :foo

>> wrong_host_req = OpenStruct.new(host: "localhost")
>> strat.call(wrong_host_req)
=> nil


23
24
25
# File 'lib/rack/multitenant/tenant_strategies/subdomain.rb', line 23

def initialize(domain, &getter)
  @domain, @getter = domain, getter || lambda {|k| k}
end

Instance Method Details

#call(request) ⇒ Object



27
28
29
30
31
# File 'lib/rack/multitenant/tenant_strategies/subdomain.rb', line 27

def call(request)
 if subdomain = subdomain(request.host, @domain)
   @getter.call(subdomain)
  end
end