ruby-grape / ruby-grape/grape

Feature request: Pass array/Regexp to namespace/request methods

Open
#931 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Currently, if I have 2 endpoints I want to merge into one, all I can do is to loop and create requests:

%w(a b c).each do |key|
  namespace key do
    get do
      "hello #{key}"
    end
  end
end

%w(e f g).each do |key|
  namespace key do
    get do
      "hello again #{key}"
    end
  end
end

In the case above, I can't use named namespace (namespace ":key") because it'll take over both request groups.

A nicer way would be to be able to send an array or regexp to namespace or get:

namespace %w(a b c) do
  get do
    "hello"
  end
end

namespace %r[^(?<key>e|f|g)$] do # regexp to support named groups
  get do
    "hello #{params[:key]}"
  end
end

get %r[^(?<key>h|i|j)$] do
  "hello #{params[:key]}"
end

Kind of aliasing same requests to a single block (also saves memory) for API changes, backward compatibility etc.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the namespace and get request-handling entry points in the Grape codebase, then review how route patterns and named parameters are currently processed. Done means arrays and regular expressions work with namespace and request methods, including the named-group examples in the issue, without requiring repeated blocks.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.