-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathversioned_helpers.rb
55 lines (53 loc) · 1.77 KB
/
versioned_helpers.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
# Versioning
module Spec
module Support
module Helpers
# Returns the path with options[:version] prefixed if options[:using] is :path.
# Returns normal path otherwise.
def versioned_path(options = {})
case options[:using]
when :path
File.join('/', options[:prefix] || '', options[:version], options[:path])
when :param
File.join('/', options[:prefix] || '', options[:path])
when :header
File.join('/', options[:prefix] || '', options[:path])
when :accept_version_header
File.join('/', options[:prefix] || '', options[:path])
else
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
end
end
def versioned_headers(options)
case options[:using]
when :path
{} # no-op
when :param
{} # no-op
when :header
{
'HTTP_ACCEPT' => [
"application/vnd.#{options[:vendor]}-#{options[:version]}",
options[:format]
].compact.join('+')
}
when :accept_version_header
{
'HTTP_ACCEPT_VERSION' => options[:version].to_s
}
else
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
end
end
def versioned_get(path, version_name, version_options = {})
path = versioned_path(version_options.merge(version: version_name, path: path))
headers = versioned_headers(version_options.merge(version: version_name))
params = {}
if version_options[:using] == :param
params = { version_options.fetch(:parameter, 'apiver') => version_name }
end
get path, params, headers
end
end
end
end