Class: DomainSwitcher::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/domain_switcher/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, config = nil) ⇒ Middleware

Returns a new instance of Middleware.



4
5
6
7
8
9
10
# File 'lib/domain_switcher/middleware.rb', line 4

def initialize(app, config = nil)
  @app = app
  @config = config || DomainSwitcher::ConfigLoader.new(Rails.root.join('config/websites.yml'), Rails.env)
  @scanner = DomainSwitcher::Scanner.new(@config.domains)
  
  DomainSwitcher::Cache.init
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/domain_switcher/middleware.rb', line 12

def call(env)
  domain = @scanner.scan_domain(env['HTTP_HOST']) || @config.default_website.default_domain

  Rails.logger.debug "DomainSwitcher: Switch context for domain: #{domain.domain} (#{domain.website.name})"

  if Thread.current[:domain_switcher_domain] != domain
    Thread.current[:domain_switcher_domain] = domain
    Thread.current[:domain_switcher_website] = domain.website

    DomainSwitcher::Cache::switch(domain.website)
    DomainSwitcher::Cookie::switch(domain)
  end
  
  @app.call(env)
end