-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpattern.rb
63 lines (51 loc) · 1.71 KB
/
pattern.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'forwardable'
require 'mustermann/grape'
module Grape
class Router
class Pattern
DEFAULT_PATTERN_OPTIONS = { uri_decode: true, type: :grape }.freeze
DEFAULT_SUPPORTED_CAPTURE = %i[format version].freeze
attr_reader :origin, :path, :capture, :pattern
extend Forwardable
def_delegators :pattern, :named_captures, :params
def_delegators :@regexp, :===
alias match? ===
def initialize(pattern, **options)
@origin = pattern
@path = build_path(pattern, **options)
@capture = extract_capture(options)
@pattern = Mustermann.new(@path, pattern_options)
@regexp = to_regexp
end
def to_regexp
@to_regexp ||= @pattern.to_regexp
end
private
def pattern_options
options = DEFAULT_PATTERN_OPTIONS.dup
options[:capture] = capture if capture.present?
options
end
def build_path(pattern, anchor: false, suffix: nil, **_options)
unless anchor || pattern.end_with?('*path')
pattern << '/' unless pattern.end_with?('/')
pattern << '*path'
end
pattern = pattern.split('/').tap do |parts|
parts[parts.length - 1] = '?' + parts.last
end.join('/') if pattern.end_with?('*path')
pattern + suffix.to_s
end
def extract_capture(requirements: {}, **options)
requirements = {}.merge(requirements)
supported_capture.each_with_object(requirements) do |field, capture|
option = Array(options[field])
capture[field] = option.map(&:to_s) if option.present?
end
end
def supported_capture
DEFAULT_SUPPORTED_CAPTURE
end
end
end
end