Class: Padrino::Router
- Inherits:
-
Object
- Object
- Padrino::Router
- Defined in:
- lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/router.rb
Overview
This class is an extended version of Rack::URLMap
Padrino::Router like Rack::URLMap dispatches in such a way that the longest paths are tried first, since they are most specific.
Features:
-
Map a path to the specified App
-
Ignore server names (this solve issues with vhost and domain aliases)
-
Use hosts instead of server name for mappings (this help us with our vhost and doman aliases)
Instance Method Summary collapse
-
#call(env) ⇒ Object
private
The call handler setup to route a request given the mappings specified.
-
#initialize(*mapping, &block) ⇒ Router
constructor
Constructs a new route mapper instance.
-
#map(options = {}) ⇒ Array
Map a route path and host to a specified application.
Constructor Details
#initialize(*mapping, &block) ⇒ Router
Constructs a new route mapper instance
31 32 33 34 35 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/router.rb', line 31 def initialize(*mapping, &block) @mapping = [] mapping.each { |m| map(m) } instance_eval(&block) if block end |
Instance Method Details
#call(env) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The call handler setup to route a request given the mappings specified.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/router.rb', line 72 def call(env) rPath = env["PATH_INFO"].to_s script_name = env['SCRIPT_NAME'] hHost, sName, sPort = env.values_at('HTTP_HOST','SERVER_NAME','SERVER_PORT') @mapping.each do |host, path, match, app| next unless host.nil? || hHost =~ host next unless rPath =~ match && rest = $1 next unless rest.empty? || rest[0] == ?/ rest = "/" if rest.empty? return app.call( env.merge( 'SCRIPT_NAME' => (script_name + path), 'PATH_INFO' => rest)) end [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{rPath}"]] end |
#map(options = {}) ⇒ Array
Map a route path and host to a specified application.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/router.rb', line 54 def map(={}) path = [:path] || "/" host = [:host] app = [:to] raise ArgumentError, "paths need to start with /" if path[0] != ?/ raise ArgumentError, "app is required" if app.nil? path = path.chomp('/') match = Regexp.new("^#{Regexp.quote(path).gsub('/', '/+')}(.*)", nil, 'n') host = Regexp.new("^#{Regexp.quote(host)}$", true, 'n') unless host.nil? || host.is_a?(Regexp) @mapping << [host, path, match, app] sort! end |