Skip to content

Commit

Permalink
Add wURL console to raddocs
Browse files Browse the repository at this point in the history
  • Loading branch information
TildeWill committed May 18, 2012
1 parent 2597cca commit cd433b2
Show file tree
Hide file tree
Showing 44 changed files with 20,966 additions and 218 deletions.
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
rspec_api_documentation (0.5.2)
activesupport (>= 3.0.0)
coderay (>= 1.0.7.rc1)
i18n (>= 0.1.0)
json (>= 1.4.6)
mustache (>= 0.99.4)
Expand Down Expand Up @@ -33,7 +32,6 @@ GEM
xpath (~> 0.1.4)
childprocess (0.3.2)
ffi (~> 1.0.6)
coderay (1.0.7.rc1)
crack (0.3.1)
cucumber (1.2.0)
builder (>= 2.1.2)
Expand Down
2 changes: 0 additions & 2 deletions example/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
rspec_api_documentation (0.5.2)
activesupport (>= 3.0.0)
coderay (>= 1.0.7.rc1)
i18n (>= 0.1.0)
json (>= 1.4.6)
mustache (>= 0.99.4)
Expand Down Expand Up @@ -44,7 +43,6 @@ GEM
addressable (2.2.8)
arel (2.2.1)
builder (3.0.0)
coderay (1.0.7.rc1)
coffee-rails (3.1.1)
coffee-script (>= 2.2.0)
railties (~> 3.1.0)
Expand Down
1 change: 1 addition & 0 deletions example/spec/acceptance/orders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
scope_parameters :order, :all

example_request "Creating an order" do
explanation "First, create an order, then make a later request to get it back"
response_body.should be_json_eql({
"name" => name,
"paid" => paid,
Expand Down
1 change: 1 addition & 0 deletions example/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
end

RspecApiDocumentation.configure do |config|
config.docs_dir = Rails.root.join("public", "docs")
config.format = [:html, :json]
config.url_prefix = "docs/"
config.curl_host = 'http://localhost:3000'
Expand Down
Empty file removed example/test/fixtures/.gitkeep
Empty file.
Empty file removed example/test/functional/.gitkeep
Empty file.
Empty file removed example/test/integration/.gitkeep
Empty file.
12 changes: 0 additions & 12 deletions example/test/performance/browsing_test.rb

This file was deleted.

13 changes: 0 additions & 13 deletions example/test/test_helper.rb

This file was deleted.

Empty file removed example/test/unit/.gitkeep
Empty file.
Empty file.
Empty file removed example/vendor/plugins/.gitkeep
Empty file.
4 changes: 1 addition & 3 deletions features/callbacks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,5 @@ Feature: Document callbacks
| User-Agent | InterestingThingApp |
And I should see the following request body:
"""
{
"message": "Something interesting happened!"
}
{"message":"Something interesting happened!"}
"""
4 changes: 1 addition & 3 deletions features/html_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,5 @@ Feature: Generate HTML documentation from test examples
| Content-Length | 35 |
And I should see the following response body:
"""
{
"hello": "rspec_api_documentation"
}
{"hello":"rspec_api_documentation"}
"""
1 change: 0 additions & 1 deletion gemfiles/minimum_dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ gem "i18n", "0.3.6"
gem "mustache", "0.99.4"
gem "webmock", "1.8.0"
gem "json", "1.4.6"
gem "coderay", "1.0.7.rc1"
gem "rack-test", "0.5.5"
gem "rack-oauth2", "0.14.4"

Expand Down
1 change: 0 additions & 1 deletion lib/rspec_api_documentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module RspecApiDocumentation
autoload :Index
autoload :ClientBase
autoload :Headers
autoload :Syntax
end

autoload :DSL
Expand Down
1 change: 1 addition & 0 deletions lib/rspec_api_documentation/api_documentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def clear_docs
FileUtils.rm_rf(docs_dir, :secure => true)
end
FileUtils.mkdir_p(docs_dir)
FileUtils.cp_r(File.join(configuration.template_path, "assets"), docs_dir)
end

def document_example(rspec_example)
Expand Down
23 changes: 9 additions & 14 deletions lib/rspec_api_documentation/client_base.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module RspecApiDocumentation
class ClientBase < Struct.new(:context, :options)
include Headers
include Syntax

delegate :example, :app, :to => :context
delegate :metadata, :to => :example
Expand Down Expand Up @@ -40,13 +39,15 @@ def document_example(method, path, params)

request_metadata[:request_method] = method
request_metadata[:request_path] = path
request_metadata[:request_body] = highlight_syntax(request_body, content_type, true)
request_metadata[:request_headers] = format_headers(request_headers)
request_metadata[:request_query_parameters] = format_query_hash(query_hash)
request_metadata[:request_body] = request_body.empty? ? nil : request_body
request_metadata[:request_headers] = request_headers
request_metadata[:request_query_parameters] = query_hash
request_metadata[:request_content_type] = request_content_type
request_metadata[:response_status] = status
request_metadata[:response_status_text] = Rack::Utils::HTTP_STATUS_CODES[status]
request_metadata[:response_body] = highlight_syntax(response_body, response_headers['Content-Type'])
request_metadata[:response_headers] = format_headers(response_headers)
request_metadata[:response_body] = response_body.empty? ? nil : response_body
request_metadata[:response_headers] = response_headers
request_metadata[:response_content_type] = response_content_type
request_metadata[:curl] = Curl.new(method, path, request_body, request_headers)

metadata[:requests] ||= []
Expand All @@ -56,18 +57,12 @@ def document_example(method, path, params)
def query_hash
strings = query_string.split("&")
arrays = strings.map do |segment|
segment.split("=")
k,v = segment.split("=")
[k, CGI.unescape(v)]
end
Hash[arrays]
end

def format_query_hash(query_hash)
return if query_hash.blank?
query_hash.map do |key, value|
"#{key}: #{CGI.unescape(value)}"
end.join("\n")
end

def headers(method, path, params)
if options && options[:headers]
options[:headers]
Expand Down
8 changes: 4 additions & 4 deletions lib/rspec_api_documentation/curl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ def output(config_host)
end

def post
"curl #{url} #{post_data} -X POST #{headers}"
"curl \"#{url}\" #{post_data} -X POST #{headers}"
end

def get
"curl #{url}#{get_data} -X GET #{headers}"
"curl \"#{url}#{get_data}\" -X GET #{headers}"
end

def put
"curl #{url} #{post_data} -X PUT #{headers}"
"curl \"#{url}\" #{post_data} -X PUT #{headers}"
end

def delete
"curl #{url} -X DELETE #{headers}"
"curl \"#{url}\" #{post_data} -X DELETE #{headers}"
end

def url
Expand Down
17 changes: 16 additions & 1 deletion lib/rspec_api_documentation/html_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ def filename
end

def requests
super.map do |hash|
super.collect do |hash|
hash[:request_headers_hash] = hash[:request_headers].collect { |k, v| {:name => k, :value => v} }
hash[:request_headers_text] = format_hash(hash[:request_headers])
hash[:request_path_no_query] = hash[:request_path].split('?').first
hash[:request_query_parameters_text] = format_hash(hash[:request_query_parameters])
hash[:request_query_parameters_hash] = hash[:request_query_parameters].collect { |k, v| {:name => k, :value => v} } if hash[:request_query_parameters].present?
hash[:response_headers_text] = format_hash(hash[:response_headers])
hash[:response_status] = hash[:response_status].to_s + " " + Rack::Utils::HTTP_STATUS_CODES[hash[:response_status]].to_s
if @host
hash[:curl] = hash[:curl].output(@host) if hash[:curl].is_a? RspecApiDocumentation::Curl
else
Expand All @@ -82,5 +89,13 @@ def requests
def url_prefix
configuration.url_prefix
end

private
def format_hash(hash = {})
return "" unless hash.present?
hash.collect do |k, v|
"#{k}: #{v}"
end.join("\n")
end
end
end
6 changes: 5 additions & 1 deletion lib/rspec_api_documentation/oauth2_mac_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ def response_body
last_response.body
end

def content_type
def request_content_type
last_request.content_type
end

def response_content_type
last_response.content_type
end

protected

def do_request(method, path, params)
Expand Down
6 changes: 5 additions & 1 deletion lib/rspec_api_documentation/rack_test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ def response_body
last_response.body
end

def content_type
def request_content_type
last_request.content_type
end

def response_content_type
last_response.content_type
end

protected

def do_request(method, path, params)
Expand Down
33 changes: 0 additions & 33 deletions lib/rspec_api_documentation/syntax.rb

This file was deleted.

7 changes: 3 additions & 4 deletions lib/rspec_api_documentation/test_server.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module RspecApiDocumentation
class TestServer < Struct.new(:context)
include Headers
include Syntax

delegate :example, :to => :context
delegate :metadata, :to => :example
Expand All @@ -18,10 +17,10 @@ def call(env)

request_metadata = {}

request_metadata[:request_method] = request_method
request_metadata[:request_method] = @request_method
request_metadata[:request_path] = env["PATH_INFO"]
request_metadata[:request_body] = highlight_syntax(request_body, request_headers["Content-Type"], true)
request_metadata[:request_headers] = format_headers(@request_headers)
request_metadata[:request_body] = @request_body
request_metadata[:request_headers] = @request_headers

metadata[:requests] ||= []
metadata[:requests] << request_metadata
Expand Down
1 change: 0 additions & 1 deletion rspec_api_documentation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "mustache", ">= 0.99.4"
s.add_runtime_dependency "webmock", ">= 1.7.0"
s.add_runtime_dependency "json", ">= 1.4.6"
s.add_runtime_dependency "coderay", ">= 1.0.7.rc1"

s.add_development_dependency "fakefs"
s.add_development_dependency "sinatra"
Expand Down
2 changes: 1 addition & 1 deletion spec/api_documentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
test_file = configuration.docs_dir.join("test")
FileUtils.mkdir_p configuration.docs_dir
FileUtils.touch test_file

FileUtils.stub(:cp_r)
subject.clear_docs

File.directory?(configuration.docs_dir).should be_true
Expand Down
17 changes: 9 additions & 8 deletions spec/rack_test_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ class StubApp < Sinatra::Base
metadata[:request_method].should eq("POST")
metadata[:request_path].should eq("/greet?query=test+query")
metadata[:request_body].should be_present
metadata[:request_headers].should match(/^Content-Type: application\/json/)
metadata[:request_headers].should match(/^X-Custom-Header: custom header value$/)
metadata[:request_query_parameters].should eq("query: test query")
metadata[:request_headers].should include({'Content-Type' => 'application/json;charset=utf-8'})
metadata[:request_headers].should include({'X-Custom-Header' => 'custom header value'})
metadata[:request_query_parameters].should == {"query" => "test query"}
metadata[:request_content_type].should match(/application\/json/)
metadata[:response_status].should eq(200)
metadata[:response_status_text].should eq("OK")
metadata[:response_body].should be_present
metadata[:response_headers].should match(/^Content-Type: application\/json/)
metadata[:response_headers].should match(/^Content-Length: 17$/)
metadata[:response_headers]['Content-Type'].should match(/application\/json/)
metadata[:response_headers]['Content-Length'].should == '17'
metadata[:response_content_type].should match(/application\/json/)
metadata[:curl].should eq(RspecApiDocumentation::Curl.new("POST", "/greet?query=test+query", post_data, {"Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value", "Host" => "example.org", "Cookie" => ""}))
end

Expand All @@ -151,8 +152,8 @@ class StubApp < Sinatra::Base
context "when post data is nil" do
let(:post_data) { }

it "should not nil out request_body" do
example.metadata[:requests].first[:request_body].should eq(nil)
it "should nil out request_body" do
example.metadata[:requests].first[:request_body].should be_nil
end
end
end
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added templates/assets/img/glyphicons-halflings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cd433b2

Please sign in to comment.