Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apipie concerns
# A common concern,include into all doc modules
#
module BaseDoc
include Apipie::DSL::Concern
def namespace(namespace, options = {})
@namespace = namespace
@namespace_name = options[:name]
end
attr_reader :namespace_name
def resource(resource)
controller_name = resource.to_s.camelize + 'Controller'
(class << self; self; end).send(:define_method, :superclass) do
mod = @namespace.present? ? @namespace.classify.constantize : Object
mod.const_get(controller_name)
end
Apipie.app.set_resource_id(self, controller_name)
resource_description do
api_version @controller.namespace_name if @controller.namespace_name
end
end
def doc_for(action_name, &block)
instance_eval(&block)
api_version namespace_name if namespace_name
define_method(action_name) do
# ... define it in your controller with the real code, blank here
end
end
end
class UsersController < ApplicationController
# And use it, just 1 line in the controller
include UsersDoc
def show
# real code
end
def create
# real code
end
def index
# real code
end
end
# Resource-specific documentation
# include into controller
#
module UsersDoc
extend BaseDoc
resource :users
resource_description do
formats [:json]
api_versions 'public'
end
doc_for :index do
api :GET, '/users', 'List users'
param :role, String, 'Filter users by role'
end
doc_for :show do
api! 'Show user'
description 'Returns user with provided id'
param :id, String, 'Id of user you want to fetch'
end
doc_for :create do
api! 'Create user'
description 'Create user with specifed user params'
param :user, Hash, desc: 'User information' do
param :full_name, String, desc: 'Full name of the user you want to create'
param :age, Fixnum, desc: 'Age of the user you want to create'
end
end
end
@marlosirapuan

This comment has been minimized.

Copy link

@marlosirapuan marlosirapuan commented Feb 18, 2016

Thank you for this gist, but...
resource_description not working in this case

how fix it?

@iliabylich

This comment has been minimized.

Copy link
Owner Author

@iliabylich iliabylich commented Feb 19, 2016

@marlosirapuan This is the code extracted from my previous project and I don't like it.
try this one (a bit more extended and in fact easier to understand) - https://gist.github.com/iliabylich/c8032b193405673062e7

@marlosirapuan

This comment has been minimized.

Copy link

@marlosirapuan marlosirapuan commented Feb 22, 2016

@iliabylich thank you! works, except: def_param_group and param_group

i added def_param_group on application_doc:

def def_param_group(name, &block)
   Apipie.add_param_group(self, name, &block)
end

but param_group displays error

@kalpeshdave

This comment has been minimized.

Copy link

@kalpeshdave kalpeshdave commented Sep 26, 2019

resource_description . not working in this case. Any update?

@iliabylich

This comment has been minimized.

Copy link
Owner Author

@iliabylich iliabylich commented Sep 26, 2019

@kalpeshdave nope, I'm not maintaining this anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.