Class: Jets::Resource::ApiGateway::RestApi::Routes::Collision
- Inherits:
-
Object
- Object
- Jets::Resource::ApiGateway::RestApi::Routes::Collision
show all
- Defined in:
- lib/jets/resource/api_gateway/rest_api/routes/collision.rb,
lib/jets/resource/api_gateway/rest_api/routes/collision/variable_exception.rb
Defined Under Namespace
Classes: VariableException
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Collision.
5
6
7
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 5
def initialize
@collisions = []
end
|
Instance Attribute Details
#collisions ⇒ Object
Returns the value of attribute collisions.
4
5
6
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 4
def collisions
@collisions
end
|
Instance Method Details
#collision?(paths) ⇒ Boolean
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 9
def collision?(paths)
paths = paths_with_variables(paths)
parents = variable_parents(paths)
collide = false
parents.each do |parent|
collide ||= variable_collision_exists?(parent, paths)
end
collide
end
|
#collision_message ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 24
def collision_message
<<~EOL
There are routes with sibling variables under the same parent that collide.
Collisions:
#{@collisions.join("\n ")}
API Gateway only allows one unique variable path You must use the same variable name within
the same parent route path.
Example: /posts/:id and /posts/:post_id/reveal should both be /posts/:id and /posts/:id/reveal.
Please check your `config/routes.rb` and remove the colliding routes.
More info: http://rubyonjets.com/docs/considerations/api-gateway/
EOL
end
|
#direct_parent?(parent, path) ⇒ Boolean
78
79
80
81
82
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 78
def direct_parent?(parent, path)
leaf = variable_leaf(path)
leaf_parent = leaf.split('/')[0..-2].join('/')
parent == leaf_parent
end
|
#exception ⇒ Object
20
21
22
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 20
def exception
VariableException.new(collision_message)
end
|
#parent?(parent, path) ⇒ Boolean
68
69
70
71
72
73
74
75
76
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 68
def parent?(parent, path)
return false if parent == path
parent_parts = parent.split('/')
path_parts = path.split('/')
n = parent_parts.size-1
parent_parts[0..n] == path_parts[0..n]
end
|
#parent_variables(parent, paths) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 59
def parent_variables(parent, paths)
paths = paths.select do |path|
parent?(parent, path)
end
paths.map do |path|
path.sub("#{parent}/",'').gsub(%r{/.*},'')
end.select { |x| x =~ /^:/ }.uniq.sort
end
|
#paths_with_variables(paths) ⇒ Object
117
118
119
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 117
def paths_with_variables(paths)
paths.select { |p| p.include?(':') }.uniq
end
|
#register_collision(parent, variables) ⇒ Object
register collision for later display We don’t register the full path but this might be more helpful.
50
51
52
53
54
55
56
57
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 50
def register_collision(parent, variables)
return unless variables.uniq.size
variables.each do |v|
@collisions << "#{parent}/#{v}"
end
@collisions.uniq!
end
|
#variable_collision_exists?(parent, paths) ⇒ Boolean
40
41
42
43
44
45
46
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 40
def variable_collision_exists?(parent, paths)
paths = paths_with_variables(paths)
variables = parent_variables(parent, paths)
collide = variables.uniq.size > 1
register_collision(parent, variables) if collide
collide
end
|
#variable_leaf(path) ⇒ Object
Strips the path down until only the leaf node part is a variable Example: users/:user_id/posts/:post_id/edit Returns: users/:user_id/posts
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 105
def variable_leaf(path)
return '' unless path.include?(':')
parts = path.split('/')
is_variable = parts.last.include?(':')
until is_variable
parts.pop
is_variable = parts.last.include?(':')
end
parts[0..-1].join('/') end
|
#variable_parent(path) ⇒ Object
Strips the path down until only the leaf node part is a variable Example: users/:user_id/posts/:post_id/edit Returns: users/:user_id/posts
96
97
98
99
100
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 96
def variable_parent(path)
path = variable_leaf(path)
path.split('/')[0..-2].join('/')
end
|
#variable_parents(paths) ⇒ Object
84
85
86
87
88
89
90
91
|
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 84
def variable_parents(paths)
parents = []
paths = paths_with_variables(paths)
paths.each do |path|
parents << variable_parent(path)
end
parents.uniq.sort
end
|