Sha256: 92600db5e814656470e58fe56edb120d99336b3f25234543da218e8765fb7e0b

Contents?: true

Size: 1.6 KB

Versions: 7

Compression:

Stored size: 1.6 KB

Contents

class <%= name.classify.pluralize %>Api < Grape::API
  represent <%= name.classify %>, with: <%= name.classify %>Entity
  desc 'Get a list of <%= name.underscore.tableize %>', entity: <%= name.classify %>Entity
  params do
    optional :ids, type: String, desc: 'comma separated <%= name.underscore %> ids'
  end
  get do
    <%= name.underscore.tableize %> = <%= name.classify %>.filter(declared(params))
    present <%= name.underscore.tableize %>
  end

  desc 'Create an <%= name.underscore %>', entity: <%= name.classify %>Entity
  params do
  end

  post do
    begin
      <%= name.underscore %> = <%= name.classify %>.create(declared(params))
    rescue NameError
      error!(present_error(:kind_not_found))
    end
    error!(present_error(:record_invalid, <%= name.underscore %>.errors.full_messages)) unless <%= name.underscore %>.errors.empty?
    present <%= name.underscore %>
  end

  params do
    requires :id, desc: 'ID of the <%= name.underscore %>'
  end
  route_param :id do
    desc 'Get an <%= name.underscore %>', entity: <%= name.classify %>Entity
    get do
      <%= name.underscore %> = <%= name.classify %>.find(params[:id])
      present <%= name.underscore %>
    end

    desc 'Update an <%= name.underscore %>', entity: <%= name.classify %>Entity
    params do
    end
    put do
      # fetch <%= name.underscore %> record and update attributes.  exceptions caught in app.rb
      <%= name.underscore %> = <%= name.classify %>.find(params[:id])
      <%= name.underscore %>.update_attributes!(declared(params).select { |param, value| value.present? })
      present <%= name.underscore %>
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
napa-0.1.21 lib/napa/generators/templates/api/app/apis/%name_tableize%_api.rb.tt
napa-0.1.20 lib/napa/generators/templates/api/app/apis/%name_tableize%_api.rb.tt
napa-0.1.19 lib/napa/generators/templates/api/app/apis/%name_tableize%_api.rb.tt
napa-0.1.18 lib/napa/generators/templates/api/app/apis/%name_tableize%_api.rb.tt
napa-0.1.17 lib/napa/generators/templates/api/app/apis/%name_tableize%_api.rb.tt
napa-0.1.16 lib/napa/generators/templates/api/app/apis/%name_tableize%_api.rb.tt
napa-0.1.15 lib/napa/generators/templates/api/app/apis/%name_tableize%_api.rb.tt