2

I am writing an API with Grape mounted in a Ruby on Rails 5.2 application. I do have some extended API notes in markdown files that I would like to use directly in the desc block for the description.

I currently load the markdown as follows:


module V1
  class DonationsAPI < Grape::API
    helpers do
      # Gives the current donation
      # @return [Donation] donation
      def current_donation
        current_organization.donations.find(params[:id])
      end
    end

    desc 'Organization donations', headers: Base::COMMON_HEADER
    resource :donations do
      desc 'Retrieve all donations made to your organization' do
        http_codes [[200, 'Ok', DonationEntity::Show]]
        description File.read(Rails.root.join("app", "api", "v1", "docs", "donations_api", "index.md"))
      end
      get do
        serialize current_organization.donations.order('id asc'), is_collection: true
      end

      # ...
    end
  end
end

I would like to have File.read(Rails.root.join("app", "api", "v1", "docs", "donations_api", "index.md")) replaced with a method.

I tried the following:

    desc 'Organization donations', headers: Base::COMMON_HEADER
    resource :donations do
      desc 'Retrieve all donations made to your organization' do
        http_codes [[200, 'Ok', DonationEntity::Show]]
        description api_description('v1/docs/donations_api/index.md')
      end

I tried defining a method in class DoantionsAPI < Grape::API without success. I also tried to define a "helper" method. In both cases it keeps saying "undefined method".

How can I describe a method to get the job done?

Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71

0 Answers0