Class: Sidekiq::WebRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/web/router.rb

Constant Summary collapse

NAMED_SEGMENTS_PATTERN =
/\/([^\/]*):([^\.:$\/]+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_method, pattern, block) ⇒ WebRoute


69
70
71
72
73
# File 'lib/sidekiq/web/router.rb', line 69

def initialize(request_method, pattern, block)
  @request_method = request_method
  @pattern = pattern
  @block = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block


65
66
67
# File 'lib/sidekiq/web/router.rb', line 65

def block
  @block
end

#nameObject

Returns the value of attribute name


65
66
67
# File 'lib/sidekiq/web/router.rb', line 65

def name
  @name
end

#patternObject

Returns the value of attribute pattern


65
66
67
# File 'lib/sidekiq/web/router.rb', line 65

def pattern
  @pattern
end

#request_methodObject

Returns the value of attribute request_method


65
66
67
# File 'lib/sidekiq/web/router.rb', line 65

def request_method
  @request_method
end

Instance Method Details

#compileObject


79
80
81
82
83
84
85
86
87
# File 'lib/sidekiq/web/router.rb', line 79

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

#match(request_method, path) ⇒ Object


89
90
91
92
93
94
95
96
97
98
# File 'lib/sidekiq/web/router.rb', line 89

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

#matcherObject


75
76
77
# File 'lib/sidekiq/web/router.rb', line 75

def matcher
  @matcher ||= compile
end