Class: Jets::Resource::ApiGateway::RestApi::Routes::Collision
- 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
-
#collisions ⇒ Object
readonly
Returns the value of attribute collisions.
Instance Method Summary collapse
- #collision? ⇒ Boolean
- #direct_parent?(parent, path) ⇒ Boolean
- #exception ⇒ Object
-
#initialize(routes) ⇒ Collision
constructor
A new instance of Collision.
- #parent?(parent, path) ⇒ Boolean
- #parent_variables(parent, paths) ⇒ Object
- #paths_with_variables(paths) ⇒ Object
-
#register_collision(parent, variables) ⇒ Object
register collision for later display We don’t register the full path but this might be more helpful.
- #variable_collision_exists?(parent, paths) ⇒ Boolean
-
#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.
-
#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/:post_id.
- #variable_parents(paths) ⇒ Object
Methods inherited from Base
Methods included from AwsServices
#apigateway, #cfn, #lambda, #logs, #s3, #s3_resource, #sns, #sqs, #sts
Methods included from AwsServices::StackStatus
#lookup, #stack_exists?, #stack_in_progress?
Constructor Details
#initialize(routes) ⇒ Collision
Returns a new instance of Collision.
7 8 9 10 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 7 def initialize(routes) @routes = routes @collisions = [] end |
Instance Attribute Details
#collisions ⇒ Object (readonly)
Returns the value of attribute collisions.
6 7 8 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 6 def collisions @collisions end |
Instance Method Details
#collision? ⇒ Boolean
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 12 def collision? paths = paths_with_variables(@routes.map(&:path)) parents = variable_parents(paths) collide = false parents.each do |parent| collide ||= variable_collision_exists?(parent, paths) end collide end |
#direct_parent?(parent, path) ⇒ Boolean
76 77 78 79 80 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 76 def direct_parent?(parent, path) leaf = variable_leaf(path) leaf_parent = leaf.split('/')[0..-2].join('/') parent == leaf_parent end |
#exception ⇒ Object
23 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 23 def exception = <<~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 VariableException.new() end |
#parent?(parent, path) ⇒ Boolean
68 69 70 71 72 73 74 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 68 def parent?(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.uniq.sort end |
#paths_with_variables(paths) ⇒ Object
91 92 93 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 91 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 # check again just in case 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
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 107 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('/') # parent 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/:post_id
98 99 100 101 102 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 98 def variable_parent(path) path = variable_leaf(path) # drop last variable to leave behind the parent path.split('/')[0..-2].join('/') end |
#variable_parents(paths) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 82 def variable_parents(paths) parents = [] paths = paths_with_variables(paths) paths.each do |path| parents << variable_parent(path) end parents.uniq.sort end |