Permalink
Browse files

Handle nil and other corner cases in CombinedTextWriter

  • Loading branch information...
1 parent fc793af commit 88c728c0318744fd9c78717e1d2c49141a971745 @oestrich committed Jun 1, 2012
@@ -73,6 +73,8 @@ Feature: Combined text
Request:
GET /greetings?target=rspec_api_documentation
+ Cookie:
+ Host: example.org
target=rspec_api_documentation
@@ -92,6 +94,8 @@ Feature: Combined text
Request:
GET /greetings?target=Sam+%26+Eric
+ Cookie:
+ Host: example.org
target=Sam & Eric
@@ -1,34 +1,22 @@
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
+ index.examples.each do |rspec_example|
+ example = CombinedTextExample.new(rspec_example)
+ FileUtils.mkdir_p(configuration.docs_dir.join(example.resource_name))
+ File.open(configuration.docs_dir.join(example.resource_name, "index.txt"), "a+") do |f|
+ f.print example.description
+ f.print example.parameters
- example.metadata[:requests].each do |request|
+ example.requests.each do |request, response|
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 request
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")
+ f.puts response
end
- unless example == index.examples.last
+ unless rspec_example == index.examples.last
f.puts
f.puts
end
@@ -42,4 +30,71 @@ def self.format_hash(hash, separator="=")
end
end
end
+
+ class CombinedTextExample
+ attr_reader :example
+
+ def initialize(example)
+ @example = example
+ end
+
+ def resource_name
+ example.resource_name.downcase.gsub(/\s+/, '_')
+ end
+
+ def description
+ example.description + "\n" + "-" * example.description.length + "\n\n"
+ end
+
+ def parameters
+ return "" unless example.metadata[:parameters]
+ "Parameters:\n" + example.metadata[:parameters].inject("") do |out, parameter|
+ out << " * #{parameter[:name]} - #{parameter[:description]}\n"
+ end + "\n"
+ end
+
+ def requests
+ return [] unless example.metadata[:requests]
+ example.metadata[:requests].map do |request|
+ [format_request(request), format_response(request)]
+ end
+ end
+
+ private
+ def format_request(request)
+ [
+ [
+ " #{request[:request_method]} #{request[:request_path]}",
+ format_hash(request[:request_headers], ": ")
+ ],
+ [
+ format_string(request[:request_body]) || format_hash(request[:request_query_parameters])
+ ]
+ ].map { |x| x.compact.join("\n") }.reject(&:blank?).join("\n\n") + "\n"
+ end
+
+ def format_response(request)
+ [
+ [
+ " Status: #{request[:response_status]} #{request[:response_status_text]}",
+ format_hash(request[:response_headers], ": ")
+ ],
+ [
+ format_string(request[:response_body])
+ ]
+ ].map { |x| x.compact.join("\n") }.reject(&:blank?).join("\n\n") + "\n"
+ end
+
+ def format_string(string)
+ return unless string
+ string.split("\n").map { |line| " #{line}" }.join("\n")
+ end
+
+ def format_hash(hash, separator="=")
+ return unless hash
+ hash.sort_by { |k, v| k }.map do |k, v|
+ " #{k}#{separator}#{v}"
+ end.join("\n")
+ end
+ end
end
@@ -0,0 +1,77 @@
+require "spec_helper"
+require "rspec_api_documentation/combined_text_writer"
+
+describe RspecApiDocumentation::CombinedTextExample do
+ let(:metadata) { {} }
+ let(:rspec_example) { stub(:resource_name => "Foo Bar", :description => "ABCDEFG", :metadata => metadata) }
+ let(:example) { RspecApiDocumentation::CombinedTextExample.new(rspec_example) }
+
+ it "should format its resource name" do
+ example.resource_name.should == "foo_bar"
+ end
+
+ it "should format its description" do
+ example.description.should == "ABCDEFG\n-------\n\n"
+ end
+
+ context "given parameters" do
+ let(:metadata) {{
+ :parameters => [
+ { :name => "foo", :description => "Foo!" },
+ { :name => "bar", :description => "Bar!" }
+ ]
+ }}
+
+ it "should format its parameters" do
+ example.parameters.should == "Parameters:\n * foo - Foo!\n * bar - Bar!\n\n"
+ end
+ end
+
+ it "renders nothing if there are no parameters" do
+ example.parameters.should == ""
+ end
+
+ context "when there are requests" do
+ let(:requests) {[
+ {
+ :request_method => "GET",
+ :request_path => "/greetings",
+ :request_headers => { "Header" => "value" },
+ :request_query_parameters => { "foo" => "bar", "baz" => "quux" },
+ :response_status => 200,
+ :response_status_text => "OK",
+ :response_headers => { "Header" => "value", "Foo" => "bar" },
+ :response_body => "body"
+ },
+ {
+ :request_method => "POST",
+ :request_path => "/greetings",
+ :request_body => "body",
+ :response_status => 404,
+ :response_status_text => "Not Found",
+ :response_headers => { "Header" => "value" },
+ :response_body => "body"
+ },
+ {
+ :request_method => "DELETE",
+ :request_path => "/greetings/1",
+ :response_status => 200,
+ :response_status_text => "OK",
+ :response_headers => { "Header" => "value" },
+ },
+ ]}
+ let(:metadata) {{ :requests => requests }}
+
+ it "exposes the requests" do
+ example.requests.should == [
+ [" GET /greetings\n Header: value\n\n baz=quux\n foo=bar\n", " Status: 200 OK\n Foo: bar\n Header: value\n\n body\n"],
+ [" POST /greetings\n\n body\n", " Status: 404 Not Found\n Header: value\n\n body\n"],
+ [" DELETE /greetings/1\n", " Status: 200 OK\n Header: value\n"],
+ ]
+ end
+ end
+
+ it "returns the empty array if there are no requests" do
+ example.requests.should == []
+ end
+end

0 comments on commit 88c728c

Please sign in to comment.