Class: RailsDbBrowser::URLTruncate

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_db_browser/url_truncate.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(app_or_path, path = nil, &block) ⇒ URLTruncate

Returns a new instance of URLTruncate.



12
13
14
15
# File 'lib/rails_db_browser/url_truncate.rb', line 12

def initialize(app_or_path, path = nil, &block)
  @path = path || app_or_path 
  @app = block || app_or_path
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_db_browser/url_truncate.rb', line 17

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