Sha256: 5d74768d37d35e02ccf23c221c4e3cf99d20b4ccccebef4caa3f75990b9787b8

Contents?: true

Size: 906 Bytes

Versions: 4

Compression:

Stored size: 906 Bytes

Contents

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
grape-path-helpers-2.0.1 lib/grape-path-helpers/all_routes.rb
grape-path-helpers-2.0.0 lib/grape-path-helpers/all_routes.rb
grape-path-helpers-1.7.1 lib/grape-path-helpers/all_routes.rb
grape-path-helpers-1.7.0 lib/grape-path-helpers/all_routes.rb