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

@ -5,8 +5,9 @@ module Repp
resource :contacts do resource :contacts do
desc 'Return list of contact' desc 'Return list of contact'
params do params do
optional :limit, type: Integer, values: (1..20).to_a optional :limit, type: Integer, values: (1..20).to_a, desc: 'How many contacts to show'
optional :offset, type: Integer optional :offset, type: Integer, desc: 'Contact number to start at'
optional :details, type: String, values: %w(true false), desc: 'Whether to include details'
end end
get '/' do get '/' do

View file

@ -5,8 +5,9 @@ module Repp
resource :domains do resource :domains do
desc 'Return list of domains' desc 'Return list of domains'
params do params do
optional :limit, type: Integer, values: (1..20).to_a optional :limit, type: Integer, values: (1..20).to_a, desc: 'How many domains to show'
optional :offset, type: Integer optional :offset, type: Integer, desc: 'Domain number to start at'
optional :details, type: String, values: %w(true false), desc: 'Whether to include details'
end end
get '/' do get '/' do

View file

@ -1,7 +1,14 @@
## GET /repp/v1/contacts ## GET /repp/v1/contacts
Returns contacts of the current registrar. Returns contacts of the current registrar.
### Example
#### Parameters
| Field name | Required | Type | Allowed values | Description |
| ---------- | -------- | ---- | -------------- | ----------- |
| limit | false | Integer | [1..20] | How many contacts to show |
| offset | false | Integer | | Contact number to start at |
| details | false | String | ["true", "false"] | Whether to include details |
#### Request #### Request
``` ```
@ -16,31 +23,36 @@ Content-Type: application/json
``` ```
HTTP/1.1 200 HTTP/1.1 200
Cache-Control: max-age=0, private, must-revalidate Cache-Control: max-age=0, private, must-revalidate
Content-Length: 483 Content-Length: 573
Content-Type: application/json Content-Type: application/json
{ {
"contacts": [ "contacts": [
{ {
"id": 1, "id": 1,
"code": "sh470279120", "code": "sh095058820",
"reg_no": null, "reg_no": null,
"phone": "+372.12345678", "phone": "+372.12345678",
"email": "bettye.feil@ratkegoldner.net", "email": "shakira.reichert@corkery.com",
"fax": null, "fax": null,
"created_at": "2015-03-31T07:39:10.854Z", "created_at": "2015-04-01T10:51:49.854Z",
"updated_at": "2015-03-31T07:39:10.854Z", "updated_at": "2015-04-01T10:51:49.854Z",
"ident": "37605030299", "ident": "37605030299",
"ident_type": "priv", "ident_type": "priv",
"created_by_id": null, "created_by_id": null,
"updated_by_id": null, "updated_by_id": null,
"auth_info": "password", "auth_info": "password",
"name": "Leopoldo Waelchi0", "name": "Henderson Feest0",
"org_name": null, "org_name": null,
"registrar_id": 1, "registrar_id": 1,
"creator_str": "autotest", "creator_str": "autotest",
"updator_str": "autotest", "updator_str": "autotest",
"ident_country_code": "EE" "ident_country_code": "EE",
"city": "Tallinn",
"street": "Short street 11",
"zip": "11111",
"country_code": "EE",
"state": null
} }
], ],
"total_number_of_records": 2 "total_number_of_records": 2
@ -50,7 +62,6 @@ Content-Type: application/json
## GET /repp/v1/contacts ## GET /repp/v1/contacts
Returns contact names with offset. Returns contact names with offset.
### Example
#### Request #### Request
``` ```
@ -70,64 +81,8 @@ Content-Type: application/json
{ {
"contacts": [ "contacts": [
"sh226475261" "sh331036241"
], ],
"total_number_of_records": 2 "total_number_of_records": 2
} }
``` ```
## GET /repp/v1/contacts
Returns contact names of the current registrar.
### Example
#### Request
```
GET /repp/v1/contacts HTTP/1.1
Accept: application/json
Authorization: Basic Z2l0bGFiOmdoeXQ5ZTRmdQ==
Content-Length: 0
Content-Type: application/json
```
#### Response
```
HTTP/1.1 200
Cache-Control: max-age=0, private, must-revalidate
Content-Length: 70
Content-Type: application/json
{
"contacts": [
"sh470279120",
"sh226475261"
],
"total_number_of_records": 2
}
```
## GET /repp/v1/contacts
Returns an error with invalid parameters in contact index.
### Example
#### Request
```
GET /repp/v1/contacts?limit=0 HTTP/1.1
Accept: application/json
Authorization: Basic Z2l0bGFiOmdoeXQ5ZTRmdQ==
Content-Length: 0
Content-Type: application/json
```
#### Response
```
HTTP/1.1 400
Cache-Control: no-cache
Content-Length: 45
Content-Type: application/json
{
"error": "limit does not have a valid value"
}
```

View file

@ -1,7 +1,14 @@
## GET /repp/v1/domains ## GET /repp/v1/domains
Returns domains of the current registrar. Returns domains of the current registrar.
### Example
#### Parameters
| Field name | Required | Type | Allowed values | Description |
| ---------- | -------- | ---- | -------------- | ----------- |
| limit | false | Integer | [1..20] | How many domains to show |
| offset | false | Integer | | Domain number to start at |
| details | false | String | ["true", "false"] | Whether to include details |
#### Request #### Request
``` ```
@ -25,21 +32,21 @@ Content-Type: application/json
"id": 1, "id": 1,
"name": "domain0.ee", "name": "domain0.ee",
"registrar_id": 1, "registrar_id": 1,
"registered_at": "2015-03-31T07:39:11.598Z", "registered_at": "2015-04-01T10:51:53.445Z",
"status": null, "status": null,
"valid_from": "2015-03-31T00:00:00.000Z", "valid_from": "2015-04-01T00:00:00.000Z",
"valid_to": "2016-03-31T00:00:00.000Z", "valid_to": "2016-04-01T00:00:00.000Z",
"owner_contact_id": 1, "owner_contact_id": 1,
"auth_info": "fc7828fbc275ff16b86a31def3e7a60d", "auth_info": "15d4b95d0611312ee96e63e48fcab2de",
"created_at": "2015-03-31T07:39:11.595Z", "created_at": "2015-04-01T10:51:53.439Z",
"updated_at": "2015-03-31T07:39:11.591Z", "updated_at": "2015-04-01T10:51:53.432Z",
"name_dirty": "domain0.ee", "name_dirty": "domain0.ee",
"name_puny": "domain0.ee", "name_puny": "domain0.ee",
"period": 1, "period": 1,
"period_unit": "y", "period_unit": "y",
"creator_str": null, "creator_str": null,
"updator_str": null, "updator_str": null,
"whois_body": " This Whois Server contains information on\n Estonian Top Level Domain ee TLD\n\n domain: domain0.ee\n registrar: registrar1\n status:\n registered: \n changed: 2015-03-31 07:39:11\n expire:\n outzone:\n delete:\n\n \n\n nsset:\n nserver:\n\n registrar: registrar1\n phone: \n address: Street 111, Town, County, Postal\n created: 2015-03-31 07:39:11\n changed: 2015-03-31 07:39:11\n" "whois_body": " This Whois Server contains information on\n Estonian Top Level Domain ee TLD\n\n domain: domain0.ee\n registrar: registrar1\n status:\n registered: \n changed: 2015-04-01 10:51:53\n expire:\n outzone:\n delete:\n\n \n\n nsset:\n nserver:\n\n registrar: registrar1\n phone: \n address: Street 111, Town, County, Postal\n created: 2015-04-01 10:51:53\n changed: 2015-04-01 10:51:53\n"
} }
], ],
"total_number_of_records": 2 "total_number_of_records": 2
@ -49,7 +56,6 @@ Content-Type: application/json
## GET /repp/v1/domains ## GET /repp/v1/domains
Returns domain names with offset. Returns domain names with offset.
### Example
#### Request #### Request
``` ```
@ -74,59 +80,3 @@ Content-Type: application/json
"total_number_of_records": 2 "total_number_of_records": 2
} }
``` ```
## GET /repp/v1/domains
Returns domain names of the current registrar.
### Example
#### Request
```
GET /repp/v1/domains HTTP/1.1
Accept: application/json
Authorization: Basic Z2l0bGFiOmdoeXQ5ZTRmdQ==
Content-Length: 0
Content-Type: application/json
```
#### Response
```
HTTP/1.1 200
Cache-Control: max-age=0, private, must-revalidate
Content-Length: 67
Content-Type: application/json
{
"domains": [
"domain0.ee",
"domain1.ee"
],
"total_number_of_records": 2
}
```
## GET /repp/v1/domains
Returns an error with invalid parameters in domain index.
### Example
#### Request
```
GET /repp/v1/domains?limit=0 HTTP/1.1
Accept: application/json
Authorization: Basic Z2l0bGFiOmdoeXQ5ZTRmdQ==
Content-Length: 0
Content-Type: application/json
```
#### Response
```
HTTP/1.1 400
Cache-Control: no-cache
Content-Length: 45
Content-Type: application/json
{
"error": "limit does not have a valid value"
}
```

View file

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

View file

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

View file

@ -8,9 +8,8 @@ describe Repp::DomainV1 do
Fabricate.times(2, :domain, registrar: @api_user.registrar) Fabricate.times(2, :domain, registrar: @api_user.registrar)
end end
describe 'GET /repp/v1/domains', autodoc: true do describe 'GET /repp/v1/domains' do
it 'returns domains of the current registrar' 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 get_with_auth '/repp/v1/domains', { limit: 1, details: true }, @api_user
response.status.should == 200 response.status.should == 200
@ -30,7 +29,7 @@ describe Repp::DomainV1 do
log[:ip].should == '127.0.0.1' log[:ip].should == '127.0.0.1'
end 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 get_with_auth '/repp/v1/domains', { offset: 1 }, @api_user
response.status.should == 200 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
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| RSpec.configure do |c|
c.include Request, type: :request c.include Request, type: :request
end end