updates example; adds postman requests
This commit is contained in:
parent
0c204b1644
commit
fc84bd0b0f
|
@ -16,6 +16,7 @@ readded/reimplemented features could be found in the ToC**
|
||||||
[Configure](#configure)
|
[Configure](#configure)
|
||||||
[Routes Configuration](#routes)
|
[Routes Configuration](#routes)
|
||||||
[Additional documentation](#additions)
|
[Additional documentation](#additions)
|
||||||
|
[Example](#example)
|
||||||
|
|
||||||
|
|
||||||
For how to use at the moment see [v2 specs](https://github.com/LeFnord/grape-swagger/tree/master/spec/swagger_v2) and or [Hussars](https://github.com/LeFnord/hussars) sample app.
|
For how to use at the moment see [v2 specs](https://github.com/LeFnord/grape-swagger/tree/master/spec/swagger_v2) and or [Hussars](https://github.com/LeFnord/hussars) sample app.
|
||||||
|
@ -708,6 +709,14 @@ desc 'thing',
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<a="example" />
|
||||||
|
# Example
|
||||||
|
|
||||||
|
Go into example directory and run it: `$ bundle exec rackup`
|
||||||
|
go to: `http://localhost:9292/swagger_doc` to get it
|
||||||
|
|
||||||
|
For request examples load the [postman file]()
|
||||||
|
|
||||||
## Contributing to grape-swagger
|
## Contributing to grape-swagger
|
||||||
|
|
||||||
See [CONTRIBUTING](CONTRIBUTING.md).
|
See [CONTRIBUTING](CONTRIBUTING.md).
|
||||||
|
|
167
example/api.rb
167
example/api.rb
|
@ -1,66 +1,127 @@
|
||||||
require 'grape'
|
# require 'active_support'
|
||||||
require '../lib/grape-swagger'
|
# require 'active_support/core_ext/string/inflections.rb'
|
||||||
|
|
||||||
@@splines = {}
|
module Api
|
||||||
|
class Root < Grape::API
|
||||||
class Api < Grape::API
|
desc 'API Root'
|
||||||
format :json
|
get do
|
||||||
|
{
|
||||||
desc 'API Root'
|
splines_url: '/splines',
|
||||||
get do
|
file_url: '/file'
|
||||||
{ splines_url: '/splines' }
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
namespace :splines do
|
class Splines < Grape::API
|
||||||
desc 'Return a spline.'
|
@@splines = {}
|
||||||
params do
|
|
||||||
requires :id, type: Integer, desc: 'Spline id.'
|
|
||||||
end
|
|
||||||
get ':id' do
|
|
||||||
@@splines[params[:id]] || error!('Not Found', 404)
|
|
||||||
end
|
|
||||||
|
|
||||||
desc 'Update a spline.'
|
namespace :splines do
|
||||||
params do
|
#
|
||||||
requires :id, type: Integer, desc: 'Spline id.'
|
desc 'Get all splines',
|
||||||
optional :reticulated, type: Boolean, default: true, desc: 'True if the spline is reticulated.'
|
is_array: true,
|
||||||
end
|
http_codes: [
|
||||||
put ':id' do
|
{ code: 200, message: 'get Splines' },
|
||||||
spline = (@@splines[params[:id]] || error!('Not Found', 404))
|
{ code: 422, message: 'SplinesOutError' }
|
||||||
spline[:reticulated] = params[:reticulated]
|
]
|
||||||
spline
|
get do
|
||||||
end
|
{ splines: @@splines }
|
||||||
|
end
|
||||||
|
|
||||||
desc 'Create a spline.'
|
#
|
||||||
params do
|
desc 'Return a spline.',
|
||||||
optional :reticulated, type: Boolean, default: true, desc: 'True if the spline is reticulated.'
|
http_codes: [
|
||||||
requires :required_group, type: Hash do
|
{ code: 200, message: 'get Splines' },
|
||||||
requires :required_param_1
|
{ code: 422, message: 'SplinesOutError' }
|
||||||
requires :required_param_2
|
]
|
||||||
|
params do
|
||||||
|
requires :id, type: Integer, desc: 'Spline id.'
|
||||||
|
end
|
||||||
|
get ':id' do
|
||||||
|
error!({ code: 422, message: 'SplinesOutError' }) unless @@splines[params[:id]]
|
||||||
|
{ "declared_params" => declared(params), spline: @@splines[params[:id]] }
|
||||||
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
desc 'Create a spline.',
|
||||||
|
http_codes: [
|
||||||
|
{ code: 201, message: 'Spline created' }
|
||||||
|
]
|
||||||
|
params do
|
||||||
|
requires :spline, type: Hash do
|
||||||
|
requires :x, type: Numeric
|
||||||
|
requires :y, type: Numeric
|
||||||
|
end
|
||||||
|
optional :reticulated, type: Boolean, default: true, desc: 'True if the spline is reticulated.'
|
||||||
|
end
|
||||||
|
post do
|
||||||
|
spline = params[:spline]
|
||||||
|
x = (spline[:x]/spline[:y] || 0.0)
|
||||||
|
y = (spline[:y]/spline[:x] || 0.0)
|
||||||
|
|
||||||
|
spline = {
|
||||||
|
id: @@splines.size + 1,
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
reticulated: params[:reticulated]
|
||||||
|
}
|
||||||
|
|
||||||
|
@@splines[spline[:id]] = spline
|
||||||
|
|
||||||
|
{ "declared_params" => declared(params), spline: spline }
|
||||||
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
desc 'Update a spline.'
|
||||||
|
params do
|
||||||
|
requires :id, type: Integer, desc: 'Spline id.'
|
||||||
|
optional :spline, type: Hash do
|
||||||
|
optional :x, type: Numeric
|
||||||
|
optional :y, type: Numeric
|
||||||
|
end
|
||||||
|
optional :reticulated, type: Boolean, default: true, desc: 'True if the spline is reticulated.'
|
||||||
|
end
|
||||||
|
put ':id' do
|
||||||
|
error!({ code: 422, message: 'SplinesOutError' }) unless @@splines[params[:id]]
|
||||||
|
|
||||||
|
update_data = params[:spline]
|
||||||
|
spline = @@splines[params[:id]]
|
||||||
|
|
||||||
|
spline[:reticulated] = !!update_data[:reticulated]
|
||||||
|
spline[:x] = update_data[:x]/update_data[:y] || 0.0
|
||||||
|
spline[:y] = update_data[:y]/update_data[:x] || 0.0
|
||||||
|
|
||||||
|
@@splines[params[:id]] = spline
|
||||||
|
|
||||||
|
{ "declared_params" => declared(params), spline: @@splines[params[:id]] }
|
||||||
|
end
|
||||||
|
|
||||||
|
desc 'Delete a spline.'
|
||||||
|
params do
|
||||||
|
requires :id, type: Integer, desc: 'Spline id.'
|
||||||
|
end
|
||||||
|
delete ':id' do
|
||||||
|
error!({ code: 422, message: 'SplinesOutError' }) unless @@splines[params[:id]]
|
||||||
|
|
||||||
|
@@splines.delete_if { |key, _| key == params[:id] }
|
||||||
|
|
||||||
|
{ "declared_params" => declared(params), spline: "#{params[:id]} deleted" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
post do
|
end
|
||||||
spline = { id: @@splines.size + 1, reticulated: params[:reticulated] }
|
|
||||||
@@splines[@@splines.size + 1] = spline
|
|
||||||
spline
|
|
||||||
end
|
|
||||||
|
|
||||||
desc 'Return all splines.'
|
|
||||||
get do
|
|
||||||
@@splines.values
|
|
||||||
end
|
|
||||||
|
|
||||||
|
class FileAccessor < Grape::API
|
||||||
# TEST api for testing uploading
|
# TEST api for testing uploading
|
||||||
# curl --form file=@splines.png http://localhost:9292/splines/upload
|
# curl --form file=@splines.png http://localhost:9292/file/upload
|
||||||
desc 'Update image'
|
namespace :file do
|
||||||
post 'upload' do
|
desc 'Update image'
|
||||||
filename = params[:file][:filename]
|
post 'upload' do
|
||||||
content_type 'application/octet-stream'
|
filename = params[:file][:filename]
|
||||||
env['api.format'] = :binary # there's no formatter for :binary, data will be returned "as is"
|
content_type 'application/octet-stream'
|
||||||
header 'Content-Disposition', "attachment; filename*=UTF-8''#{URI.escape(filename)}"
|
env['api.format'] = :binary # there's no formatter for :binary, data will be returned "as is"
|
||||||
params[:file][:tempfile].read
|
header 'Content-Disposition', "attachment; filename*=UTF-8''#{URI.escape(filename)}"
|
||||||
|
params[:file][:tempfile].read
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
add_swagger_documentation
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,5 +6,40 @@ use Rack::Cors do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# run Api.new
|
||||||
|
|
||||||
|
require 'grape'
|
||||||
|
require '../lib/grape-swagger'
|
||||||
|
|
||||||
require './api'
|
require './api'
|
||||||
run Api.new
|
|
||||||
|
class Base < Grape::API
|
||||||
|
format :json
|
||||||
|
|
||||||
|
mount Api::Root
|
||||||
|
mount Api::Splines
|
||||||
|
mount Api::FileAccessor
|
||||||
|
|
||||||
|
before do
|
||||||
|
header['Access-Control-Allow-Origin'] = '*'
|
||||||
|
header['Access-Control-Request-Method'] = '*'
|
||||||
|
end
|
||||||
|
|
||||||
|
# global exception handler, used for error notifications
|
||||||
|
rescue_from :all do |e|
|
||||||
|
raise e
|
||||||
|
error_response(message: "Internal server error: #{e}", status: 500)
|
||||||
|
end
|
||||||
|
|
||||||
|
add_swagger_documentation :format => :json,
|
||||||
|
:hide_documentation_path => true,
|
||||||
|
:api_version => 'v1',
|
||||||
|
:info => {
|
||||||
|
title: "Horses and Hussars",
|
||||||
|
description: "Demo app for dev of grape swagger 2.0"
|
||||||
|
}
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
run Base.new
|
||||||
|
|
|
@ -0,0 +1,146 @@
|
||||||
|
{
|
||||||
|
"id": "1a2c4a65-9c99-3435-17db-307763b28fd1",
|
||||||
|
"name": "grape-rackup",
|
||||||
|
"description": "",
|
||||||
|
"order": [
|
||||||
|
"cf633582-c2ea-cb44-24d7-d64e73a65049",
|
||||||
|
"60bd637b-7b77-b280-d4f7-3d3d38cd86ef",
|
||||||
|
"4780e2ef-f05d-f464-9d18-538c5960be3c",
|
||||||
|
"f83c2241-c239-13ea-bffb-255436e95863",
|
||||||
|
"0648649b-9e54-1bc5-f835-2c690f67d47a"
|
||||||
|
],
|
||||||
|
"folders": [],
|
||||||
|
"timestamp": 1453407636651,
|
||||||
|
"owner": "",
|
||||||
|
"remoteLink": "",
|
||||||
|
"public": false,
|
||||||
|
"requests": [
|
||||||
|
{
|
||||||
|
"id": "0648649b-9e54-1bc5-f835-2c690f67d47a",
|
||||||
|
"headers": "Content-Type: application/json\n",
|
||||||
|
"url": "http://localhost:9292/splines/1",
|
||||||
|
"preRequestScript": "",
|
||||||
|
"pathVariables": {},
|
||||||
|
"method": "DELETE",
|
||||||
|
"data": [],
|
||||||
|
"dataMode": "params",
|
||||||
|
"version": 2,
|
||||||
|
"tests": "",
|
||||||
|
"currentHelper": "normal",
|
||||||
|
"helperAttributes": {},
|
||||||
|
"time": 1453408596777,
|
||||||
|
"name": "http://localhost:9292/splines",
|
||||||
|
"description": "",
|
||||||
|
"collectionId": "1a2c4a65-9c99-3435-17db-307763b28fd1",
|
||||||
|
"responses": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "4780e2ef-f05d-f464-9d18-538c5960be3c",
|
||||||
|
"headers": "Content-Type: application/json\n",
|
||||||
|
"url": "http://localhost:9292/splines/1",
|
||||||
|
"pathVariables": {},
|
||||||
|
"preRequestScript": "",
|
||||||
|
"method": "GET",
|
||||||
|
"collectionId": "1a2c4a65-9c99-3435-17db-307763b28fd1",
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"key": "required_group[required_param_1]",
|
||||||
|
"value": "sadf",
|
||||||
|
"type": "text",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "required_group[required_param_2]",
|
||||||
|
"value": "fgsd",
|
||||||
|
"type": "text",
|
||||||
|
"enabled": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dataMode": "params",
|
||||||
|
"name": "http://localhost:9292/splines",
|
||||||
|
"description": "",
|
||||||
|
"descriptionFormat": "html",
|
||||||
|
"time": 1453408415061,
|
||||||
|
"version": 2,
|
||||||
|
"responses": [],
|
||||||
|
"tests": "",
|
||||||
|
"currentHelper": "normal",
|
||||||
|
"helperAttributes": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "60bd637b-7b77-b280-d4f7-3d3d38cd86ef",
|
||||||
|
"headers": "Content-Type: application/json\n",
|
||||||
|
"url": "http://localhost:9292/splines",
|
||||||
|
"pathVariables": {},
|
||||||
|
"preRequestScript": "",
|
||||||
|
"method": "GET",
|
||||||
|
"collectionId": "1a2c4a65-9c99-3435-17db-307763b28fd1",
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"key": "required_group[required_param_1]",
|
||||||
|
"value": "sadf",
|
||||||
|
"type": "text",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "required_group[required_param_2]",
|
||||||
|
"value": "fgsd",
|
||||||
|
"type": "text",
|
||||||
|
"enabled": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dataMode": "params",
|
||||||
|
"name": "http://localhost:9292/splines",
|
||||||
|
"description": "",
|
||||||
|
"descriptionFormat": "html",
|
||||||
|
"time": 1453408387712,
|
||||||
|
"version": 2,
|
||||||
|
"responses": [],
|
||||||
|
"tests": "",
|
||||||
|
"currentHelper": "normal",
|
||||||
|
"helperAttributes": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "cf633582-c2ea-cb44-24d7-d64e73a65049",
|
||||||
|
"headers": "Content-Type: application/json\n",
|
||||||
|
"url": "http://localhost:9292/splines",
|
||||||
|
"pathVariables": {},
|
||||||
|
"preRequestScript": "",
|
||||||
|
"method": "POST",
|
||||||
|
"collectionId": "1a2c4a65-9c99-3435-17db-307763b28fd1",
|
||||||
|
"data": [],
|
||||||
|
"dataMode": "raw",
|
||||||
|
"name": "http://localhost:9292/splines",
|
||||||
|
"description": "",
|
||||||
|
"descriptionFormat": "html",
|
||||||
|
"time": 1453407769825,
|
||||||
|
"version": 2,
|
||||||
|
"responses": [],
|
||||||
|
"tests": "",
|
||||||
|
"currentHelper": "normal",
|
||||||
|
"helperAttributes": {},
|
||||||
|
"rawModeData": "{\n \"spline\":{\n \"x\":1.23,\n \"y\":3.14\n }\n}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "f83c2241-c239-13ea-bffb-255436e95863",
|
||||||
|
"headers": "Content-Type: application/json\n",
|
||||||
|
"url": "http://localhost:9292/splines/1",
|
||||||
|
"pathVariables": {},
|
||||||
|
"preRequestScript": "",
|
||||||
|
"method": "PUT",
|
||||||
|
"collectionId": "1a2c4a65-9c99-3435-17db-307763b28fd1",
|
||||||
|
"data": [],
|
||||||
|
"dataMode": "raw",
|
||||||
|
"name": "http://localhost:9292/splines",
|
||||||
|
"description": "",
|
||||||
|
"descriptionFormat": "html",
|
||||||
|
"time": 1453408494448,
|
||||||
|
"version": 2,
|
||||||
|
"responses": [],
|
||||||
|
"tests": "",
|
||||||
|
"currentHelper": "normal",
|
||||||
|
"helperAttributes": {},
|
||||||
|
"rawModeData": "{\n \"spline\":{\n \"x\":131.23,\n \"y\":93.14\n }\n}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -1,3 +1,6 @@
|
||||||
|
require 'active_support'
|
||||||
|
require 'active_support/core_ext/string/inflections.rb'
|
||||||
|
|
||||||
module Grape
|
module Grape
|
||||||
class Endpoint
|
class Endpoint
|
||||||
PRIMITIVE_MAPPINGS = {
|
PRIMITIVE_MAPPINGS = {
|
||||||
|
@ -98,8 +101,8 @@ module Grape
|
||||||
path.sub!('(.:format)', '')
|
path.sub!('(.:format)', '')
|
||||||
# ... format params
|
# ... format params
|
||||||
path.gsub!(/:(\w+)/, '{\1}')
|
path.gsub!(/:(\w+)/, '{\1}')
|
||||||
# set item from path, this is used for the definitions object
|
|
||||||
|
|
||||||
|
# set item from path, this could be used for the definitions object
|
||||||
@item = path.gsub(/\/\{(.+?)\}/, '').split('/').last.singularize.underscore.camelize || 'Item'
|
@item = path.gsub(/\/\{(.+?)\}/, '').split('/').last.singularize.underscore.camelize || 'Item'
|
||||||
@entity = route.route_entity || route.route_success
|
@entity = route.route_entity || route.route_success
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue