ruby-grape / ruby-grape/grape

Organization with blocks of methods

Open
#1,487 16 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Is there a simple way, to organize the methods with blocks for code folding, organization, etc?

What I'm thinking of using a short version of the example in the README, but wrapping methods and blocks in group blocks:

module Twitter
  class API < Grape::API
    version 'v1', using: :header, vendor: 'twitter'
    format :json
    prefix :api

    resource :statuses do
      group :public_timeline do
        desc 'Return a public timeline.'
        get :public_timeline do
          Status.limit(20)
        end
      end

      group :home_timeline do
        desc 'Return a personal timeline.'
        get :home_timeline do
          authenticate!
          current_user.statuses.limit(20)
        end
      end

      group :status do
        desc 'Return a status.'
        params do
          requires :id, type: Integer, desc: 'Status id.'
        end
        route_param :id do
          get do
            Status.find(params[:id])
          end
        end
      end
    end
  end
end

I'm finding the only way I can get this right now is to use Ruby's open classes, and then having a separate definition for each one. It feels weird for desc and params to not be block based, but rather order based.

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 with the short API example in the README and review how the existing DSL handles desc, params, routes, and nested blocks. Define whether a group block should scope these declarations while preserving the current API; done means the proposed organization works without separate open-class definitions and is covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.