Sha256: 30b4725308d5da30d35e35d7da302bafa6600e858d06bb8d1faa334717a4f0c4

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

#
# The Penthouse::App class defines a Rack middleware to be included into your
# stack before your main application is called.
#
# @example Typically in Rails you'd use:
#   Rails.application.config.middleware.use Penthouse::App, router: Penthouse::Routers::BaseRouter
#
# This app uses the router to determine the tenant instance, then calls the
# application within that tenant.
#

require 'rack/request'

module Penthouse
  class App
    attr_accessor :app, :router, :runner
    private :app=, :router=, :runner=

    # @param app the Rack application
    # @param router [#call] the class/proc to use as the router
    # @param runner [#call] the class/proc to use as the runner
    def initialize(app, router: Penthouse.configuration.router, runner: Penthouse.configuration.runner)
      self.app = app
      self.router = router
      self.runner = runner
    end

    # @param env [Hash] the environment passed from Rack
    # @raise [Penthouse::TenantNotFound] if the tenant cannot be found/switched to
    # @return [void]
    def call(env)
      request = Rack::Request.new(env)
      runner.call(tenant_identifier: router.call(request)) do
        app.call(env)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
penthouse-0.13.2 lib/penthouse/app.rb
penthouse-0.13.1 lib/penthouse/app.rb
penthouse-0.13 lib/penthouse/app.rb
penthouse-0.12.2 lib/penthouse/app.rb
penthouse-0.12.1 lib/penthouse/app.rb
penthouse-0.12.0 lib/penthouse/app.rb