Skip to content

Commit b336cf7

Browse files
committed
Install template and update feature. [zipmark#275]
1 parent e8263e4 commit b336cf7

File tree

2 files changed

+370
-0
lines changed

2 files changed

+370
-0
lines changed

features/slate_documentation.feature

+284
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
Feature: Generate Slate documentation from test examples
2+
3+
Background:
4+
Given a file named "app.rb" with:
5+
"""
6+
require 'sinatra'
7+
8+
class App < Sinatra::Base
9+
get '/orders' do
10+
content_type :json
11+
12+
[200, {
13+
:page => 1,
14+
:orders => [
15+
{ name: 'Order 1', amount: 9.99, description: nil },
16+
{ name: 'Order 2', amount: 100.0, description: 'A great order' }
17+
]
18+
}.to_json]
19+
end
20+
21+
get '/orders/:id' do
22+
content_type :json
23+
24+
[200, { order: { name: 'Order 1', amount: 100.0, description: 'A great order' } }.to_json]
25+
end
26+
27+
post '/orders' do
28+
201
29+
end
30+
31+
put '/orders/:id' do
32+
200
33+
end
34+
35+
delete '/orders/:id' do
36+
200
37+
end
38+
39+
get '/help' do
40+
[200, 'Welcome Henry !']
41+
end
42+
end
43+
"""
44+
And a file named "app_spec.rb" with:
45+
"""
46+
require "rspec_api_documentation"
47+
require "rspec_api_documentation/dsl"
48+
49+
RspecApiDocumentation.configure do |config|
50+
config.app = App
51+
config.api_name = "Example API"
52+
config.format = :slate
53+
config.request_headers_to_include = %w[Content-Type Host]
54+
config.response_headers_to_include = %w[Content-Type Content-Length]
55+
end
56+
57+
resource 'Orders' do
58+
get '/orders' do
59+
response_field :page, "Current page"
60+
61+
example_request 'Getting a list of orders' do
62+
status.should eq(200)
63+
response_body.should eq('{"page":1,"orders":[{"name":"Order 1","amount":9.99,"description":null},{"name":"Order 2","amount":100.0,"description":"A great order"}]}')
64+
end
65+
end
66+
67+
get '/orders/:id' do
68+
let(:id) { 1 }
69+
70+
example_request 'Getting a specific order' do
71+
status.should eq(200)
72+
response_body.should == '{"order":{"name":"Order 1","amount":100.0,"description":"A great order"}}'
73+
end
74+
end
75+
76+
post '/orders' do
77+
parameter :name, 'Name of order', :required => true
78+
parameter :amount, 'Amount paid', :required => true
79+
parameter :description, 'Some comments on the order'
80+
81+
let(:name) { "Order 3" }
82+
let(:amount) { 33.0 }
83+
84+
example_request 'Creating an order' do
85+
status.should == 201
86+
end
87+
end
88+
89+
put '/orders/:id' do
90+
parameter :name, 'Name of order', :required => true
91+
parameter :amount, 'Amount paid', :required => true
92+
parameter :description, 'Some comments on the order'
93+
94+
let(:id) { 2 }
95+
let(:name) { "Updated name" }
96+
97+
example_request 'Updating an order' do
98+
status.should == 200
99+
end
100+
end
101+
102+
delete "/orders/:id" do
103+
let(:id) { 1 }
104+
105+
example_request "Deleting an order" do
106+
status.should == 200
107+
end
108+
end
109+
end
110+
111+
resource 'Help' do
112+
get '/help' do
113+
example_request 'Getting welcome message' do
114+
status.should eq(200)
115+
response_body.should == 'Welcome Henry !'
116+
end
117+
end
118+
119+
end
120+
"""
121+
When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter`
122+
123+
Scenario: Output helpful progress to the console
124+
Then the output should contain:
125+
"""
126+
Generating API Docs
127+
Orders
128+
GET /orders
129+
* Getting a list of orders
130+
GET /orders/:id
131+
* Getting a specific order
132+
POST /orders
133+
* Creating an order
134+
PUT /orders/:id
135+
* Updating an order
136+
DELETE /orders/:id
137+
* Deleting an order
138+
Help
139+
GET /help
140+
* Getting welcome message
141+
"""
142+
And the output should contain "6 examples, 0 failures"
143+
And the exit status should be 0
144+
145+
Scenario: Index file should look like we expect
146+
Then the file "doc/api/index.markdown" should contain exactly:
147+
"""
148+
# Example API
149+
150+
## Help
151+
152+
* [Getting welcome message](help/getting_welcome_message.markdown)
153+
154+
## Orders
155+
156+
* [Creating an order](orders/creating_an_order.markdown)
157+
* [Deleting an order](orders/deleting_an_order.markdown)
158+
* [Getting a list of orders](orders/getting_a_list_of_orders.markdown)
159+
* [Getting a specific order](orders/getting_a_specific_order.markdown)
160+
* [Updating an order](orders/updating_an_order.markdown)
161+
"""
162+
163+
Scenario: Example 'Getting a list of orders' file should look like we expect
164+
Then the file "doc/api/orders/getting_a_list_of_orders.markdown" should contain exactly:
165+
"""
166+
## Getting a list of orders
167+
168+
### Request
169+
170+
#### Endpoint
171+
172+
```
173+
GET /orders
174+
Host: example.org
175+
```
176+
177+
`GET /orders`
178+
179+
180+
#### Parameters
181+
182+
183+
None known.
184+
185+
186+
### Response
187+
188+
```
189+
Content-Type: application/json
190+
Content-Length: 137
191+
200 OK
192+
```
193+
194+
195+
```json
196+
{
197+
"page": 1,
198+
"orders": [
199+
{
200+
"name": "Order 1",
201+
"amount": 9.99,
202+
"description": null
203+
},
204+
{
205+
"name": "Order 2",
206+
"amount": 100.0,
207+
"description": "A great order"
208+
}
209+
]
210+
}
211+
```
212+
213+
214+
215+
#### Fields
216+
217+
| Name | Description |
218+
|:-----------|:--------------------|
219+
| page | Current page |
220+
221+
222+
"""
223+
224+
Scenario: Example 'Creating an order' file should look like we expect
225+
Then the file "doc/api/orders/creating_an_order.markdown" should contain exactly:
226+
"""
227+
## Creating an order
228+
229+
### Request
230+
231+
#### Endpoint
232+
233+
```
234+
POST /orders
235+
Host: example.org
236+
Content-Type: application/x-www-form-urlencoded
237+
```
238+
239+
`POST /orders`
240+
241+
242+
#### Parameters
243+
244+
245+
```json
246+
name=Order+3&amount=33.0
247+
```
248+
249+
250+
| Name | Description |
251+
|:-----|:------------|
252+
| name *required* | Name of order |
253+
| amount *required* | Amount paid |
254+
| description | Some comments on the order |
255+
256+
257+
258+
### Response
259+
260+
```
261+
Content-Type: text/html;charset=utf-8
262+
Content-Length: 0
263+
201 Created
264+
```
265+
266+
267+
268+
269+
"""
270+
271+
Scenario: Example 'Deleting an order' file should be created
272+
Then a file named "doc/api/orders/deleting_an_order.markdown" should exist
273+
274+
Scenario: Example 'Getting a list of orders' file should be created
275+
Then a file named "doc/api/orders/getting_a_list_of_orders.markdown" should exist
276+
277+
Scenario: Example 'Getting a specific order' file should be created
278+
Then a file named "doc/api/orders/getting_a_specific_order.markdown" should exist
279+
280+
Scenario: Example 'Updating an order' file should be created
281+
Then a file named "doc/api/orders/updating_an_order.markdown" should exist
282+
283+
Scenario: Example 'Getting welcome message' file should be created
284+
Then a file named "doc/api/help/getting_welcome_message.markdown" should exist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## {{ description }}
2+
3+
### Request
4+
5+
#### Endpoint
6+
7+
{{# requests}}
8+
```
9+
{{ request_method }} {{ request_path }}
10+
{{ request_headers_text }}
11+
```
12+
{{/ requests}}
13+
14+
`{{ http_method }} {{ route }}`
15+
16+
{{# explanation }}
17+
18+
{{{ explanation_with_linebreaks }}}
19+
{{/ explanation }}
20+
21+
#### Parameters
22+
23+
{{# requests}}
24+
{{# request_query_parameters_text }}
25+
26+
```json
27+
{{ request_query_parameters_text }}
28+
```
29+
{{/ request_query_parameters_text }}
30+
{{# request_body }}
31+
32+
```json
33+
{{{ request_body }}}
34+
```
35+
{{/ request_body }}
36+
37+
{{# has_parameters? }}
38+
39+
| Name | Description |
40+
|:-----|:------------|
41+
{{# parameters }}
42+
| {{#scope}}{{scope}}[{{/scope}}{{ name }}{{#scope}}]{{/scope}} {{# required }}*required*{{/ required }} | {{{ description }}} |
43+
{{/ parameters }}
44+
45+
{{/ has_parameters? }}
46+
{{^ has_parameters? }}
47+
None known.
48+
{{/ has_parameters? }}
49+
50+
{{# response_status}}
51+
52+
### Response
53+
54+
```
55+
{{ response_headers_text }}
56+
{{ response_status }} {{ response_status_text}}
57+
```
58+
59+
{{# response_body}}
60+
61+
```json
62+
{{{ response_body }}}
63+
```
64+
{{/response_body}}
65+
66+
{{/ response_status}}
67+
68+
{{# has_response_fields? }}
69+
70+
#### Fields
71+
72+
| Name | Description |
73+
|:-----------|:--------------------|
74+
{{# response_fields }}
75+
| {{#scope}}{{scope}}[{{/scope}}{{ name }}{{#scope}}]{{/scope}} | {{{ description }}} |
76+
{{/ response_fields }}
77+
78+
{{/ has_response_fields? }}
79+
80+
{{# curl }}
81+
82+
### cURL
83+
84+
<code>{{{ curl_with_linebreaks }}}</code>
85+
{{/ curl }}
86+
{{/ requests}}

0 commit comments

Comments
 (0)