Generate API documentation using RSpec
## Installation Add this line to your application's Gemfile: ```ruby gem 'rspec-api-docs' ``` And then execute: $ bundle Or install it yourself as: $ gem install rspec-api-docs ## Usage **rspec-api-docs** works in two stages. The first stage introduces a new DSL method, `doc`, to include in your RSpec specs. ``` ruby require 'rspec_api_docs/dsl' RSpec.describe 'Characters' do include RspecApiDocs::Dsl # ... end ``` The `doc` method stores data in a hash on the RSpec example metadata. The second stage is the formatter (`RspecApiDocs::Formatter`). The formatter parses the hash stored on each RSpec example and uses a [renderer](lib/rspec_api_docs/formatter/renderer/README.md) to write out your documentation. ``` $ rspec spec/requests/characters_spec.rb --formatter=RspecApiDocs::Formatter ``` ### DSL First, require the DSL and include the DSL module. You can do this in your `spec_helper.rb`: ``` ruby require 'rspec_api_docs/dsl' RSpec.configure do |config| config.include RspecApiDocs::Dsl, type: :request # ... end ``` Or in individual specs: ``` ruby require 'rspec_api_docs/dsl' RSpec.describe 'Characters' do include RspecApiDocs::Dsl # ... end ``` You also need to require a lambda that runs after each expectation: ``` ruby require 'rspec_api_docs/after' RSpec.configure do |config| config.after &RspecApiDocs::After::Hook end ``` This automatically stores the `last_request` and `last_response` objects for use by the formatter. **rspec-api-docs** doesn't touch any of the built-in RSpec DSL methods. Everything is contained in the `doc` block. You can use RSpec `before` blocks to share setup between multiple examples. ``` ruby require 'rspec_api_docs/dsl' RSpec.describe 'Characters' do include RspecApiDocs::Dsl before do doc do resource_name 'Characters' resource_description <<-EOF Characters inhabit the Land of Ooo. EOF end end describe 'GET /characters/:id' do it 'returns a character' do doc do name 'Fetching a Character' description 'For getting information about a Character.' path '/characters/:id' field :id, 'The id of a character', scope: :character, type: 'integer' field :name, "The character's name", scope: :character, type: 'string' end get '/characters/1' # normal expectations ... end # ... end end ``` #### `resource_name` Accepts a string of the name of the resource. > Characters #### `resource_description` Accepts a string that describes the resource. > Characters inhabit the Land of Ooo. #### `name` Accepts a string of the name of the resource. > Fetching a character Note: This defaults to the "description" of the RSpec example. ``` ruby it 'Fetching a character' do # ... end ``` #### `description` Accepts a string that describes the example. > To find out information about a Character. #### `path` Accepts a string for the path requested in the example. > /characters/:id Note: This defaults to the path of the first route requested in the example. #### `field` Accepts a `name`, `description`, and optionally a `scope` and `type`. - `name` [`Symbol`] the name of the response field - `description` [`String`] a description of the response field - `scope` [`Symbol`, `Array