Skip to content

Commit 95a95cc

Browse files
authored
Merge pull request #31 from drewish/document-dsl
Add documentation for the DSL
2 parents 2d400b9 + 7e84d5e commit 95a95cc

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

README.md

+183
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,186 @@ bundle exec rake swagger
6363
Now you can use Swagger UI or the renderer of your choice to display the
6464
formatted documentation. [swagger_engine](https://github.com/batdevis/swagger_engine)
6565
works pretty well and supports multiple documents.
66+
67+
## RSpec DSL
68+
69+
The DSL follows the hierachy of the Swagger Schema:
70+
71+
- [Paths Object](http://swagger.io/specification/#paths-object-29)
72+
- [Path Item Object](http://swagger.io/specification/#path-item-object-32)
73+
- [Parameter Object](http://swagger.io/specification/#parameter-object-44)s (Optional)
74+
- [Operation Object](http://swagger.io/specification/#operation-object-36)
75+
- [Parameter Object](http://swagger.io/specification/#parameter-object-44)s (Optional)
76+
- [Responses Object](http://swagger.io/specification/#responses-object-54)
77+
- [Response Object](http://swagger.io/specification/#response-object-58)
78+
- [Example Object](http://swagger.io/specification/#example-object-65)s (Optional)
79+
80+
Here's an example of a spec with comments to for the corresponding objects:
81+
82+
```rb
83+
require 'swagger_helper'
84+
85+
# Paths Object
86+
RSpec.describe "Posts Controller", type: :request do
87+
before { Post.new.save }
88+
89+
# Path Item Object
90+
path '/posts' do
91+
# Operation Object
92+
operation "GET", summary: "fetch list" do
93+
# Response Object
94+
response 200, description: "successful"
95+
end
96+
end
97+
98+
# Path Object
99+
path '/posts/{post_id}' do
100+
# Parameter Object
101+
parameter "post_id", {in: :path, type: :integer}
102+
let(:post_id) { 1 }
103+
104+
# Operation Object
105+
get summary: "fetch item" do
106+
# Response Object
107+
response 200, description: "success"
108+
end
109+
end
110+
```
111+
112+
113+
### Paths Object
114+
These methods are available inside of an RSpec contexts with the `type: :request` tag.
115+
116+
#### `path(template, attributes = {}, &block)`
117+
Defines a new Path Item.
118+
119+
### Path Item Object
120+
These methods are available inside of blocks passed to the `path` method.
121+
122+
#### `operation(method, attributes = {}, &block)`
123+
Defines a new Operation Object. The `method` is case insensitive.
124+
125+
#### `delete(attributes = {}, &block)`
126+
Alias for `operation(:delete, attributes, block)`.
127+
128+
#### `get(attributes = {}, &block)`
129+
Alias for `operation(:get, attributes, block)`.
130+
131+
#### `head(attributes = {}, &block)`
132+
Alias for `operation(:head, attributes, block)`.
133+
134+
#### `options(attributes = {}, &block)`
135+
Alias for `operation(:options, attributes, block)`.
136+
137+
#### `patch(attributes = {}, &block)`
138+
Alias for `operation(:patch, attributes, block)`.
139+
140+
#### `post(attributes = {}, &block)`
141+
Alias for `operation(:post, attributes, block)`.
142+
143+
#### `put(attributes = {}, &block)`
144+
Alias for `operation(:put, attributes, block)`.
145+
146+
147+
### Parameters
148+
These methods are available inside of blocks passed to the `path` or `operation` method.
149+
150+
#### `parameter(name, attributes = {})`
151+
Defines a new Parameter Object. You can define the parameter inline:
152+
```rb
153+
parameter :callback_url, in: :query, type: :string, required: true
154+
```
155+
156+
Or, via reference:
157+
```rb
158+
parameter ref: "#/parameters/site_id"
159+
```
160+
161+
Values for the parameters are set using `let`:
162+
```rb
163+
post summary: "create" do
164+
parameter "body", in: :body, schema: { foo: :bar}
165+
let(:body) { { post: { title: 'asdf', body: "blah" } } }
166+
# ...
167+
end
168+
```
169+
170+
171+
### Operation Object
172+
These methods are available inside of blocks passed to the `operation` method.
173+
174+
#### `consumes(*mime_types)`
175+
Use this to add MIME types that are specific to the operation. They will be merged
176+
with the Swagger Object's consumes field.
177+
```rb
178+
consumes 'application/json', 'application/xml'
179+
```
180+
181+
#### `produces(*mime_types)`
182+
Use this to add MIME types that are specific to the operation. They will be merged
183+
with the Swagger Object's consumes field.
184+
```rb
185+
produces 'application/json', 'application/xml'
186+
```
187+
188+
#### `response(status_code, attributes = {}, &block)`
189+
Defines a new Response Object. `status_code` must be between 1 and 599. `attributes`
190+
must include a `description`.
191+
192+
#### `tags(*tags)`
193+
Adds operation specific tags.
194+
```rb
195+
tags :accounts, :pets
196+
```
197+
198+
You can also provide tags through the RSpec context block and/or `path` method:
199+
```rb
200+
RSpec.describe "Sample Requests", type: :request, tags: [:context_tag] do
201+
path '/posts', tags: ['path_tag'] do
202+
operation "GET", summary: "fetch list" do
203+
produces 'application/json'
204+
tags 'operation_tag'
205+
206+
response(200, {description: "successful"})
207+
end
208+
end
209+
end
210+
```
211+
These tags will be merged with those of the operation. The `GET /posts` operation
212+
in this example will be tagged with `["context_tag", "path_tag", "operation_tag"]`.
213+
214+
215+
### Response Object
216+
These methods are available inside of blocks passed to the `response` method.
217+
218+
#### `capture_example()`
219+
This method will capture the response body from the test and create an Example
220+
Object for the Response.
221+
222+
You could also set this in an RSpec context block if you'd like examples for
223+
multiple operations or paths:
224+
```rb
225+
describe 'Connections', type: :request, capture_examples: true do
226+
# Any requests in this block will capture example responses
227+
end
228+
```
229+
230+
#### `schema(definition)`
231+
Sets the schema field for the Response Object. You can define it inline:
232+
```rb
233+
schema(
234+
type: :array,
235+
items: {
236+
type: :object,
237+
properties: {
238+
id: { type: :string },
239+
name: { type: :string },
240+
},
241+
}
242+
)
243+
```
244+
245+
Or, by reference:
246+
```rb
247+
schema ref: '#/definitions/Account'
248+
```

0 commit comments

Comments
 (0)