grape-3scale provides an autogenerated documentation for your grape-API. The generated documentation is 3scale-compliant, meaning it can be used as the spec for 3scale Active Docs. This gem is a fork of grape-swagger (https://github.com/tim-vandecasteele/grape-swagger.git)
grape-3scale is available as a gem, install it simply via the commandline:
gem install grape-3scale
or add to your Gemfile:
gem 'grape-3scale'
Once you have the gem installed, mount all your different APIs (with Grape::API
superclass) on a root node. In the root class definition, also include add_3scale_documentation
, this sets up the system and registers the documentation on '/3scale_doc.json'. Setup done, you can restart your local server now.
require 'grape-3scale'
module API
class Root < Grape::API
mount API::Cats
mount API::Dogs
mount API::Pirates
add_3scale_documentation
end
end
To use with Active Docs get the generated spec from http://localhost:3000/3scale_doc.json and upload to your 3scale admin portal either using the form on the api service edit page or via the API:
curl -v -X PUT -F "body=@YOUR_API_JSON_SPEC.json" "https://YOUR_ADMIN_DOMAIN/admin/api/active_docs/ID_OF_THE_SERVICE_SPEC.xml?provider_key=YOUR_PROVIDER_KEY"
You can pass a hash with some configuration possibilities to add_3scale_documentation
, all of these are optional:
:mount_path
The path were the API documentation is loaded, default '/3scale_doc':api_version
Version of the API that's being exposed:base_path
Basepath of the API that's being exposed:markdown
Allow markdown innotes
, defaultfalse
:hide_documentation_path
Don't show the '/3scale_doc' path in the generated 3scale documentation:default_params
A hash of values for paramaters so save to repeatedly documenting the same parameters
3scale also supports the documentation of parameters passed in the header. Since grape's params[]
doesn't return header parameters we can
to specify header parameters seperately in a block after the description.
desc "Return super-secret information", {
headers: {
"XAuthToken" => {
description: "Valdates your identity",
required: true
},
"XOptionalHeader" => {
description: "Not reallly needed",
required: false
}
}
}
We found that to supply good descriptions to function paramaters for 3scale, we were adding the same comments in again and again. To apply a more DRY approach, we allowed the generator to have passed in a hash of default values for parameters. i.e
default_params = {
"App-Id" => {
description: "Identifies application", required: true, threescale_name: 'app_ids', type: "String"
},
"App-Key" => {
description: "Authorizes application", required: true, threescale_name: 'app_keys', type: "String"
},
.....
}
add_3scale_documentation :markdown => true, :hide_documentation_path => true,
:hide_format => true, :default_params=>default_params
This means that later you can simply specifiy a param name, and the description and other params are added from the hash. i.e.
params do
requires "App-Id"
requires "App-Key"
end
You can override any of the parameters locally if needed, and the defaults only apply if not set
grape-3scale allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Active Docs.
We're using kramdown for parsing the markdown, specific syntax can be found here.
Be sure to enable markdown in the add_3scale_documentation
call: ':markdown => true'
desc "Reserve a virgin in heaven", {
:notes => <<-NOTE
Virgins in heaven
-----------------
> A virgin doesn't come for free
If you want to reserve a virgin in heaven, you have to do
some crazy stuff on earth.
def do_good
puts 'help people'
end
* _Will go to Heaven:_ Probably
* _Will go to Hell:_ Probably not
NOTE
}
You can also make use of the extra 3scale params:
:threescale_name
The options 3scame param name - useful for app-id and app-key:default
This passes in to 3scale as their param::defaultValue
:allowedValues
A list of allowed values for a parameter
Finally the gem also automatically uses the 3scale group
variable to group opperations by their route name
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
- Fork the project.
- Start a feature/bugfix branch.
- Commit and push until you are happy with your contribution.
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright (c) 2012 Tim Vandecasteele. See LICENSE.txt for further details.