-
-
Save AlanGabbianelli/bcbf1253c32970f66146420358377dd8 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment