File: all_routes.rb

package info (click to toggle)
ruby-grape-path-helpers 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 192 kB
  • sloc: ruby: 814; makefile: 3
file content (31 lines) | stat: -rw-r--r-- 906 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module GrapePathHelpers
  # 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
  module AllRoutes
    def decorated_routes_by_helper_name
      return @decorated_routes_by_helper_name if @decorated_routes_by_helper_name # rubocop:disable Metrics/LineLength

      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

    def all_routes
      routes = descendants.flat_map { |s| s.send(:prepare_routes) }
      routes.uniq { |r| r.options.merge(path: r.path) }
    end
  end
end