Class: Rack::Multiplexer::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/multiplexer.rb

Constant Summary collapse

PLACEHOLDER_REGEXP =
/:(\w+)/

Instance Method Summary collapse

Constructor Details

#initialize(pattern, application) ⇒ Route

Returns a new instance of Route.



76
77
78
79
# File 'lib/rack/multiplexer.rb', line 76

def initialize(pattern, application)
  @application = application
  @regexp, @keys = compile(pattern)
end

Instance Method Details

#call(env) ⇒ Object



81
82
83
84
85
86
# File 'lib/rack/multiplexer.rb', line 81

def call(env)
  request = Rack::Request.new(env)
  data = @regexp.match(env["PATH_INFO"])
  (data.size - 1).times {|i| request.update_param(@keys[i], data[i + 1]) }
  @application.call(request.env)
end

#compile(pattern) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rack/multiplexer.rb', line 92

def compile(pattern)
  keys = []
  segments = []
  pattern.split("/").each do |segment|
    segments << segment.gsub(PLACEHOLDER_REGEXP, "([^#?/]+)")
    if key = Regexp.last_match(1)
      keys << key
    end
  end
  return Regexp.new("\\A#{segments.join(?/)}\\z"), keys
end

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/rack/multiplexer.rb', line 88

def match?(path)
  @regexp === path
end