• Stars
    star
    234
  • Rank 171,630 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automated API documentation from Rspec

Build Status Code Climate Test Coverage

Dox

Automate your documentation writing process! Dox generates API documentation from Rspec controller/request specs in a Rails application. It formats the tests output in the OpenApi format. Use the ReDoc renderer for generating and displaying the documentation as HTML.

Here's a demo app.

Installation

Add this line to your application's Gemfile:

group :test do
  gem 'dox', require: false
end

And then execute:

$ bundle

Or install it yourself as:

$ gem install dox

Usage

Require it

Require Dox in the rails_helper:

require 'dox'

Configure it

Set these optional options in the rails_helper:

Option Value Description
descriptions_location Pathname instance or fullpath string Folder containing markdown descriptions of resources.
schema_request_folder_path Pathname instance or fullpath string Folder with request schemas of resources.
schema_response_folder_path Pathname instance or fullpath string Folder with response schemas of resources.
schema_response_fail_file_path Pathname instance or fullpath string Json file that contains the default schema of a failed response.
openapi_version string Openapi version (default: '3.0.0' )
api_version string Api Version (default: '1.0')
title string Documentation title (default: 'API Documentation')
header_description Pathname instance or string Description (header) of the documentation (default: ''). If pathname ends with .md, the file is looked in descriptions_location folder
headers_whitelist Array of headers (strings) Requests and responses will by default list only Content-Type header. To list other http headers, you must whitelist them.

Example:

Dox.configure do |config|
  config.descriptions_location  = Rails.root.join('spec/docs/v1/descriptions')
  config.schema_request_folder_path = Rails.root.join('spec/docs/v1/schemas')
  config.schema_response_folder_path = Rails.root.join('spec/support/v1/schemas')
  config.schema_response_fail_file_path = Rails.root.join('spec/support/v1/schemas/error.json')
  config.headers_whitelist = ['Accept', 'X-Auth-Token']
  config.title = 'API'
  config.api_version = '2.0'
  config.header_description = 'api_description.md'
end

Basic example

Define a descriptor module for a resource using Dox DSL:

module Docs
  module V1
    module Bids
      extend Dox::DSL::Syntax

      # define common resource data for each action
      document :api do
        resource 'Bids' do
          group 'Bids'
          desc 'bids.md'
        end
      end

      # define data for specific action
      document :index do
        action 'Get bids'
      end
    end
  end
end

You can define the descriptors for example in specs/docs folder, just make sure you load them in the rails_helper.rb:

Dir[Rails.root.join('spec/docs/**/*.rb')].each { |f| require f }

Include the descriptor modules in a controller and tag the specs you want to document with dox:

describe Api::V1::BidsController, type: :controller do
  # include resource module
  include Docs::V1::Bids::Api

  describe 'GET #index' do
    # include action module
    include Docs::V1::Bids::Index

    it 'returns a list of bids', :dox do
      get :index
      expect(response).to have_http_status(:ok)
    end
  end
end

And generate the documentation.

Advanced options

Before running into any more details, here's roughly how the generated OpenApi document is structured:

  • openapi
  • info
  • paths
    • action 1
      • tag1
      • example 1
      • example 2
    • action 2
      • tag2
      • example 3
  • x-tagGroups - tags1 - tag 1 - tag 2 - tags2 - tag 3 - tag 4
  • tags
    • tag1
    • tag2

OpenApi and info are defined in a json file as mentioned before. Examples are concrete test examples (you can have multiple examples for both happy and fail paths). They are completely automatically generated from the request/response objects. And you can customize the following in the descriptors:

  • x-tagGroup (resourceGroup)
  • tag (resource)
  • action
  • example

ResourceGroup

ResourceGroup contains related resources and is defined with:

  • name (required)
  • desc (optional, inline string or relative filepath)

Example:

document :bids_group do
  group 'Bids' do
    desc 'Here are all bid related resources'
  end
end

You can omit defining the resource group, if you don't have any description for it. Related resources will be linked in a group by the group option at the resource definition.

Resource

Resource contains actions and is defined with:

  • name (required)
  • group (required; to associate it with the related group)
  • desc (optional; inline string or relative filepath)

Example:

document :bids do
  resource 'Bids' do
    group 'Bids'
    desc 'bids/bids.md'
  end
end

Usually you'll want to define resourceGroup and resource together, so you don't have to include 2 modules with common data per spec file:

document :bids_common do
  group 'Bids' do
    desc 'Here are all bid related resources'
  end

  resource 'Bids' do
    group 'Bids'
    desc 'bids/bids.md'
  end
end

Action

Action contains examples and is defined with:

  • name (required)
  • path* (optional)
  • verb* (optional)
  • params (optional; depricated)
  • query_params (optional; more info)
  • desc (optional; inline string or relative filepath)
  • request_schema (optional; inline string or relative filepath)
  • response_schema_success (optional; inline string or relative filepath)
  • response_schema_fail (optional; inline string or relative filepath)

* these optional attributes are guessed (if not defined) from the request object of the test example and you can override them.

Example:

show_params = { id: { type: :number, required: :required, value: 1, description: 'bid id' } }
query_params = [ {
  "in": "query",
  "name": "filter",
  "required": false,
  "style": "deepObject",
  "explode": true,
  "schema": {
    "type": "object",
    "required": ["updated_at_gt"],
    "example": {
      "updated_at_gt": "2018-02-03 10:30:00"
    },
    "properties": {
      "updated_at_gt": {
        "type": "string",
        "title": "date"
      }
    }
  }
]

document :action do
  action 'Get bid' do
    path '/bids/{id}'
    verb 'GET'
    params show_params
    query_params query_params
    desc 'Some description for get bid action'
    request_schema 'namespace/bids'
    response_schema_success 'namespace/bids_s'
    response_schema_fail 'namespace/bids_f'
  end
end

Generate documentation

Documentation is generated in 2 steps:

  1. generate OpenApi json file: bundle exec rspec --tag apidoc -f Dox::Formatter --order defined --tag dox --out spec/api_doc/v1/schemas/docs.json

  2. render HTML with Redoc: redoc-cli bundle -o public/api/docs/v2/docs.html spec/api_doc/v1/schemas/docs.json

Use rake tasks

It's recommendable to write a few rake tasks to make things easier. Here's an example:

namespace :dox do
  desc 'Generate API documentation markdown'

  task :json, [:version, :docs_path, :host] => :environment do |_, args|
    require 'rspec/core/rake_task'
    version = args[:version] || :v1

    RSpec::Core::RakeTask.new(:api_spec) do |t|
      t.pattern = "spec/requests/api/#{version}"
      t.rspec_opts =
        "-f Dox::Formatter --tag dox --order defined --out spec/docs/#{version}/apispec.json"
    end

    Rake::Task['api_spec'].invoke
  end

  task :html, [:version, :docs_path, :host] => :json do |_, args|
    version = args[:version] || :v1
    docs_path = args[:docs_path] || "api/#{version}/docs"

    `yarn run redoc-cli bundle -o public/#{docs_path}/index.html spec/docs/#{version}/apispec.json`
  end

  task :open, [:version, :docs_path, :host] => :html do |_, args|
    version = args[:version] || :v1
    docs_path = args[:docs_path] || "api/#{version}/docs"

    `open public/#{docs_path}/index.html`
  end
end

Renderers

You can render the HTML yourself with ReDoc:

Common issues

You might experience some strange issues when generating the documentation. Here are a few examples of what we've encountered so far.

Uninitialized constant errors

There seems to be a problem with rspec-rails versions 3.7 and later not automatically requiring the project's rails_helper.rb when run with the --format flag.

To fix this issue, generate your documentation with --require rails_helper:

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/infinum/dox. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Credits

Dox is maintained and sponsored by Infinum.

Infinum

License

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

More Repositories

1

android_dbinspector

Android library for viewing, editing and sharing in app databases.
Kotlin
951
star
2

rails-handbook

Describing the development process used by the Infinum Rails Team.
Slim
763
star
3

FBAnnotationClustering

iOS library for clustering map notifications in an easy and performant way
Objective-C
713
star
4

Android-Goldfinger

Android library to simplify Biometric authentication implementation.
Java
653
star
5

ios-viper-xcode-templates

Used for generating template files for the VIPER architecture, which solves the common Massive View Controller issues in iOS apps.
HTML
576
star
6

phrasing

Edit phrases inline for your Ruby on Rails applications!
Ruby
545
star
7

eightshift-boilerplate

This repository contains all the tools you need to start building a modern WordPress theme, using all the latest front end development tools.
PHP
511
star
8

Android-GoldenEye

A wrapper for Camera1 and Camera2 API which exposes simple to use interface.
Kotlin
376
star
9

cookies_eu

Gem to add cookie consent to Rails application
Haml
269
star
10

MjolnirRecyclerView

[DEPRECATED] This library is no longer maintained and it will not receive any more updates.
Java
218
star
11

ios-nuts-and-bolts

iOS bits and pieces that you can include in your project to make your life a bit easier.
Swift
195
star
12

Japx

Lightweight parser for the complex JSON:API (http://jsonapi.org/) structure.
Swift
152
star
13

flutter-charts

Customizable charts library for flutter.
Dart
142
star
14

datx

DatX is an opinionated JS/TS data store. It features support for simple property definition, references to other models and first-class TypeScript support.
TypeScript
138
star
15

learnQuery

Learn JavaScript fundamentals by building your own jQuery equivalent library
JavaScript
137
star
16

flutter-dasher

Dart
118
star
17

floggy

Customizable logger for dart and flutter applications.
Dart
117
star
18

android-complexify

An Android library which makes checking the quality of user's password a breeze.
Java
113
star
19

Android-Prince-of-Versions

Android library for handling application updates.
Java
104
star
20

android_connectionbuddy

Utility library for handling connectivity change events.
Java
94
star
21

eightshift-docs

A documentation website for Eightshift open source projects
MDX
84
star
22

Retromock

Java library for mocking responses in a Retrofit service.
Kotlin
68
star
23

eightshift-frontend-libs

Frontend library that exposes custom scripts and styles for modern WordPress projects
JavaScript
68
star
24

eightshift-libs

Library that is meant to be used inside Eightshift Boilerplate and Eightshift Boilerplate Plugin libs via composer in order to be able to easily set up a modern development process.
PHP
61
star
25

frontend-handbook

Our handbook based on 10 years of experience in Frontend/JS development
Slim
57
star
26

Locker

Securely lock your secrets under the watch of TouchID or FaceID keeper 🔒
Swift
50
star
27

emotion-normalize

normalize.css but for emotion.js
JavaScript
50
star
28

mobx-jsonapi-store

JSON API Store for MobX
TypeScript
48
star
29

Dagger-2-Example

Dagger 2 example project
Java
44
star
30

ios-prince-of-versions

Library used for easier versioning of your applications, allowing you to prompt your users to update the app to the newest version
Swift
43
star
31

android-sentinel

Sentinel is a simple one screen UI which provides a standardised entry point for tools used in development and QA alongside device, application and permissions data.
Kotlin
38
star
32

enumerations

Better Rails Enumerations
Ruby
37
star
33

kotlin-jsonapix

JsonApiX is an Android, annotation processor library that was made to transform regular Kotlin classes into their JSON API representations, with the ability to serialize or deserialize them to or from strings.
Kotlin
37
star
34

eightshift-boilerplate-plugin

This repository contains all the tools you need to start building a modern WordPress plugin.
PHP
36
star
35

mobx-collection-store

Data collection store for MobX
TypeScript
35
star
36

decoupled-json-content

JavaScript
30
star
37

wordpress-handbook

Official WordPress handbook at Infinum
Slim
30
star
38

webpack-asset-pipeline

🚀 A missing link for the asset pipeline alternative with Webpack.
JavaScript
30
star
39

ios-loggie

Simplify debugging by showing network requests of your app as they happen.
Swift
30
star
40

eightshift-forms

WordPress plugin project for Gutenberg forms
PHP
29
star
41

flutter-plugins-locker

Flutter plugin that secures your secrets in keychain using biometric authentication (Fingerprint, Touch ID, Face ID...).
Dart
29
star
42

default_rails_template

Default template for generating new Rails applications.
Ruby
27
star
43

media-blender

Easy and predictable SASS/SCSS media queries
CSS
26
star
44

android-collar

Gradle plugin which collects all analytics screen names, events and user properties for Android projects.
Kotlin
26
star
45

secrets_cli

CLI for storing and reading your secrets via vault
Ruby
25
star
46

flutter-plugins-japx

JSON API parser for Flutter
Dart
25
star
47

flutter-bits

Flutter
Dart
22
star
48

react-mobx-translatable

Make React components translatable using MobX
JavaScript
21
star
49

dungeons-and-dragons

🎲 Dungeons & Dragons Character builder and keeper (work in progress)
TypeScript
19
star
50

android-crash-handler

Utility library which handles crash handler configuration
Java
19
star
51

qa-handbook

Describing the processes used by the Infinum QA Team
Slim
18
star
52

iOS-Bugsnatch

Swift
18
star
53

eightshift-coding-standards

Eightshift coding standards for WordPress
PHP
16
star
54

jsonapi-query_builder

Ruby
14
star
55

thrifty-retrofit-converter

Retrofit converter which uses Thrifty for Apache Thrift-compatible serialization
Java
13
star
56

json-wp-post-parser

JSON Post Parser plugin parses your content and saves it as JSON available in REST posts and pages endpoints.
PHP
13
star
57

swift-style-guide

12
star
58

react-responsive-ssr

TypeScript
11
star
59

redbreast

iOS strong image typing library
Ruby
11
star
60

generator-infinitely-static

💫 Static page generator with routes support thats infinitely awesome
JavaScript
10
star
61

ngx-hal

Angular datastore library with HAL support
TypeScript
10
star
62

fiscalizer

A gem for fiscalizing invoices in Croatia using Ruby.
Ruby
9
star
63

ember-form-object

Form object pattern in ember apps
JavaScript
9
star
64

JS-React-Example

Infinum's way of doing React
TypeScript
9
star
65

js-talks

✨ Interesting talks and mini lectures about new and cool stuff that's going on in the world of JS development, organized by the Infinum JS team
9
star
66

android-handbook

Slim
8
star
67

js-linters

Infinum's JS team linter rules
TypeScript
7
star
68

auth-worker

OAuth2 Service Worker handler
TypeScript
7
star
69

array_validator

Array Validations for ActiveModel
Ruby
7
star
70

eightshift-storybook

Storybook package for Eightshift-Boilerplate
7
star
71

icomcom-react

💬 A React component for handling communication with content in <iframe />
JavaScript
7
star
72

dox-demo

Demo app for dox gem:
HTML
6
star
73

android-localian

Android library that manages your application locale and language across multiple Android API levels.
Kotlin
6
star
74

ios-handbook

Slim
6
star
75

flutter-prince-of-versions

Library used for easier versioning of your applications, allowing you to prompt your users to update the app to the newest version
Dart
6
star
76

learn-react

👋 ⚛️ Learn React by implementing your own!
TypeScript
5
star
77

I18n-js

Javascript library for string internationalization.
CoffeeScript
5
star
78

ios-collar

In-app analytics debugging tool
Swift
5
star
79

ngx-form-object

Reactive forms manager
TypeScript
5
star
80

ios-sentinel

Developer’s toolbox for debugging applications
Swift
4
star
81

mobx-keys-store

Keys store for MobX
TypeScript
4
star
82

mysterious-sampler

Swift
4
star
83

rails-infinum-jsonapi_example_app_old

Ruby
4
star
84

phrasing_plus

Phrasing extension for editing images inline
Ruby
4
star
85

eightshift-web-components

Web components library that exposes custom scripts and styles for modern WordPress projects
Svelte
4
star
86

Install-Flutter-Version-Manager-Bitrise

Shell
4
star
87

infinum_setup

Setup script
Ruby
4
star
88

loglevel-filesave

Loglevel plugin for saving logs to the file
JavaScript
4
star
89

eightshift-blocks

Project dedicated to use inside WP Boilerplate and WP Boilerplate Plugin projects via composer to be able to easily setup modern development process for Gutenberg blocks..
PHP
3
star
90

data_sync

Rails plugin for database and file synchronization
Ruby
3
star
91

docusaurus-theme

Infinum Docusaurus teme
JavaScript
3
star
92

next-passenger

next.js with passenger proof of concept
Dockerfile
3
star
93

SocketMan

Android WebSocket client app
Java
3
star
94

react-asset-collector

Collect assets from react components so you can do HTTP2 push
JavaScript
3
star
95

rails_log_book

Ruby
3
star
96

mina-secrets

Mina plugin for secrets_cli gem
Ruby
2
star
97

ngx-nuts-and-bolts

A collection of commonly used pieces of Angular-related code that we use everyday at Infinum.
TypeScript
2
star
98

money_with_date

Extension for the money gem which adds dates to Money objects.
Ruby
2
star
99

blog-android-permissions

Java
2
star
100

JS-RxWorkshop

TypeScript
2
star