class ApidocoGenerator

ApidocoGenerator

@author sufinsha

Public Instance Methods

create_apidoco_folder() click to toggle source
# File lib/generators/apidoco_generator.rb, line 10
def create_apidoco_folder
  resource = args[0]

  resource_actions = actions(args[1..-1])

  resource_actions.each do |action|
    create_file "#{Rails.root}/#{Apidoco.base_path}/#{file_name(resource, action)}",
                file_content(resource, action)
  end
end

Private Instance Methods

actions(args) click to toggle source
# File lib/generators/apidoco_generator.rb, line 23
def actions(args)
  return args if args.present?

  %i[show index create update destroy]
end
api_name(resource, action) click to toggle source
# File lib/generators/apidoco_generator.rb, line 50
def api_name(resource, action)
  endpoint_with_method = default_end_points_with_method(action.intern)
  resource_title = if endpoint_with_method[:collection]
                     resource.pluralize.titleize
                   else
                     resource.singularize.titleize
                   end
  "#{action.to_s.titleize} #{resource_title}"
end
default_end_points_with_method(action) click to toggle source
# File lib/generators/apidoco_generator.rb, line 29
def default_end_points_with_method(action)
  end_points_with_method = {
    index: {
      endpoint: '.json', method: 'GET', collection: true
    },
    show: {
      endpoint: '/:id.json', method: 'GET', collection: false
    },
    create: {
      endpoint: '.json', method: 'POST', collection: true
    },
    update: {
      endpoint: '/:id.json', method: 'PUT|PATCH', collection: false
    },
    destroy: {
      endpoint: '/:id.json', method: 'DELETE', collection: false
    }
  }
  end_points_with_method[action] || {}
end
file_content(resource, action) click to toggle source
# File lib/generators/apidoco_generator.rb, line 68
  def file_content(resource, action)
    endpoint_with_method = default_end_points_with_method(action.intern)
    name = api_name(resource_name(resource), action)
    <<-FILE
{
  "published": true,
  "name": "#{name}",
  "end_point": "#{resource}#{endpoint_with_method[:endpoint]}",
  "http_method": "#{endpoint_with_method[:method]}",
  "params": [],
  "header": {},
  "examples": [{
    "request": {},
    "response": {}
  }]
}
FILE
  end
file_name(resource, action) click to toggle source
# File lib/generators/apidoco_generator.rb, line 60
def file_name(resource, action)
  "#{resource}/#{action}.json"
end
resource_name(resource) click to toggle source
# File lib/generators/apidoco_generator.rb, line 64
def resource_name(resource)
  resource.split('/').last
end