Skip to content

Instantly share code, notes, and snippets.

@iliabylich
Created June 4, 2015 18:40
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iliabylich/d140f3a8fcfbcafc41b8 to your computer and use it in GitHub Desktop.
Save iliabylich/d140f3a8fcfbcafc41b8 to your computer and use it in GitHub Desktop.
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
Copy link

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

how fix it?

@iliabylich
Copy link
Author

@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
Copy link

@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
Copy link

resource_description . not working in this case. Any update?

@iliabylich
Copy link
Author

@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