diff --git a/app/api/repp/contact_v1.rb b/app/api/repp/contact_v1.rb index 52403fb2d..af7526231 100644 --- a/app/api/repp/contact_v1.rb +++ b/app/api/repp/contact_v1.rb @@ -5,8 +5,9 @@ module Repp resource :contacts do desc 'Return list of contact' params do - optional :limit, type: Integer, values: (1..20).to_a - optional :offset, type: Integer + optional :limit, type: Integer, values: (1..20).to_a, desc: 'How many contacts to show' + optional :offset, type: Integer, desc: 'Contact number to start at' + optional :details, type: String, values: %w(true false), desc: 'Whether to include details' end get '/' do diff --git a/app/api/repp/domain_v1.rb b/app/api/repp/domain_v1.rb index dbdcc778d..99b9c4cb3 100644 --- a/app/api/repp/domain_v1.rb +++ b/app/api/repp/domain_v1.rb @@ -5,8 +5,9 @@ module Repp resource :domains do desc 'Return list of domains' params do - optional :limit, type: Integer, values: (1..20).to_a - optional :offset, type: Integer + optional :limit, type: Integer, values: (1..20).to_a, desc: 'How many domains to show' + optional :offset, type: Integer, desc: 'Domain number to start at' + optional :details, type: String, values: %w(true false), desc: 'Whether to include details' end get '/' do diff --git a/doc/repp/v1/contact.md b/doc/repp/v1/contact.md index bc50cda54..ebe38c476 100644 --- a/doc/repp/v1/contact.md +++ b/doc/repp/v1/contact.md @@ -1,7 +1,14 @@ ## GET /repp/v1/contacts 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 ``` @@ -16,31 +23,36 @@ Content-Type: application/json ``` HTTP/1.1 200 Cache-Control: max-age=0, private, must-revalidate -Content-Length: 483 +Content-Length: 573 Content-Type: application/json { "contacts": [ { "id": 1, - "code": "sh470279120", + "code": "sh095058820", "reg_no": null, "phone": "+372.12345678", - "email": "bettye.feil@ratkegoldner.net", + "email": "shakira.reichert@corkery.com", "fax": null, - "created_at": "2015-03-31T07:39:10.854Z", - "updated_at": "2015-03-31T07:39:10.854Z", + "created_at": "2015-04-01T10:51:49.854Z", + "updated_at": "2015-04-01T10:51:49.854Z", "ident": "37605030299", "ident_type": "priv", "created_by_id": null, "updated_by_id": null, "auth_info": "password", - "name": "Leopoldo Waelchi0", + "name": "Henderson Feest0", "org_name": null, "registrar_id": 1, "creator_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 @@ -50,7 +62,6 @@ Content-Type: application/json ## GET /repp/v1/contacts Returns contact names with offset. -### Example #### Request ``` @@ -70,64 +81,8 @@ Content-Type: application/json { "contacts": [ - "sh226475261" + "sh331036241" ], "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" -} -``` diff --git a/doc/repp/v1/domain.md b/doc/repp/v1/domain.md index f717f7da7..06234524d 100644 --- a/doc/repp/v1/domain.md +++ b/doc/repp/v1/domain.md @@ -1,7 +1,14 @@ ## GET /repp/v1/domains 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 ``` @@ -25,21 +32,21 @@ Content-Type: application/json "id": 1, "name": "domain0.ee", "registrar_id": 1, - "registered_at": "2015-03-31T07:39:11.598Z", + "registered_at": "2015-04-01T10:51:53.445Z", "status": null, - "valid_from": "2015-03-31T00:00:00.000Z", - "valid_to": "2016-03-31T00:00:00.000Z", + "valid_from": "2015-04-01T00:00:00.000Z", + "valid_to": "2016-04-01T00:00:00.000Z", "owner_contact_id": 1, - "auth_info": "fc7828fbc275ff16b86a31def3e7a60d", - "created_at": "2015-03-31T07:39:11.595Z", - "updated_at": "2015-03-31T07:39:11.591Z", + "auth_info": "15d4b95d0611312ee96e63e48fcab2de", + "created_at": "2015-04-01T10:51:53.439Z", + "updated_at": "2015-04-01T10:51:53.432Z", "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-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 @@ -49,7 +56,6 @@ Content-Type: application/json ## GET /repp/v1/domains Returns domain names with offset. -### Example #### Request ``` @@ -74,59 +80,3 @@ Content-Type: application/json "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" -} -``` diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 8c63e5109..019f638cb 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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 diff --git a/spec/requests/contact_spec.rb b/spec/requests/contact_spec.rb index e9c9c4112..bd5f8567b 100644 --- a/spec/requests/contact_spec.rb +++ b/spec/requests/contact_spec.rb @@ -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) diff --git a/spec/requests/domain_spec.rb b/spec/requests/domain_spec.rb index d1d17b819..e4a10680a 100644 --- a/spec/requests/domain_spec.rb +++ b/spec/requests/domain_spec.rb @@ -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 diff --git a/spec/requests/repp_doc_template.md.erb b/spec/requests/repp_doc_template.md.erb new file mode 100644 index 000000000..100e95081 --- /dev/null +++ b/spec/requests/repp_doc_template.md.erb @@ -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 %> +``` diff --git a/spec/support/request.rb b/spec/support/request.rb index 053874f0b..5c7315cfe 100644 --- a/spec/support/request.rb +++ b/spec/support/request.rb @@ -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