Module: GrapePathHelpers::AllRoutes

Defined in:
lib/grape-path-helpers/all_routes.rb

Overview

methods to extend Grape::API's behavior so it can get a list of routes from all APIs and decorate them with the DecoratedRoute class

Instance Method Summary collapse

Instance Method Details

#all_routesObject



28
29
30
31
# File 'lib/grape-path-helpers/all_routes.rb', line 28

def all_routes
  routes = descendants.flat_map { |d| d.endpoints.flat_map(&:routes) }
  routes.uniq { |r| r.options.merge(path: r.path) }
end

#decorated_routes_by_helper_nameObject

rubocop:disable Metrics/AbcSize



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/grape-path-helpers/all_routes.rb', line 8

def decorated_routes_by_helper_name # rubocop:disable Metrics/AbcSize
  return @decorated_routes_by_helper_name if @decorated_routes_by_helper_name

  routes = {}

  all_routes
    .map { |r| DecoratedRoute.new(r) }
    .sort_by { |r| -r.dynamic_path_segments.count }
    .each do |route|
      route.helper_names.each do |helper_name|
        key = helper_name.to_sym

        routes[key] ||= []
        routes[key] << route
      end
    end

  @decorated_routes_by_helper_name = routes
end