class Newark::Route

Constants

PARAM_MATCHER
PARAM_SUB
PATH_MATCHER
PATH_SUB

Attributes

handler[R]

Public Class Methods

new(path, constraints, handler) click to toggle source
# File lib/newark/route.rb, line 11
def initialize(path, constraints, handler)
  fail ArgumentError, 'You must define a route handler' if handler.nil?

  @constraints = Constraint.load(constraints)
  @handler     = handler
  @path        = path_matcher(path)
end

Public Instance Methods

match?(request) click to toggle source
# File lib/newark/route.rb, line 19
def match?(request)
  path_data = path_match?(request)
  (path_data && constraints_match?(request)).tap do |matched|
    if matched
      request.params.merge! Hash[ path_data.names.zip( path_data.captures ) ]
    end
  end
end

Private Instance Methods

constraints_match?(request) click to toggle source
# File lib/newark/route.rb, line 30
def constraints_match?(request)
  @constraints.all? { |constraint| constraint.match?(request) }
end
match_params(path) click to toggle source
# File lib/newark/route.rb, line 55
def match_params(path)
  while match = PARAM_MATCHER.match(path)
    path.sub!(PARAM_SUB, "(?<#{match[:param]}>[^\/]*)")
  end
end
match_path(path) click to toggle source
# File lib/newark/route.rb, line 49
def match_path(path)
  if match = PATH_MATCHER.match(path)
    path.sub!(PATH_SUB, "(?<#{match[:path]}>.*)")
  end
end
path_match?(request) click to toggle source
# File lib/newark/route.rb, line 34
def path_match?(request)
  @path.match(request.path_info)
end
path_matcher(path) click to toggle source
# File lib/newark/route.rb, line 38
def path_matcher(path)
  return path if path.is_a? Regexp
  /^#{path_params(path.to_s)}$/
end
path_params(path) click to toggle source
# File lib/newark/route.rb, line 43
def path_params(path)
  match_path(path)
  match_params(path)
  path != '/' ? path.sub(/\/$/, '') : path
end