class RestPack::Web::Rack::Domain

Public Class Methods

new(app) click to toggle source
# File lib/restpack_web/rack/domain.rb, line 3
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# 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