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)
|
||||
[Routes Configuration](#routes)
|
||||
[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.
|
||||
|
@ -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
|
||||
|
||||
See [CONTRIBUTING](CONTRIBUTING.md).
|
||||
|
|
119
example/api.rb
119
example/api.rb
|
@ -1,57 +1,118 @@
|
|||
require 'grape'
|
||||
require '../lib/grape-swagger'
|
||||
|
||||
@@splines = {}
|
||||
|
||||
class Api < Grape::API
|
||||
format :json
|
||||
# require 'active_support'
|
||||
# require 'active_support/core_ext/string/inflections.rb'
|
||||
|
||||
module Api
|
||||
class Root < Grape::API
|
||||
desc 'API Root'
|
||||
get do
|
||||
{ splines_url: '/splines' }
|
||||
{
|
||||
splines_url: '/splines',
|
||||
file_url: '/file'
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
class Splines < Grape::API
|
||||
@@splines = {}
|
||||
|
||||
namespace :splines do
|
||||
desc 'Return a spline.'
|
||||
#
|
||||
desc 'Get all splines',
|
||||
is_array: true,
|
||||
http_codes: [
|
||||
{ code: 200, message: 'get Splines' },
|
||||
{ code: 422, message: 'SplinesOutError' }
|
||||
]
|
||||
get do
|
||||
{ splines: @@splines }
|
||||
end
|
||||
|
||||
#
|
||||
desc 'Return a spline.',
|
||||
http_codes: [
|
||||
{ code: 200, message: 'get Splines' },
|
||||
{ code: 422, message: 'SplinesOutError' }
|
||||
]
|
||||
params do
|
||||
requires :id, type: Integer, desc: 'Spline id.'
|
||||
end
|
||||
get ':id' do
|
||||
@@splines[params[:id]] || error!('Not Found', 404)
|
||||
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
|
||||
spline = (@@splines[params[:id]] || error!('Not Found', 404))
|
||||
spline[:reticulated] = params[:reticulated]
|
||||
spline
|
||||
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 'Create a spline.'
|
||||
desc 'Delete a spline.'
|
||||
params do
|
||||
optional :reticulated, type: Boolean, default: true, desc: 'True if the spline is reticulated.'
|
||||
requires :required_group, type: Hash do
|
||||
requires :required_param_1
|
||||
requires :required_param_2
|
||||
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
|
||||
post do
|
||||
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
|
||||
# curl --form file=@splines.png http://localhost:9292/splines/upload
|
||||
# curl --form file=@splines.png http://localhost:9292/file/upload
|
||||
namespace :file do
|
||||
desc 'Update image'
|
||||
post 'upload' do
|
||||
filename = params[:file][:filename]
|
||||
|
@ -61,6 +122,6 @@ class Api < Grape::API
|
|||
params[:file][:tempfile].read
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
add_swagger_documentation
|
||||
end
|
||||
|
|
|
@ -6,5 +6,40 @@ use Rack::Cors do
|
|||
end
|
||||
end
|
||||
|
||||
# run Api.new
|
||||
|
||||
require 'grape'
|
||||
require '../lib/grape-swagger'
|
||||
|
||||
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
|
||||
class Endpoint
|
||||
PRIMITIVE_MAPPINGS = {
|
||||
|
@ -98,8 +101,8 @@ module Grape
|
|||
path.sub!('(.:format)', '')
|
||||
# ... format params
|
||||
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'
|
||||
@entity = route.route_entity || route.route_success
|
||||
|
||||
|
|
Loading…
Reference in New Issue