Skip to content

Organization with blocks of methods #1487

Open
@tibbon

Description

@tibbon

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions