Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/rspec_api_documentation/views/api_blueprint_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ def sections
super.map do |section|
routes = section[:examples].group_by { |e| "#{e.route_uri}#{e.route_optionals}#{e.route_name}" }.map do |route, examples|
attrs = fields(:attributes, examples)
params = fields(:parameters, examples)

methods = examples.group_by(&:http_method).map do |http_method, examples|
params = fields(:parameters, examples)

{
"has_parameters?".to_sym => params.size > 0,
http_method: http_method,
parameters: params,
description: examples.first.respond_to?(:action_name) && examples.first.action_name,
examples: examples
}
end

{
"has_attributes?".to_sym => attrs.size > 0,
"has_parameters?".to_sym => params.size > 0,
route: format_route(examples[0]),
route_name: examples[0][:route_name],
attributes: attrs,
parameters: params,
http_methods: methods
}
end
Expand Down
44 changes: 30 additions & 14 deletions spec/views/api_blueprint_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,36 @@
post_route = sections[1][:routes][1]
post_route_with_optionals = sections[1][:routes][2]

comments_examples = comments_route[:http_methods].map { |http_method| http_method[:examples] }.flatten
http_methods = comments_route[:http_methods]
comments_examples = http_methods.map { |http_method| http_method[:examples] }.flatten
examples_has_parameters = http_methods.map { |http_method| http_method[:has_parameters?] }
examples_parameters = http_methods.map { |http_method| http_method[:parameters] }

expect(comments_examples.size).to eq 1
expect(comments_route[:route]).to eq "/comments"
expect(comments_route[:route_name]).to eq "Comments Collection"
expect(comments_route[:has_parameters?]).to eq false
expect(comments_route[:parameters]).to eq []
expect(examples_has_parameters).to eq [false]
expect(examples_parameters).to eq [[]]
expect(comments_route[:has_attributes?]).to eq false
expect(comments_route[:attributes]).to eq []

post_examples = post_route[:http_methods].map { |http_method| http_method[:examples] }.flatten
http_methods = post_route[:http_methods]
post_examples = http_methods.map { |http_method| http_method[:examples] }.flatten
examples_has_parameters = http_methods.map { |http_method| http_method[:has_parameters?] }
examples_parameters = http_methods.map { |http_method| http_method[:parameters] }

expect(post_examples.size).to eq 2
expect(post_route[:route]).to eq "/posts/{id}"
expect(post_route[:route_name]).to eq "Single Post"
expect(post_route[:has_parameters?]).to eq true
expect(post_route[:parameters]).to eq [{
expect(examples_has_parameters).to eq [true]
expect(examples_parameters).to eq [[{
required: true,
type: "string",
example: "1",
name: "id",
description: "The id",
properties_description: "required, string"
}]
}]]
expect(post_route[:has_attributes?]).to eq true
expect(post_route[:attributes]).to eq [{
required: true,
Expand All @@ -128,12 +136,16 @@
properties_description: "required"
}]

post_w_optionals_examples = post_route_with_optionals[:http_methods].map { |http_method| http_method[:examples] }.flatten
http_methods = post_route_with_optionals[:http_methods]
post_w_optionals_examples = http_methods.map { |http_method| http_method[:examples] }.flatten
examples_has_parameters = http_methods.map { |http_method| http_method[:has_parameters?] }
examples_parameters = http_methods.map { |http_method| http_method[:parameters] }

expect(post_w_optionals_examples.size).to eq 1
expect(post_route_with_optionals[:route]).to eq "/posts/{id}{?option=:option}"
expect(post_route_with_optionals[:route_name]).to eq "Single Post"
expect(post_route_with_optionals[:has_parameters?]).to eq true
expect(post_route_with_optionals[:parameters]).to eq [{
expect(examples_has_parameters).to eq [true]
expect(examples_parameters).to eq [[{
required: true,
type: "string",
example: "1",
Expand All @@ -144,16 +156,20 @@
name: "option",
description: nil,
properties_description: 'optional'
}]
}]]
expect(post_route_with_optionals[:has_attributes?]).to eq false
expect(post_route_with_optionals[:attributes]).to eq []

posts_examples = posts_route[:http_methods].map { |http_method| http_method[:examples] }.flatten
http_methods = posts_route[:http_methods]
posts_examples = http_methods.map { |http_method| http_method[:examples] }.flatten
examples_has_parameters = http_methods.map { |http_method| http_method[:has_parameters?] }
examples_parameters = http_methods.map { |http_method| http_method[:parameters] }

expect(posts_examples.size).to eq 1
expect(posts_route[:route]).to eq "/posts"
expect(posts_route[:route_name]).to eq "Posts Collection"
expect(posts_route[:has_parameters?]).to eq false
expect(posts_route[:parameters]).to eq []
expect(examples_has_parameters).to eq [false]
expect(examples_parameters).to eq [[]]
expect(posts_route[:has_attributes?]).to eq true
expect(posts_route[:attributes]).to eq [{
required: false,
Expand Down
26 changes: 13 additions & 13 deletions templates/rspec_api_documentation/api_blueprint_index.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ description: {{ description }}

explanation: {{ explanation }}
{{/ explanation }}
{{# has_parameters? }}
{{# has_attributes? }}

+ Parameters
{{# parameters }}
+ Attributes (object)
{{# attributes }}
+ {{ name }}{{# example }}: {{ example }}{{/ example }}{{# properties_description }} ({{ properties_description }}){{/ properties_description }}{{# description }} - {{ description }}{{/ description }}
{{# has_default?}}
+ Default: `{{default}}`
Expand All @@ -40,12 +40,15 @@ explanation: {{ explanation }}
{{# annotations }}
{{ . }}
{{/ annotations }}
{{/ parameters }}
{{/ has_parameters? }}
{{# has_attributes? }}
{{/ attributes }}
{{/ has_attributes? }}
{{# http_methods }}

+ Attributes (object)
{{# attributes }}
### {{ description }} [{{ http_method }}]
{{# has_parameters? }}

+ Parameters
{{# parameters }}
+ {{ name }}{{# example }}: {{ example }}{{/ example }}{{# properties_description }} ({{ properties_description }}){{/ properties_description }}{{# description }} - {{ description }}{{/ description }}
{{# has_default?}}
+ Default: `{{default}}`
Expand All @@ -59,11 +62,8 @@ explanation: {{ explanation }}
{{# annotations }}
{{ . }}
{{/ annotations }}
{{/ attributes }}
{{/ has_attributes? }}
{{# http_methods }}

### {{ description }} [{{ http_method }}]
{{/ parameters }}
{{/ has_parameters? }}
{{# examples }}
{{# requests }}
{{# has_request? }}
Expand Down