-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpath.rb
70 lines (56 loc) · 1.45 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
module Grape
class Path
def self.prepare(raw_path, namespace, settings)
Path.new(raw_path, namespace, settings).path_with_suffix
end
attr_reader :raw_path, :namespace, :settings
def initialize(raw_path, namespace, settings)
@raw_path = raw_path
@namespace = namespace
@settings = settings
end
def mount_path
split_setting(:mount_path, '/')
end
def root_prefix
split_setting(:root_prefix, '/')
end
def uses_path_versioning?
!!(settings[:version] && settings[:version_options][:using] == :path)
end
def has_namespace?
namespace && namespace.to_s =~ /^\S/ && namespace != '/'
end
def has_path?
raw_path && raw_path.to_s =~ /^\S/ && raw_path != '/'
end
def suffix
if !uses_path_versioning? || (has_namespace? || has_path?)
'(.:format)'
else
'(/.:format)'
end
end
def path
Rack::Mount::Utils.normalize_path(parts.join('/'))
end
def path_with_suffix
"#{path}#{suffix}"
end
def to_s
path_with_suffix
end
private
def parts
parts = [mount_path, root_prefix].compact
parts << ':version' if uses_path_versioning?
parts << namespace.to_s
parts << raw_path.to_s
parts.flatten.reject { |part| part == '/' }
end
def split_setting(key, delimiter)
return if settings[key].nil?
settings[key].to_s.split("/")
end
end
end