Module: Aserto::Sinatra::Utils

Defined in:
lib/aserto/sinatra/utils.rb

Class Method Summary collapse

Class Method Details

.route(request) ⇒ Object

Finds the Sinatra route for the given path. If the route is not found, returns nil. If the route is found, returns a hash with the following keys: :path :action

Eg: route(“/api/v1/users/1”) => {

:action => :GET,
:path => "/api/v1/users/:id" }


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
# File 'lib/aserto/sinatra/utils.rb', line 17

def route(request)
  return unless defined? ::Sinatra

  path = request.path_info
  routes = ::Sinatra::Application.routes[request.request_method].map do |route|
    route.flatten.first
  end
  route = routes.find do |r|
    r.match(path)
  end

  return unless route

  substitutions = route.match(path).named_captures
  unless substitutions&.any?
    return {
      path: route.to_s,
      action: request.request_method.to_sym
    }
  end

  substitutions.each_pair do |sub, val|
    path.sub!(val, ":#{sub}")
  end
  {
    path: path,
    action: request.request_method.to_sym
  }
end