README.markdown in grape-swagger-0.6.0 vs README.markdown in grape-swagger-0.7.0

- old
+ new

@@ -31,11 +31,11 @@ add_swagger_documentation end end ``` -To explore your API, either download [Swagger UI](https://github.com/wordnik/swagger-ui) and set it up yourself or go to the [online swagger demo](http://petstore.swagger.wordnik.com/) and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc.json). +To explore your API, either download [Swagger UI](https://github.com/wordnik/swagger-ui) and set it up yourself or go to the [online swagger demo](http://petstore.swagger.wordnik.com/) and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc.json). If you use the online demo, make sure your API supports foreign requests by enabling CORS in grape, otherwise you'll see the API description, but requests on the API won't return. You can do this by putting below code in your Grape API definition: ```` ruby before do header['Access-Control-Allow-Origin'] = '*' @@ -45,34 +45,76 @@ ## Configure You can pass a hash with some configuration possibilities to ```add_swagger_documentation```, all of these are optional: * ```:mount_path``` The path were the API documentation is loaded, default '/swagger_doc' * ```:api_version``` Version of the API that's being exposed -* ```:base_path``` Basepath of the API that's being exposed +* ```:base_path``` Basepath of the API that's being exposed, this configuration parameter accepts a Proc to evaluate base_path, useful when you need to use request attributes to determine the base_path. * ```:markdown``` Allow markdown in `notes`, default `false` * ```:hide_documentation_path``` Don't show the '/swagger_doc' path in the generated swagger documentation + ## Swagger Header Parameters -Swagger 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. +Swagger also supports the documentation of parameters passed in the header. Since grape's ```params[]``` doesn't return header parameters we can specify header parameters seperately in a block after the description. ``` ruby desc "Return super-secret information", { headers: { "XAuthToken" => { description: "Valdates your identity", - required: true + required: true }, "XOptionalHeader" => { description: "Not reallly needed", - required: false + required: false } } } ``` +## Hiding an endpoint + +You can hide an endpoint by adding ```:hidden => true``` in the description of the endpoint: + +``` ruby +desc 'Hide this endpoint', { + :hidden => true +} +``` + +## Grape Entities + +Add the [grape-entity](https://github.com/agileanimal/grape-entity) gem to our Gemfile. +Please refer to the [grape-entity documentation](https://github.com/gileanimal/grape-entity/blob/master/README.markdown) +for more details. + +The following example exposes statuses. And exposes statuses documentation adding :type and :desc. + +```ruby +module API + + module Entities + class Status < Grape::Entity + expose :text, :documentation => { :type => "string", :desc => "Status update text." } + end + end + + class Statuses < Grape::API + version 'v1' + + desc 'Statuses index', { + :entity => API::Entities::Status + } + get '/statuses' do + statuses = Status.all + type = current_user.admin? ? :full : :default + present statuses, with: API::Entities::Status, :type => type + end + end +end +``` + ## Swagger additions grape-swagger allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Swagger UI. The default Swagger UI doesn't allow HTML in the notes field, so you need to use an adapted version of Swagger UI (you can find one at https://github.com/tim-vandecasteele/swagger-ui/tree/vasco). We're using [kramdown](http://kramdown.rubyforge.org) for parsing the markdown, specific syntax can be found [here](http://kramdown.rubyforge.org/syntax.html). @@ -95,9 +137,19 @@ * _Will go to Heaven:_ Probably * _Will go to Hell:_ Probably not NOTE } +``` + +You can also document the HTTP status codes that your API returns with this syntax: + +``` ruby +get '/', :http_codes => [ + [400, "Invalid parameter entry"], +] do + ... +end ``` ## Contributing to grape-swagger * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.