Class: ActionDispatch::Routing::RouteSet

Inherits:
Object
  • Object
show all
Defined in:
lib/patches/rails/recognize_path_env.rb

Instance Method Summary collapse

Instance Method Details

#recognize_path(path, environment = {}) ⇒ Object

Raises:

  • (ActionController::RoutingError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/patches/rails/recognize_path_env.rb', line 9

def recognize_path(path, environment = {})
  method = (environment[:method] || "GET").to_s.upcase
  path = Rack::Mount::Utils.normalize_path(path)

  begin
    # env = Rack::MockRequest.env_for(path, {:method => method})
    env = Rack::MockRequest.env_for(path, {:method => method}).merge(environment)
  rescue URI::InvalidURIError => e
    raise ActionController::RoutingError, e.message
  end

  req = Rack::Request.new(env)
  @set.recognize(req) do |route, matches, params|
    params.each do |key, value|
      if value.is_a?(String)
        value = value.dup.force_encoding(Encoding::BINARY) if value.encoding_aware?
        params[key] = URI.unescape(value)
      end
    end

    dispatcher = route.app
    dispatcher = dispatcher.app while dispatcher.is_a?(Mapper::Constraints)

    if dispatcher.is_a?(Dispatcher) && dispatcher.controller(params, false)
      dispatcher.prepare_params!(params)
      return params
    end
  end

  raise ActionController::RoutingError, "No route matches #{path.inspect}"
end