Class: Hanami::Router::MountedPath

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/router/mounted_path.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(prefix, app) ⇒ MountedPath

Returns a new instance of MountedPath.

Since:

  • 0.1.0



6
7
8
9
# File 'lib/hanami/router/mounted_path.rb', line 6

def initialize(prefix, app)
  @prefix = prefix
  @app = app
end

Instance Method Details

#endpoint_and_params(env) ⇒ Object

Since:

  • 0.1.0



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hanami/router/mounted_path.rb', line 11

def endpoint_and_params(env)
  return [] unless (match = @prefix.peek_match(env[::Rack::PATH_INFO]))

  if @prefix.to_s == "/"
    env[::Rack::SCRIPT_NAME] = EMPTY_STRING
  else
    env[::Rack::SCRIPT_NAME] = env[::Rack::SCRIPT_NAME].to_s + @prefix.to_s
    env[::Rack::PATH_INFO] = env[::Rack::PATH_INFO].sub(@prefix.to_s, EMPTY_STRING)
    env[::Rack::PATH_INFO] = DEFAULT_PREFIX if env[::Rack::PATH_INFO] == EMPTY_STRING
  end

  [@app, match.named_captures]
end