Sha256: 8a799e2b95ffd9e5ab91d7fe190e3d8fc2ce736833e9e6d7c569965f1b67d47a

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

require 'rack'

module Motel

  class Lobby

    def initialize(app)
      @app = app
    end

    def call(env)
      request = Rack::Request.new(env)

      name = tenant_name(request)

      if name
        if Motel::Manager.tenant?(name)
          Motel::Manager.current_tenant = name
          @app.call(env)
        else
          path = Motel::Manager.nonexistent_tenant_page
          file = File.expand_path(path) if path
          body = (File.exists?(file.to_s)) ? File.read(file) : "Nonexistent #{name} tenant"
          [404, {"Content-Type" => "text/html", "Content-Length" => body.size.to_s}, [body]]
        end
      else
        Motel::Manager.current_tenant = nil
        @app.call(env)
      end
    end

    private

      def tenant_name(request)
        if Motel::Manager.admission_criteria
          regex = Regexp.new(Motel::Manager.admission_criteria)
          name = request.path.match(regex)
          name[1] if name
        else
         request.host.split('.').first
        end
      end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
motel-activerecord-2.0.3 lib/motel/lobby.rb
motel-activerecord-2.0.2 lib/motel/lobby.rb
motel-activerecord-2.0.1 lib/motel/lobby.rb
motel-activerecord-2.0.0 lib/motel/lobby.rb