Merge branch 'repp-doc-generator'

This commit is contained in:
Martin Lensment 2015-04-01 13:53:45 +03:00
commit 07973af3a4
9 changed files with 136 additions and 142 deletions

View file

@ -92,4 +92,5 @@ RSpec.configure do |config|
Autodoc.configuration.path = 'doc/repp/v1'
Autodoc.configuration.suppressed_request_header = ['Host']
Autodoc.configuration.suppressed_response_header = ['ETag', 'X-Request-Id', 'X-Runtime']
Autodoc.configuration.template = File.read('spec/requests/repp_doc_template.md.erb')
end

View file

@ -8,8 +8,8 @@ describe Repp::ContactV1 do
Fabricate.times(2, :contact)
end
describe 'GET /repp/v1/contacts', autodoc: true do
it 'returns contacts of the current registrar' do
describe 'GET /repp/v1/contacts' do
it 'returns contacts of the current registrar', autodoc: true, route_info_doc: true do
get_with_auth '/repp/v1/contacts', { limit: 1, details: true }, @api_user
expect(response.status).to eq(200)
@ -29,7 +29,7 @@ describe Repp::ContactV1 do
expect(log[:ip]).to eq('127.0.0.1')
end
it 'returns contact names with offset' do
it 'returns contact names with offset', autodoc: true do
get_with_auth '/repp/v1/contacts', { offset: 1 }, @api_user
expect(response.status).to eq(200)

View file

@ -8,9 +8,8 @@ describe Repp::DomainV1 do
Fabricate.times(2, :domain, registrar: @api_user.registrar)
end
describe 'GET /repp/v1/domains', autodoc: true do
it 'returns domains of the current registrar' do
describe 'GET /repp/v1/domains' do
it 'returns domains of the current registrar', autodoc: true, route_info_doc: true do
get_with_auth '/repp/v1/domains', { limit: 1, details: true }, @api_user
response.status.should == 200
@ -30,7 +29,7 @@ describe Repp::DomainV1 do
log[:ip].should == '127.0.0.1'
end
it 'returns domain names with offset' do
it 'returns domain names with offset', autodoc: true do
get_with_auth '/repp/v1/domains', { offset: 1 }, @api_user
response.status.should == 200

View file

@ -0,0 +1,20 @@
<%# coding: UTF-8 -%>
## <%= title %>
<%= description %>
<% rid = route_info_doc %>
<% if rid %>
#### Parameters
<%= rid %>
<% end %>
#### Request
```
<%= method %> <%= request.path %><%= request_query %> <%= request_http_version %>
<%= request_header %><%= request_body_section %>
```
#### Response
```
<%= response_http_version %> <%= response.status %>
<%= response_header %><%= response_body_section %>
```

View file

@ -31,6 +31,73 @@ module Request
end
end
module Autodoc
class Document
def route_info_doc
return unless example.metadata[:route_info_doc]
route = request.env["rack.routing_args"][:route_info]
return unless route.route_params.is_a?(Hash)
rows = [
"| Field name | Required | Type | Allowed values | Description |",
"| ---------- | -------- | ---- | -------------- | ----------- |"
]
route.route_params.each do |name, desc|
details = []
details << "| #{name} "
details << "| #{desc[:required]} "
details << "| #{desc[:type]} "
details << "| #{ranges_from_array(desc[:values])} "
details << "| #{desc[:desc]} |"
rows << details.join
end
pretty_table(rows).join("\n")
end
def pretty_table(rows)
# longest_in_col = 0
matrix_array = []
rows.each do |x|
matrix_array << x.split('|') + [''] # [''] is because split loses last |
end
new_arr = []
matrix_array.transpose.each do |col|
new_col = []
longest = col.max_by(&:size).size
col.each do |r|
new_col << r.center(longest)
end
new_arr << new_col
end
matrix_array = []
new_arr.transpose.each do |x|
matrix_array << x.join('|')
end
matrix_array
end
def ranges_from_array(a)
return unless a
ranges = a.sort.uniq.reduce([]) do |spans, n|
return a if n.is_a?(String)
if spans.empty? || spans.last.last != n - 1
spans + [n..n]
else
spans[0..-2] + [spans.last.first..n]
end
end
ranges
end
end
end
RSpec.configure do |c|
c.include Request, type: :request
end