Created
June 4, 2015 18:40
-
-
Save iliabylich/d140f3a8fcfbcafc41b8 to your computer and use it in GitHub Desktop.
Apipie concerns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 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
@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
resource_description .
not working in this case. Any update?
@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
Thank you for this gist, but...
resource_description
not working in this casehow fix it?