1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/web/router.rb

101 lines
2.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-08-02 13:47:59 -04:00
require 'rack'
module Sidekiq
module WebRouter
2018-02-16 16:11:54 -05:00
GET = 'GET'
DELETE = 'DELETE'
POST = 'POST'
PUT = 'PUT'
PATCH = 'PATCH'
HEAD = 'HEAD'
2018-02-16 16:11:54 -05:00
ROUTE_PARAMS = 'rack.route_params'
REQUEST_METHOD = 'REQUEST_METHOD'
PATH_INFO = 'PATH_INFO'
def get(path, &block)
route(GET, path, &block)
end
def post(path, &block)
route(POST, path, &block)
end
def put(path, &block)
route(PUT, path, &block)
end
def patch(path, &block)
route(PATCH, path, &block)
end
def delete(path, &block)
route(DELETE, path, &block)
end
def route(method, path, &block)
@routes ||= { GET => [], POST => [], PUT => [], PATCH => [], DELETE => [], HEAD => [] }
@routes[method] << WebRoute.new(method, path, block)
@routes[HEAD] << WebRoute.new(method, path, block) if method == GET
end
def match(env)
request_method = env[REQUEST_METHOD]
2016-08-02 13:47:59 -04:00
path_info = ::Rack::Utils.unescape env[PATH_INFO]
# There are servers which send an empty string when requesting the root.
# These servers should be ashamed of themselves.
path_info = "/" if path_info == ""
@routes[request_method].each do |route|
if params = route.match(request_method, path_info)
env[ROUTE_PARAMS] = params
return WebAction.new(env, route.block)
end
end
nil
end
end
class WebRoute
attr_accessor :request_method, :pattern, :block, :name
2018-02-16 16:11:54 -05:00
NAMED_SEGMENTS_PATTERN = /\/([^\/]*):([^\.:$\/]+)/
def initialize(request_method, pattern, block)
@request_method = request_method
@pattern = pattern
@block = block
end
def matcher
@matcher ||= compile
end
def compile
if pattern.match(NAMED_SEGMENTS_PATTERN)
p = pattern.gsub(NAMED_SEGMENTS_PATTERN, '/\1(?<\2>[^$/]+)')
Regexp.new("\\A#{p}\\Z")
else
pattern
end
end
def match(request_method, path)
case matcher
when String
{} if path == matcher
else
if path_match = path.match(matcher)
Hash[path_match.names.map(&:to_sym).zip(path_match.captures)]
end
end
end
end
end