Class: Jets::Resource::ApiGateway::RestApi::Routes::Collision

Inherits:
Object
  • Object
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

#initializeCollision

Returns a new instance of Collision.



7
8
9
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 7

def initialize
  @collisions = []
end

Instance Attribute Details

#collisionsObject (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?(paths) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 11

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_messageObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 26

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

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 80

def direct_parent?(parent, path)
  leaf = variable_leaf(path)
  leaf_parent = leaf.split('/')[0..-2].join('/')
  parent == leaf_parent
end

#exceptionObject



22
23
24
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 22

def exception
  VariableException.new(collision_message)
end

#parent?(parent, path) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 70

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



61
62
63
64
65
66
67
68
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 61

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



119
120
121
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 119

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.



52
53
54
55
56
57
58
59
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 52

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

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 42

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



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



86
87
88
89
90
91
92
93
# File 'lib/jets/resource/api_gateway/rest_api/routes/collision.rb', line 86

def variable_parents(paths)
  parents = []
  paths = paths_with_variables(paths)
  paths.each do |path|
    parents << variable_parent(path)
  end
  parents.uniq.sort
end