-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpath.rb
93 lines (74 loc) · 1.91 KB
/
path.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
module Grape
# Represents a path to an endpoint.
class Path
attr_reader :raw_path, :namespace, :settings
def initialize(raw_path, namespace, settings)
@raw_path = raw_path
@namespace = namespace
@settings = settings
end
def mount_path
settings[:mount_path]
end
def root_prefix
settings[:root_prefix]
end
def uses_specific_format?
return false unless settings.key?(:format) && settings.key?(:content_types)
settings[:format] && Array(settings[:content_types]).size == 1
end
def uses_path_versioning?
return false unless settings.key?(:version) && settings[:version_options]&.key?(:using)
settings[:version] && settings[:version_options][:using] == :path
end
def namespace?
namespace&.match?(/^\S/) && not_slash?(namespace)
end
def path?
raw_path&.match?(/^\S/) && not_slash?(raw_path)
end
def suffix
if uses_specific_format?
"(.#{settings[:format]})"
elsif !uses_path_versioning? || (namespace? || path?)
'(.:format)'
else
'(/.:format)'
end
end
def path
PartsCache[parts]
end
def path_with_suffix
"#{path}#{suffix}"
end
def to_s
path_with_suffix
end
private
class PartsCache < Grape::Util::Cache
def initialize
super
@cache = Hash.new do |h, parts|
h[parts] = Grape::Router.normalize_path(parts.join('/'))
end
end
end
def parts
[].tap do |parts|
add_part(parts, mount_path)
add_part(parts, root_prefix)
parts << ':version' if uses_path_versioning?
add_part(parts, namespace)
add_part(parts, raw_path)
end
end
def add_part(parts, value)
parts << value if value && not_slash?(value)
end
def not_slash?(value)
value != '/'
end
end
end