Skip to content

Commit

Permalink
Merge pull request #291 from myabc/fix/response_fields-nested-scopes-…
Browse files Browse the repository at this point in the history
…formatting

Fix formatting of nested response_field scopes
  • Loading branch information
oestrich authored Dec 8, 2016
2 parents 8d0a315 + d56a820 commit c7eff87
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
19 changes: 12 additions & 7 deletions features/html_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ Feature: Generate HTML documentation from test examples
request = Rack::Request.new(env)
response = Rack::Response.new
response["Content-Type"] = "application/json"
response.write({ "hello" => request.params["target"] }.to_json)
response.write({
"hello" => request.params["target"],
"more_greetings" => { "bonjour" => { "message" => "le monde" } }
}.to_json)
response.finish
end
end
Expand All @@ -31,14 +34,15 @@ Feature: Generate HTML documentation from test examples
parameter :scoped, "This is a scoped variable", :scope => :scope
parameter :sub, "This is scoped", :scope => [:scope, :further]
response_field :hello, "The greeted thing"
response_field :hello, "The greeted thing"
response_field :message, "Translated greeting", scope: [:more_greetings, :bonjour]
example "Greeting your favorite gem" do
do_request :target => "rspec_api_documentation"
expect(response_headers["Content-Type"]).to eq("application/json")
expect(status).to eq(200)
expect(response_body).to eq('{"hello":"rspec_api_documentation"}')
expect(response_body).to eq('{"hello":"rspec_api_documentation","more_greetings":{"bonjour":{"message":"le monde"}}}')
end
end
end
Expand Down Expand Up @@ -71,12 +75,13 @@ Feature: Generate HTML documentation from test examples
| scope[scoped] | This is a scoped variable |
| scope[further][sub] | This is scoped |

Scenario: Examle HTML documentation should include the response fields
Scenario: Example HTML documentation should include the response fields
When I open the index
And I navigate to "Greeting your favorite gem"
Then I should see the following response fields:
| name | description |
| hello | The greeted thing |
| name | description |
| hello | The greeted thing |
| more_greetings[bonjour][message] | Translated greeting |

Scenario: Example HTML documentation includes the request information
When I open the index
Expand All @@ -99,5 +104,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", "more_greetings": { "bonjour": { "message": "le monde" } } }
"""
27 changes: 19 additions & 8 deletions lib/rspec_api_documentation/views/markup_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ def filename
def parameters
super.each do |parameter|
if parameter.has_key?(:scope)
scope = Array(parameter[:scope]).each_with_index.map do |scope, index|
if index == 0
scope
else
"[#{scope}]"
end
end.join
parameter[:scope] = scope
parameter[:scope] = format_scope(parameter[:scope])
end
end
end

def response_fields
super.each do |response_field|
if response_field.has_key?(:scope)
response_field[:scope] = format_scope(response_field[:scope])
end
end
end
Expand Down Expand Up @@ -72,6 +73,16 @@ def format_hash(hash = {})
"#{k}: #{v}"
end.join("\n")
end

def format_scope(unformatted_scope)
Array(unformatted_scope).each_with_index.map do |scope, index|
if index == 0
scope
else
"[#{scope}]"
end
end.join
end
end
end
end

0 comments on commit c7eff87

Please sign in to comment.