Code Monkey home page Code Monkey logo

swaggard's Introduction

Swaggard

Swaggard is a Rails Engine that can be used to document a REST api. It does this by generating a json that is compliant with Swagger and displaying it using Swagger-ui. This gem is inspired and based on SwaggerYard by Chris Trinh.

Swaggard vs SwaggerYard

The main reason this gem exists is to avoid having to write by hand some information and use what rails already give us ie: controllers names and methods paths.

And also:

  • Bring support for Rails 4.
  • Bring support for Swagger 2.
  • Bring support for models (serializers).
  • Bring support for form and body params.
  • and more...

Installation

Put Swaggard in your Gemfile:

gem 'swaggard'

Install the gem with Bundler:

bundle install

Getting Started

Place your configuration in a your rails initializers

# config/initializers/swaggard.rb

Swaggard.configure do |config|
  config.api_version = '0.1'
  config.doc_base_path = 'http://swagger.example.com/doc'
  config.api_base_path = 'http://swagger.example.com/api'
end

Mount your engine

# config/routes.rb

mount Swaggard::Engine, at: '/swagger'

Access your service documentation

open http://localhost:3000/swagger

Access the raw swagger json

open http://localhost:3000/swagger.json

Example

By just using Swaggard you'll get documentation for the endpoints that exist on your service: http method, path, path params. And grouping will be done based on the controller that holds each path.

This is fine base but you should add more documentation in order to provide more information of the expected inputs and outputs or even change the grouping of the endpoints.

Here is a example of how to use Swaggard

# app/controllers/users/posts_controller.rb

# User posts
#
# API for creating, deleting, and listing user posts.
class User::PostsController < ActionController::Base

  # Returns the list of user posts
  #
  # @response_class Array<PostSerializer>
  def index
    ...
  end

  # Create user post
  #
  # @body_parameter [string] title
  # @body_parameter [string] body
  # @body_parameter [string] topic_id
  #
  # @response_class PostSerializer
  def create
    ...
  end

end


# app/serializers/post_serializer.rb

# @attr [integer] id
# @attr [string] title
# @attr [string] body
# @attr [date-time] created_at
# @attr [date-time] updated_at
# @attr [TopicSerializer] topic
class PostSerializer < ActiveModel::Serializer

  attribute :id
  attribute :title
  attribute :body
  attribute :created_at
  attribute :updated_at

  has_one :topic, serializer: TopicSerializer

end


# app/serializers/topic_serializer.rb

# @attr [integer] id
# @attr [string] name
# @attr [date-time] created_at
# @attr [date-time] updated_at
class TopicSerializer < ActiveModel::Serializer

  attribute :id
  attribute :name
  attribute :created_at
  attribute :updated_at

end

Will generate

Web UI

Primitive type

When one of this types is given Swaggard will handle them directly instead of searching for a definition:

  • integer
  • long
  • float
  • double
  • string
  • byte
  • binary
  • boolean
  • date
  • date-time
  • password
  • hash

Authentication

Swaggard supports two types of authentication: header and query.

You can configure it as follows:

# config/initializers/swaggard.rb
Swaggard.configure do |config|
  config.authentication_type  = 'header' # Defaults to 'query'
  config.authentication_key   = 'X-AUTH-TOKEN' # Defaults to 'api_key'
  config.authentication_value = 'your-secret-key' # Initial value for authentication. Defaults to ''
end

Even if you provide a authentication_value you can later change it from the ui.

Access authorization

Swaggard supports access authorization.

You can configure it as follows:

# config/initializers/swaggard.rb
Swaggard.configure do |config|
  config.access_username  = 'admin'
  config.access_password   = 'password'
end

If you not set access_username, everyone will have access to Swagger documentation.

Default content type

You can set default content type in Swaggard configuration as follows:

# config/initializers/swaggard.rb
Swaggard.configure do |config|
  config.default_content_type = 'application/json'
end

If you set default_content_type, Swagger will use it in example request.

Caching

You can improve Swagger performance by using caching. You can enable use_cache in Swaggard configuration as follows:

# config/initializers/swaggard.rb
Swaggard.configure do |config|
  config.use_cache = Rails.env.production?
end

If you set use_cache as Rails.env.production?, Swagger will use cache only in production mode.

Note. For cache clearing you can execute rake swaggard:clear_cache.

Documentation Scoping

Its possible to only generate Swagger documentation for a subset of your application controllers to do this you just need to use the controllers_path config option. For instance to only generate documentation for the controllers under app/controllers/api you need do this:

# config/initializers/swaggard.rb
Swaggard.configure do |config|
  ...
  config.controllers_path = "#{Rails.root}/app/controllers/api/**/*.rb"
  ...
end

The default value for controllers_path is "#{Rails.root}/app/controllers/**/*.rb".

More Information

Author

Adrian Gomez

Credits

Chris Trinh author of SwaggerYard in which this gem is inspired and used a base.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.