Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a resource explanation. #196

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions lib/rspec_api_documentation/dsl/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def header(name, value)
headers[name] = value
end

def explanation(text)
safe_metadata(:resource_explanation, text)
end

private

def safe_metadata(field, default)
Expand Down
4 changes: 4 additions & 0 deletions lib/rspec_api_documentation/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def has_response_fields?
respond_to?(:response_fields) && response_fields.present?
end

def resource_explanation
metadata[:resource_explanation] || nil
end

def explanation
metadata[:explanation] || nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec_api_documentation/writers/index_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module IndexHelper
def sections(examples, configuration)
resources = examples.group_by(&:resource_name).inject([]) do |arr, (resource_name, examples)|
ordered_examples = configuration.keep_source_order ? examples : examples.sort_by(&:description)
arr.push(:resource_name => resource_name, :examples => ordered_examples)
arr.push(:resource_name => resource_name, :examples => ordered_examples, :resource_explanation => examples.first.metadata[:resource_explanation])
end
configuration.keep_source_order ? resources : resources.sort_by { |resource| resource[:resource_name] }
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec_api_documentation/writers/json_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def as_json(opts = nil)
def section_hash(section)
{
:name => section[:resource_name],
:explanation => section[:resource_explanation],
:examples => section[:examples].map { |example|
{
:description => example.description,
Expand Down Expand Up @@ -85,6 +86,7 @@ def filename
def as_json(opts = nil)
{
:resource => resource_name,
:resource_explanation => resource_explanation,
:http_method => http_method,
:route => route,
:description => description,
Expand Down
5 changes: 5 additions & 0 deletions templates/rspec_api_documentation/html_example.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<body>
<div class="container">
<h1>{{resource_name}} API</h1>
{{# resource_explanation }}
<p class="explanation">
{{{ resource_explanation }}}
</p>
{{/ resource_explanation }}

<div class="article">
<h2>{{ description }}</h2>
Expand Down
5 changes: 5 additions & 0 deletions templates/rspec_api_documentation/html_index.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
{{# sections }}
<div class="article">
<h2>{{ resource_name }}</h2>
{{# resource_explanation }}
<p class="explanation">
{{{ resource_explanation }}}
</p>
{{/ resource_explanation }}

<ul>
{{# examples }}
Expand Down
4 changes: 4 additions & 0 deletions templates/rspec_api_documentation/markdown_example.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# {{ resource_name }} API
{{# resource_explanation }}

{{ resource_explanation }}
{{/ resource_explanation }}

## {{ description }}

Expand Down
4 changes: 4 additions & 0 deletions templates/rspec_api_documentation/markdown_index.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

{{# sections }}
## {{ resource_name }}
{{# resource_explanation }}

{{ resource_explanation }}
{{/ resource_explanation }}

{{# examples }}
* [{{ description }}]({{ dirname }}/{{ filename }})
Expand Down
4 changes: 4 additions & 0 deletions templates/rspec_api_documentation/textile_example.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
h1. {{ resource_name }} API

{{# resource_explanation }}
{{ resource_explanation }}

{{/ resource_explanation }}
h2. {{ description }}

h3. {{ http_method }} {{ route }}
Expand Down
4 changes: 4 additions & 0 deletions templates/rspec_api_documentation/textile_index.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ h1. {{ api_name }}
{{# sections }}
h2. {{ resource_name }}

{{# resource_explanation }}
{{ resource_explanation }}

{{/ resource_explanation }}
{{# examples }}
* "{{ description }}":{{ dirname }}/{{ filename }}
{{/ examples }}
Expand Down