forked from ruby-grape/grape-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.rb
66 lines (57 loc) · 1.73 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'grape'
require '../lib/grape-swagger'
@@splines = {}
class Api < Grape::API
format :json
desc 'API Root'
get do
{ splines_url: '/splines' }
end
namespace :splines do
desc 'Return a spline.'
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.'
params do
requires :id, type: Integer, desc: 'Spline id.'
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
end
desc 'Create 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
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
# TEST api for testing uploading
# curl --form [email protected] http://localhost:9292/splines/upload
desc 'Update image'
post 'upload' do
filename = params[:file][:filename]
content_type 'application/octet-stream'
env['api.format'] = :binary # there's no formatter for :binary, data will be returned "as is"
header 'Content-Disposition', "attachment; filename*=UTF-8''#{URI.escape(filename)}"
params[:file][:tempfile].read
end
end
add_swagger_documentation
end