Class: RestPack::Web::Rack::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/restpack_web/rack/domain.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Domain

Returns a new instance of Domain.



3
4
5
# File 'lib/restpack_web/rack/domain.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/restpack_web/rack/domain.rb', line 7

def call(env)
  identifier = Rack::Request.new(env).host

  response = Commands::Core::Domain::ByIdentifier.run({
    identifier: identifier,
    include: 'applications'
  })

  if response.status == :ok
    domain = response.result[:domains][0]
    application = response.result[:applications][0]

    env['restpack'] ||= {}
    env['restpack'][:domain] = domain
    env['restpack'][:application] = application
    env['restpack'][:application_id] = application[:id]

    env['restpack.session.options'] ||= {}
    env['restpack.session.options'][:key] = 'restpack.session'
    env['restpack.session.options'][:secret] = domain[:session_secret]
    env['restpack.session.options'][:domain] = domain[:identifier]
  else
    #TODO: GJ: better exceptions based on response status
    raise "[#{identifier}] is not a RestPack domain"
  end

  @app.call(env)
end