-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathendpoints.rb
131 lines (115 loc) · 3.99 KB
/
endpoints.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'grape-entity'
require './api/entities'
module Api
class Spline
attr_accessor :id, :x, :y, :reticulated
end
module Endpoints
class Root < Grape::API
desc 'API Root'
get do
{
splines_url: '/splines',
file_url: '/file'
}
end
end
class Splines < Grape::API
@@splines = []
namespace :splines do
#
desc 'Get all splines',
is_array: true,
http_codes: [
{ code: 200, message: 'get Splines', model: Api::Entities::Splines },
{ code: 422, message: 'SplinesOutError' }
]
get do
present :items, @@splines, with: Entities::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
error!(code: 422, message: 'SplinesOutError') unless @@splines[params[:id] - 1]
present @@splines[params[:id] - 1], with: Entities::Splines
end
#
desc 'Create a spline.',
http_codes: [
{ code: 201, message: 'Spline created', model: Api::Entities::Splines }
]
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 = Spline.new
spline.id = @@splines.size + 1
spline.x = (params[:spline][:x] / params[:spline][:y] || 0.0)
spline.y = (params[:spline][:y] / params[:spline][:x] || 0.0)
spline.reticulated = params[:reticulated]
@@splines << spline
present spline, with: Entities::Splines
end
#
desc 'Update a spline.',
http_codes: [
{ code: 200, message: 'update Splines', model: Api::Entities::Splines },
{ code: 422, message: 'SplinesOutError' }
]
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] - 1]
update_data = params[:spline]
spline = @@splines[params[:id] - 1]
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
present spline, with: Entities::Splines
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] - 1]
@@splines.delete_at(params[:id] - 1)
{ 'deleted' => params[:id] }
end
end
end
class FileAccessor < Grape::API
namespace :file do
desc 'Update image',
details: "# TEST api for testing uploading\n
# curl --form file=@splines.png http://localhost:9292/file/upload",
content_type: 'application/octet-stream'
post 'upload' do
filename = params[:file][:filename]
content_type 'binary', '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
end
end
end