Skip to content

Commit 6a821f0

Browse files
committed
Merge branch 'master' of github.com:zipmark/rspec_api_documentation
2 parents e8f91a0 + d68eb45 commit 6a821f0

File tree

8 files changed

+208
-9
lines changed

8 files changed

+208
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tmp

Gemfile.lock

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,39 @@ GEM
1616
activesupport (3.1.3)
1717
multi_json (~> 1.0)
1818
addressable (2.2.6)
19+
aruba (0.4.11)
20+
childprocess (>= 0.2.3)
21+
cucumber (>= 1.1.1)
22+
ffi (>= 1.0.11)
23+
rspec (>= 2.7.0)
1924
builder (3.0.0)
25+
capybara (1.1.2)
26+
mime-types (>= 1.16)
27+
nokogiri (>= 1.3.3)
28+
rack (>= 1.0.0)
29+
rack-test (>= 0.5.4)
30+
selenium-webdriver (~> 2.0)
31+
xpath (~> 0.1.4)
32+
childprocess (0.3.1)
33+
ffi (~> 1.0.6)
2034
crack (0.3.1)
35+
cucumber (1.1.8)
36+
builder (>= 2.1.2)
37+
diff-lcs (>= 1.1.2)
38+
gherkin (~> 2.8.0)
39+
json (>= 1.4.6)
40+
term-ansicolor (>= 1.0.6)
2141
diff-lcs (1.1.3)
2242
fakefs (0.4.0)
43+
ffi (1.0.11)
44+
gherkin (2.8.0)
45+
json (>= 1.4.6)
2346
i18n (0.6.0)
24-
json (1.6.3)
47+
json (1.6.5)
48+
mime-types (1.17.2)
2549
multi_json (1.0.4)
2650
mustache (0.99.4)
51+
nokogiri (1.5.0)
2752
rack (1.3.5)
2853
rack-test (0.6.1)
2954
rack (>= 1.0)
@@ -35,19 +60,30 @@ GEM
3560
rspec-expectations (2.8.0)
3661
diff-lcs (~> 1.1.2)
3762
rspec-mocks (2.8.0)
63+
rubyzip (0.9.6.1)
64+
selenium-webdriver (2.19.0)
65+
childprocess (>= 0.2.5)
66+
ffi (~> 1.0.9)
67+
multi_json (~> 1.0.4)
68+
rubyzip
3869
sinatra (1.2.6)
3970
rack (~> 1.1)
4071
tilt (>= 1.2.2, < 2.0)
72+
term-ansicolor (1.0.7)
4173
tilt (1.3.3)
4274
webmock (1.7.10)
4375
addressable (~> 2.2, > 2.2.5)
4476
crack (>= 0.1.7)
77+
xpath (0.1.4)
78+
nokogiri (~> 1.3)
4579

4680
PLATFORMS
4781
ruby
4882

4983
DEPENDENCIES
84+
aruba
5085
builder
86+
capybara
5187
fakefs
5288
rspec_api_documentation!
5389
sinatra

features/html_documentation.feature

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Feature: Generate HTML documentation from test examples
2+
3+
Background:
4+
Given a file named "app.rb" with:
5+
"""
6+
require "sinatra/base"
7+
8+
class App < Sinatra::Base
9+
before do
10+
content_type :json
11+
end
12+
13+
get "/greetings" do
14+
if target = params["target"]
15+
{ "hello" => params["target"] }.to_json
16+
else
17+
422
18+
end
19+
end
20+
end
21+
"""
22+
And a file named "app_spec.rb" with:
23+
"""
24+
require "active_support/inflector"
25+
require "rspec_api_documentation"
26+
require "rspec_api_documentation/dsl"
27+
28+
RspecApiDocumentation.configure do |config|
29+
config.app = App
30+
end
31+
32+
resource "Greetings" do
33+
get "/greetings" do
34+
parameter :target, "The thing you want to greet"
35+
36+
example "Greeting your favorite gem" do
37+
do_request :target => "rspec_api_documentation"
38+
39+
status.should eq(200)
40+
response_body.should eq('{"hello":"rspec_api_documentation"}')
41+
end
42+
end
43+
end
44+
"""
45+
When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter`
46+
47+
Scenario: Output helpful progress to the console
48+
Then the output should contain:
49+
"""
50+
Generating API Docs
51+
Greetings
52+
GET /greetings
53+
* Greeting your favorite gem
54+
"""
55+
And the output should contain "1 example, 0 failures"
56+
And the exit status should be 0
57+
58+
Scenario: Create an index of all API examples, including all resources
59+
When I open the index
60+
Then I should see the following resources:
61+
| Greetings |
62+
63+
Scenario: Example HTML documentation includes the parameters
64+
When I open the index
65+
And I navigate to "Greeting your favorite gem"
66+
Then I should see the following parameters:
67+
| name | description |
68+
| target | The thing you want to greet |
69+
70+
Scenario: Example HTML documentation includes the request information
71+
When I open the index
72+
And I navigate to "Greeting your favorite gem"
73+
Then I should see the route is "GET /greetings?target=rspec_api_documentation"
74+
And I should see the following request headers:
75+
"""
76+
Host: example.org
77+
Cookie:
78+
"""
79+
And I should see the following query parameters:
80+
"""
81+
target: rspec_api_documentation
82+
"""
83+
84+
Scenario: Example HTML documentation includes the response information
85+
When I open the index
86+
And I navigate to "Greeting your favorite gem"
87+
Then I should see the response status is "200 OK"
88+
And I should see the following response headers:
89+
"""
90+
Content-Type: application/json
91+
Content-Length: 35
92+
"""
93+
And I should see the following response_body:
94+
"""
95+
{
96+
"hello" => "rspec_api_documentation"
97+
}
98+
"""
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
When /^I open the index$/ do
2+
visit "/index.html"
3+
end
4+
5+
When /^I navigate to "([^"]*)"$/ do |example|
6+
click_link example
7+
end
8+
9+
Then /^I should see the following resources:$/ do |table|
10+
all("h2").map(&:text).should == table.raw.flatten
11+
end
12+
13+
Then /^I should see the following parameters:$/ do |table|
14+
names = all(".parameters .name").map(&:text)
15+
descriptions = all(".parameters .description").map(&:text)
16+
names.zip(descriptions).should == table.rows
17+
end
18+
19+
Then /^I should see the following (request|response) headers:$/ do |part, headers|
20+
page.should have_css("pre.#{part}.headers", :content => headers)
21+
end
22+
23+
Then /^I should see the route is "([^"]*)"$/ do |route|
24+
page.should have_css(".request.route", :content => route)
25+
end
26+
27+
Then /^I should see the following query parameters:$/ do |query_parameters|
28+
page.should have_css("pre.request.query_parameters"), :content => query_parameters
29+
end
30+
31+
Then /^I should see the response status is "([^"]*)"$/ do |status|
32+
page.should have_css(".response.status", :content => status)
33+
end
34+
35+
Then /^I should see the following response_body:$/ do |response_body|
36+
page.should have_css("pre.response.body", :content => response_body)
37+
end

features/support/capybara.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'rack'
2+
require 'capybara/cucumber'
3+
4+
# Wire up Capybara to test again static files served by Rack
5+
# Courtesy of http://opensoul.org/blog/archives/2010/05/11/capybaras-eating-cucumbers/
6+
7+
root_dir = File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'aruba', 'docs')
8+
9+
Capybara.app = Rack::Builder.new do
10+
map "/" do
11+
# use Rack::CommonLogger, $stderr
12+
use Rack::Static, :urls => ["/"], :root => root_dir
13+
use Rack::Lint
14+
run lambda {|env| [404, {}, '']}
15+
end
16+
end.to_app
17+
18+
Capybara.default_selector = :css
19+
Capybara.default_driver = :rack_test

features/support/env.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Aruba has an important step that is only on git right now, so we need to
2+
# explicitly load the development bundle because gemspecs can't point to git
3+
require "bundler"
4+
Bundler.setup(:development)
5+
6+
require "aruba/cucumber"

rspec_api_documentation.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Gem::Specification.new do |s|
2525
s.add_development_dependency "fakefs"
2626
s.add_development_dependency "sinatra"
2727
s.add_development_dependency "builder"
28+
s.add_development_dependency "aruba"
29+
s.add_development_dependency "capybara"
2830

2931
s.files = Dir.glob("lib/**/*")
3032
s.require_path = 'lib'

templates/rspec_api_documentation/html_example.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
{{# has_parameters? }}
3232
<h3>Parameters</h3>
33-
<table>
33+
<table class="parameters">
3434
<thead>
3535
<tr>
3636
<th>Name</th>
@@ -40,7 +40,7 @@
4040
<tbody>
4141
{{# parameters }}
4242
<tr>
43-
<td{{# required }} class="required"{{/ required }}>
43+
<th{{# required }} class="required"{{/ required }}>
4444
<span class="name">{{ name }}</span>
4545
</td>
4646
<td>
@@ -55,12 +55,12 @@
5555
{{# requests }}
5656
<h3>Request</h3>
5757
<h4>Headers</h4>
58-
<pre class="headers">{{ request_headers }}</pre>
58+
<pre class="request headers">{{ request_headers }}</pre>
5959
<h4>Route</h4>
60-
<pre class="request highlight">{{ method }} {{ route }}</pre>
60+
<pre class="request route highlight">{{ method }} {{ route }}</pre>
6161
{{# request_query_parameters }}
6262
<h4>Query Parameters</h4>
63-
<pre class="request highlight">{{ request_query_parameters }}</pre>
63+
<pre class="request query_parameters highlight">{{ request_query_parameters }}</pre>
6464
{{/ request_query_parameters }}
6565
{{# request_body }}
6666
<h4>Body</h4>
@@ -75,12 +75,12 @@
7575
{{# response_status }}
7676
<h3>Response</h3>
7777
<h4>Headers</h4>
78-
<pre class="headers">{{ response_headers }}</pre>
78+
<pre class="response headers">{{ response_headers }}</pre>
7979
<h4>Status</h4>
80-
<pre class="response_status">{{ response_status }} {{ response_status_text}}</pre>
80+
<pre class="response status">{{ response_status }} {{ response_status_text}}</pre>
8181
{{# response_body }}
8282
<h4>Body</h4>
83-
<pre class="response highlight">{{ response_body }}</pre>
83+
<pre class="response body highlight">{{ response_body }}</pre>
8484
{{/ response_body }}
8585
{{/ response_status }}
8686
{{/ requests }}

0 commit comments

Comments
 (0)