All Projects → qonto → Api_schema

qonto / Api_schema

Licence: mit
DSL for describing APIs and generate swagger json

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Api schema

Leaky Gems
A list of Ruby gems that have known memory leaks (and issues)
Stars: ✭ 895 (+4872.22%)
Mutual labels:  rails, rubygems
Unread
Handle unread records and mark them as read with Ruby on Rails
Stars: ✭ 638 (+3444.44%)
Mutual labels:  rails, rubygems
Bluedoc
An open-source document management tool for enterprise self host.
Stars: ✭ 579 (+3116.67%)
Mutual labels:  documentation, rails
Acts as api
makes creating API responses in Rails easy and fun
Stars: ✭ 506 (+2711.11%)
Mutual labels:  api, rails
Materialize Sass
Materializecss rubygem for Rails Asset Pipeline / Sprockets
Stars: ✭ 785 (+4261.11%)
Mutual labels:  rails, rubygems
Oas Kit
Convert Swagger 2.0 definitions to OpenAPI 3.0 and resolve/validate/lint
Stars: ✭ 516 (+2766.67%)
Mutual labels:  api, documentation
Versioncake
🍰 Version Cake is an unobtrusive way to version APIs in your Rails or Rack apps
Stars: ✭ 623 (+3361.11%)
Mutual labels:  api, rails
Verb
HEADS UP! Verb is going though a major transition, we've completely refactored everything from the ground up. If you're interested, please see the dev branch.
Stars: ✭ 442 (+2355.56%)
Mutual labels:  api, documentation
Kin Openapi
OpenAPI 3.0 implementation for Go (parsing, converting, validation, and more)
Stars: ✭ 776 (+4211.11%)
Mutual labels:  api, documentation
Wobike
Documentation of Bike Sharing APIs 🚴🛴🛵
Stars: ✭ 705 (+3816.67%)
Mutual labels:  api, documentation
Apiv2 Graphql Docs
AniList API V2 GraphQL Documentation
Stars: ✭ 501 (+2683.33%)
Mutual labels:  api, documentation
Rails Settings
Manage settings with Ruby on Rails
Stars: ✭ 807 (+4383.33%)
Mutual labels:  rails, rubygems
Awesome Openapi3
😎 A list of awesome projects related to OpenAPI 3.0.x, curated by the community
Stars: ✭ 469 (+2505.56%)
Mutual labels:  api, documentation
Fuzzapi
Fuzzapi is a tool used for REST API pentesting and uses API_Fuzzer gem
Stars: ✭ 521 (+2794.44%)
Mutual labels:  api, rails
Rails5 api tutorial
Learn how to build a modern API on Michael Hartl's Rails 5 tutorial
Stars: ✭ 458 (+2444.44%)
Mutual labels:  api, rails
Mailcatcher
Catches mail and serves it through a dream.
Stars: ✭ 5,512 (+30522.22%)
Mutual labels:  rails, rubygems
Deep pluck
Allow you to pluck attributes from nested associations without loading a bunch of records.
Stars: ✭ 385 (+2038.89%)
Mutual labels:  rails, rubygems
Cljdoc
📚 A central documentation hub for the Clojure community
Stars: ✭ 416 (+2211.11%)
Mutual labels:  api, documentation
Api Pagination
📄 Link header pagination for Rails and Grape APIs.
Stars: ✭ 641 (+3461.11%)
Mutual labels:  api, rails
Graphiti
Stylish Graph APIs
Stars: ✭ 783 (+4250%)
Mutual labels:  api, rails

Api Schema Build Status Gem Version

Provides a framework and DSL for describing APIs and generate swagger-json using minimalist, schema.db-like syntax.

Installation | Usage | License

Installation

Add this line to your application's Gemfile:

gem 'api_schema'

And then execute:

$ bundle

Or install it yourself as:

$ gem install api_schema

Usage

Just add include ApiSchema and configurations to your base class and inherit from it. You also should define your default :error_model

To generate json use BaseDocs.generate_json method.

BaseDocs

module V1
  class BaseDocs
    include ApiSchema

    configure do |config|
      config.title = 'Users API'
      config.description = 'API for users'
      config.version = '1.0'
      config.host = 'sample.com'
      config.base_path = '/api'
      config.terms_of_service = 'https://sample.com/terms'
      config.contact_name = 'API Team'
      config.consumes = 'application/json'
      config.produces = 'application/json'
      config.authorization = true
      config.error_model = :error_model
      config.error_desc = {
        '401' => "Unauthorized",
        '403' => "Forbidden",
        '404' => "Not found",
        '422' => "Unprocessable Entity"
      }
    end

    serializer :error_model do |f|
      f.integer :code, required: true
      f.string :message, required: true
    end
  end
end

UsersController

module V1
  module ControllersDocs
    class UsersController < BaseDocs

      get do
        path_param :id, :integer
        name 'Get User'
        desc 'Returns user with provided id'
        response 200, :user
        error! 401, 403, 404, 422
      end
    end
  end

UserSerializer

module V1
  module SerializersDocs
    class UserSerializer < BaseDocs

      serializer :user do |f|
        f.integer  :id, required: true
        f.string   :email, required: true
        f.string   :name
      end
    end
  end

Serializers

To describe serializers you can use serializer and array_serializer methods.

Here :user and :users are unique identifiers

For member responses:

serializer :user do |f|
  f.integer  :id, required: true
  f.string   :email, required: true
  f.string   :name
end

Will have such a structure:

{
  "user": {
    "id": 1,
    "email": "john.doe.gmail.com",
    "name": "John Doe"
  }
}

For collection responses:

array_serializer :users do |f|
  f.reference :meta
  f.reference :user, type: :array
end

Will have such a structure:

{
  "meta": {...},
  "users": [
    {
      "id": 1,
      "email": "john.doe.gmail.com",
      "name": "John Doe"
    },
    {...}]
}

Then you can use this descriptions in the controllers with identifiers:

response 200, :user
response 200, :users

References

To user nested descriptions, you can use reference method:

serializer :file do |f|
  f.integer :file_name, required: true
  f.string  :file_url, required: true
end
serializer :attachment do |f|
  f.integer   :id, required: true
  f.reference :file
end

Parents

To inherit fields from another serializer, you can use parent parameter:

serializer :file do |f|
  f.integer   :file_name, required: true
  f.string    :file_url, required: true
end
serializer :attachment, parent: :file do |f|
  f.integer   :id, required: true
end

Controllers

Endpoints

To describe endpoints you can use get, post, put, patch methods.

Get collection:

get do
  query_param :query, :string
  query_param :sort_by, :string
  name 'List Users'
  desc "Returns list of the users"
  response 200, :users
  error! 401, 403, 422
end

Will produce /users endpoint.

To get member you should use path_param method:

get do
  path_param :id, :integer
  name 'Get User'
  desc 'Returns user with provided id'
  response 200, :user
  error! 401, 403, 422
end

Will produce /users/{id} endpoint.

By default gem uses controller's name to generate endpoints, but you can make custom by passing first argument:

get 'people' do
  path_param :id, :integer
  name 'Get User'
  desc 'Returns user with provided id'
  response 200, :user
  error! 401, 403, 422
end

Will produce /people/{id} endpoint.

Use extra_path argument to add extra path to the endpoint

get extra_path: :read do
  path_param :id, :integer
  name 'Read Notification'
  desc 'Reads notification with provided id'
  response 200
  error! 401, 403, 422
end

Will produce /notification/{id}/read endpoint.

Parameters

To describe each endpoint you can use header, body, path_param, query_param

header and body:

post do
  header :token, :string
  body :create_user
  name 'Create User'
  desc 'Creates and returns new user'
  response 200, :user
  error! 401, 403, 422
end

To describe body of the request you can use request_body method. It's just an alias for serializer:

request_body :create_user do |f|
  f.string   :email, required: true
  f.string    :first_name, required: true
  f.string    :last_name, required: true
end

Dependencies

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].