Sha256: 09a9a2f701c7e09040da3c8d707103808d1b902e8366a01a3fdc5f99d0b164fc

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module RailsDbBrowser
  # do mostly same thing as Rack::URLMap
  # but UrlMapper could not work under Rails
  # usage:
  #    mounted_app = RailsDbBrowser::URLTruncate.new( RackApplication, '/path')
  #    mounted_app = RailsDbBrowser::URLTruncate.new( '/path') do |env| [200, {'Content-type': 'text/plain'}, [env.inspect]] end
  #    app = Rack::Builder.app do
  #      use RailsDbBrowser::URLTruncate, '/path'
  #      run RackApplication
  #    end
  class URLTruncate
    def initialize(app_or_path, path = nil, &block)
      @path = path || app_or_path 
      @app = block || app_or_path
    end
    
    def call(env)
      path, script_name = env.values_at("PATH_INFO", "SCRIPT_NAME")
      if path.start_with?(@path)
        env.merge!('SCRIPT_NAME' => (script_name + @path), 'PATH_INFO' => path[@path.size .. -1] )
        @app.call(env)
      else
        [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{path}"]]
      end
    ensure
      env.merge!  'PATH_INFO' => path, 'SCRIPT_NAME' => script_name
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_db_browser-0.0.9 lib/rails_db_browser/url_truncate.rb