Use ranges for allowed values instead of arrays

This commit is contained in:
Martin Lensment 2015-04-01 13:45:59 +03:00
parent 21f07076cd
commit 42001c4601
3 changed files with 41 additions and 30 deletions

View file

@ -6,7 +6,7 @@ Returns contacts of the current registrar.
| Field name | Required | Type | Allowed values |
| ---------- | -------- | ---- | -------------- |
| limit | false | Integer | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
| limit | false | Integer | [1..20] |
| offset | false | Integer | |
| details | false | String | ["true", "false"] |
@ -23,26 +23,26 @@ Content-Type: application/json
```
HTTP/1.1 200
Cache-Control: max-age=0, private, must-revalidate
Content-Length: 566
Content-Length: 586
Content-Type: application/json
{
"contacts": [
{
"id": 1,
"code": "sh889404040",
"code": "sh651514820",
"reg_no": null,
"phone": "+372.12345678",
"email": "annetta.toy@mitchell.org",
"email": "giles_altenwerth@oberbrunnerbrakus.com",
"fax": null,
"created_at": "2015-04-01T10:30:47.470Z",
"updated_at": "2015-04-01T10:30:47.470Z",
"created_at": "2015-04-01T10:45:28.045Z",
"updated_at": "2015-04-01T10:45:28.045Z",
"ident": "37605030299",
"ident_type": "priv",
"created_by_id": null,
"updated_by_id": null,
"auth_info": "password",
"name": "Rogers Bruen0",
"name": "Ms. Lucienne Olson0",
"org_name": null,
"registrar_id": 1,
"creator_str": "autotest",
@ -81,7 +81,7 @@ Content-Type: application/json
{
"contacts": [
"sh914462381"
"sh371827431"
],
"total_number_of_records": 2
}

View file

@ -6,7 +6,7 @@ Returns domains of the current registrar.
| Field name | Required | Type | Allowed values |
| ---------- | -------- | ---- | -------------- |
| limit | false | Integer | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
| limit | false | Integer | [1..20] |
| offset | false | Integer | |
| details | false | String | ["true", "false"] |
@ -32,21 +32,21 @@ Content-Type: application/json
"id": 1,
"name": "domain0.ee",
"registrar_id": 1,
"registered_at": "2015-04-01T10:30:48.773Z",
"registered_at": "2015-04-01T10:45:28.858Z",
"status": null,
"valid_from": "2015-04-01T00:00:00.000Z",
"valid_to": "2016-04-01T00:00:00.000Z",
"owner_contact_id": 1,
"auth_info": "1a93d4599945df52de0a38c64b470e67",
"created_at": "2015-04-01T10:30:48.768Z",
"updated_at": "2015-04-01T10:30:48.762Z",
"auth_info": "81d71922080506c636030ff5634823ba",
"created_at": "2015-04-01T10:45:28.854Z",
"updated_at": "2015-04-01T10:45:28.850Z",
"name_dirty": "domain0.ee",
"name_puny": "domain0.ee",
"period": 1,
"period_unit": "y",
"creator_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-04-01 10:30:48\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:30:48\n changed: 2015-04-01 10:30:48\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:45:28\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:45:28\n changed: 2015-04-01 10:45:28\n"
}
],
"total_number_of_records": 2

View file

@ -38,7 +38,7 @@ module Autodoc
route = request.env["rack.routing_args"][:route_info]
return unless route.route_params.is_a?(Hash)
params_details = [
rows = [
"| Field name | Required | Type | Allowed values |",
"| ---------- | -------- | ---- | -------------- |"
]
@ -48,17 +48,14 @@ module Autodoc
details << "| #{name} "
details << "| #{desc[:required]} "
details << "| #{desc[:type]} "
details << "| #{desc[:values]} |"
params_details << details.join
# required = desc.is_a?(Hash) ? desc[:required] : false
# description = desc.is_a?(Hash) ? desc[:description] : desc.to_s
# [name, required, " * #{name}: #{description} #{required ? '(required)' : ''}"]
details << "| #{ranges_from_array(desc[:values])} |"
rows << details.join
end
prettify_table(params_details).join("\n")
pretty_table(rows).join("\n")
end
def prettify_table(rows)
def pretty_table(rows)
# longest_in_col = 0
matrix_array = []
rows.each do |x|
@ -83,6 +80,20 @@ module Autodoc
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