class Rasti::Web::Route

Attributes

pattern[R]

Public Class Methods

new(pattern, endpoint=nil, &block) click to toggle source
# File lib/rasti/web/route.rb, line 7
def initialize(pattern, endpoint=nil, &block)
  @pattern = normalize pattern
  @endpoint = endpoint || Endpoint.new(&block)
  @regexp = compile
end

Public Instance Methods

call(env) click to toggle source
# File lib/rasti/web/route.rb, line 22
def call(env)
  @endpoint.call env
end
extract_params(path) click to toggle source
# File lib/rasti/web/route.rb, line 17
def extract_params(path)
  result = @regexp.match normalize(path)
  result ? result.names.each_with_object(Hash::Indifferent.new) { |v,h| h[v] = result[v] } : {}
end
match?(path) click to toggle source
# File lib/rasti/web/route.rb, line 13
def match?(path)
  !@regexp.match(normalize(path)).nil?
end

Private Instance Methods

compile() click to toggle source
# File lib/rasti/web/route.rb, line 28
def compile
  compiled = pattern.gsub(')', '){0,1}')
                    .gsub('/*', '(\/(?<wildcard>.*))?')
                    .gsub(/:[a-z0-9_-]+/) { |var| "(?<#{var[1..-1]}>[^\/?#]+)" }
  %r{^#{compiled}$}
end
normalize(path) click to toggle source
# File lib/rasti/web/route.rb, line 35
def normalize(path)
  return '/' if path.strip.empty? || path == '/'
  return path[0..-2] if path[-1, 1] == '/'
  path
end