forked from zipmark/rspec_api_documentation
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
Feature: Combined text | ||
In order to serve the docs from my API | ||
As Zipmark | ||
I want to generate text files for each of my resources containing their combined docs | ||
|
||
Background: | ||
Given a file named "app.rb" with: | ||
""" | ||
class App | ||
def self.call(env) | ||
request = Rack::Request.new(env) | ||
response = Rack::Response.new | ||
response["Content-Type"] = "text/plain" | ||
response.write("Hello, #{request.params["target"]}!") | ||
response.finish | ||
end | ||
end | ||
""" | ||
And a file named "app_spec.rb" with: | ||
""" | ||
require "rspec_api_documentation" | ||
require "rspec_api_documentation/dsl" | ||
RspecApiDocumentation.configure do |config| | ||
config.app = App | ||
config.format = :combined_text | ||
end | ||
resource "Greetings" do | ||
get "/greetings" do | ||
parameter :target, "The thing you want to greet" | ||
example "Greeting your favorite gem" do | ||
do_request :target => "rspec_api_documentation" | ||
response_headers["Content-Type"].should eq("text/plain") | ||
status.should eq(200) | ||
response_body.should eq('Hello, rspec_api_documentation!') | ||
end | ||
example "Greeting your favorite developers of your favorite gem" do | ||
do_request :target => "Sam & Eric" | ||
response_headers["Content-Type"].should eq("text/plain") | ||
status.should eq(200) | ||
response_body.should eq('Hello, Sam & Eric!') | ||
end | ||
end | ||
end | ||
""" | ||
When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter` | ||
|
||
Scenario: Output helpful progress to the console | ||
Then the output should contain: | ||
""" | ||
Generating API Docs | ||
Greetings | ||
GET /greetings | ||
* Greeting your favorite gem | ||
* Greeting your favorite developers of your favorite gem | ||
""" | ||
And the output should contain "2 examples, 0 failures" | ||
And the exit status should be 0 | ||
|
||
Scenario: File should look like we expect | ||
Then the file "docs/greetings/index.txt" should contain exactly: | ||
""" | ||
Greeting your favorite gem | ||
-------------------------- | ||
Parameters: | ||
* target - The thing you want to greet | ||
Request: | ||
GET /greetings?target=rspec_api_documentation | ||
target=rspec_api_documentation | ||
Response: | ||
Status: 200 OK | ||
Content-Type: text/plain | ||
Content-Length: 31 | ||
Hello, rspec_api_documentation! | ||
Greeting your favorite developers of your favorite gem | ||
------------------------------------------------------ | ||
Parameters: | ||
* target - The thing you want to greet | ||
Request: | ||
GET /greetings?target=Sam+%26+Eric | ||
target=Sam & Eric | ||
Response: | ||
Status: 200 OK | ||
Content-Type: text/plain | ||
Content-Length: 18 | ||
Hello, Sam & Eric! | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module RspecApiDocumentation | ||
class CombinedTextWriter | ||
def self.write(index, configuration) | ||
index.examples.each do |example| | ||
resource_name = example.resource_name.downcase.gsub(/\s+/, '_') | ||
FileUtils.mkdir_p(configuration.docs_dir.join(resource_name)) | ||
File.open(configuration.docs_dir.join(resource_name, "index.txt"), "a+") do |f| | ||
f.puts example.description | ||
f.puts "-" * example.description.length | ||
f.puts | ||
|
||
f.puts "Parameters:" | ||
example.metadata[:parameters].each do |parameter| | ||
f.puts " * #{parameter[:name]} - #{parameter[:description]}" | ||
end | ||
f.puts | ||
|
||
example.metadata[:requests].each do |request| | ||
f.puts "Request:" | ||
f.puts " #{request[:request_method]} #{request[:request_path]}" | ||
f.puts | ||
f.puts format_hash(request[:request_body] || request[:request_query_parameters]) | ||
f.puts | ||
f.puts "Response:" | ||
f.puts " Status: #{request[:response_status]} #{request[:response_status_text]}" | ||
f.puts format_hash(request[:response_headers], ": ") | ||
f.puts | ||
f.puts request[:response_body].split("\n").map { |line| " #{line}" }.join("\n") | ||
end | ||
|
||
unless example == index.examples.last | ||
f.puts | ||
f.puts | ||
end | ||
end | ||
end | ||
end | ||
|
||
def self.format_hash(hash, separator="=") | ||
hash.inject("") do |out, (k, v)| | ||
out << " #{k}#{separator}#{v}\n" | ||
end | ||
end | ||
end | ||
end |