Skip to content

Commit

Permalink
Add a combined text writer
Browse files Browse the repository at this point in the history
  • Loading branch information
oestrich committed Jun 1, 2012
1 parent c50982b commit ad19821
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
106 changes: 106 additions & 0 deletions features/combined_text.feature
@@ -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!
"""

1 change: 1 addition & 0 deletions lib/rspec_api_documentation.rb
Expand Up @@ -28,6 +28,7 @@ module RspecApiDocumentation
autoload :WurlWriter
autoload :JsonWriter
autoload :IndexWriter
autoload :CombinedTextWriter
autoload :Curl

def self.configuration
Expand Down
45 changes: 45 additions & 0 deletions lib/rspec_api_documentation/combined_text_writer.rb
@@ -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

0 comments on commit ad19821

Please sign in to comment.