schemaless_rest_api 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/schemaless_rest_api/rest_server.rb +42 -1
- data/lib/schemaless_rest_api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a86e21c3979d2542206f5c993491444ee5e91e38d348eaaf4cc4a6a242eaf11f
|
4
|
+
data.tar.gz: 2c82b7556b1ed4ce916224bdcc9404efe20283d7a7c7f2564bc750968b9f9cc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abfb1f463d723d366327e16067e751e2e616d7026adabd83365ddf0f5841e69f422ebcfbb1a8b7a6c756606241b8d400052ac496bbf011e9bd807ced28c0ebfb
|
7
|
+
data.tar.gz: 37a864e55ffd3740404a7ce7d632c3496c9d1e546d4e9a75092a5dfe3655e127a69f2a82c0253f7dccff5faaa7dcb5559549e702f0da760aaad36696300f55a5
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "sinatra"
|
4
|
+
require 'docdsl'
|
4
5
|
require "puma"
|
5
6
|
require 'route_downcaser'
|
6
7
|
|
@@ -10,6 +11,17 @@ class RestServer < Sinatra::Base
|
|
10
11
|
set :bind, "0.0.0.0"
|
11
12
|
use RouteDowncaser::DowncaseRouteMiddleware
|
12
13
|
|
14
|
+
register Sinatra::DocDsl
|
15
|
+
|
16
|
+
page do
|
17
|
+
title "Schmaless REST API"
|
18
|
+
header "REST API using models #{Entities.models.keys}"
|
19
|
+
introduction "REST APIs allowing CRUD on:
|
20
|
+
#{Entities.models.keys.join("\n")}"
|
21
|
+
end
|
22
|
+
|
23
|
+
doc_endpoint "/docs"
|
24
|
+
|
13
25
|
def has_id?(model, id)
|
14
26
|
Entities.models[model].key?(id)
|
15
27
|
end
|
@@ -18,8 +30,12 @@ class RestServer < Sinatra::Base
|
|
18
30
|
[404, JSON.generate({ error: "'#{id}' not found" })]
|
19
31
|
end
|
20
32
|
|
33
|
+
documentation "Get summary" do
|
34
|
+
response "Basic basic summary of API"
|
35
|
+
end
|
21
36
|
get "/" do
|
22
|
-
summary = { models: Entities.models.keys.to_s
|
37
|
+
summary = { models: Entities.models.keys.to_s,
|
38
|
+
docs_url: '<a href=/docs>/docs</a>' }
|
23
39
|
summary[:db] = MongoClient.client.summary.to_s if ENV['mongodb']
|
24
40
|
JSON.generate(summary)
|
25
41
|
rescue Exception => e
|
@@ -27,6 +43,11 @@ class RestServer < Sinatra::Base
|
|
27
43
|
end
|
28
44
|
|
29
45
|
Entities.models.each_key do |model|
|
46
|
+
documentation "Create instance of #{model}" do
|
47
|
+
payload "Whatever JSON you want. Needs to be valid JSON"
|
48
|
+
response "Id of created #{model}"
|
49
|
+
status 201
|
50
|
+
end
|
30
51
|
post "/#{model.downcase}" do
|
31
52
|
id = SecureRandom.uuid
|
32
53
|
data = JSON.parse(request.body.read)
|
@@ -38,6 +59,11 @@ class RestServer < Sinatra::Base
|
|
38
59
|
[201, id]
|
39
60
|
end
|
40
61
|
|
62
|
+
documentation "Retrieve all instances of #{model} or filtered by query param" do
|
63
|
+
response "Data in #{model}"
|
64
|
+
query_param :key_to_search_in_data, "Value of key"
|
65
|
+
status 200
|
66
|
+
end
|
41
67
|
get "/#{model.downcase}" do
|
42
68
|
puts request.path.downcase
|
43
69
|
if ENV['mongodb']
|
@@ -55,6 +81,11 @@ class RestServer < Sinatra::Base
|
|
55
81
|
[404, "Nothing found using #{params}. Only first param considered. #{e.message}"]
|
56
82
|
end
|
57
83
|
|
84
|
+
documentation "Retrieve specific instance of #{model} by id" do
|
85
|
+
response "Data in #{model}"
|
86
|
+
status 200
|
87
|
+
status 404
|
88
|
+
end
|
58
89
|
get "/#{model.downcase}/:id" do |id|
|
59
90
|
puts request.path.downcase
|
60
91
|
if ENV['mongodb']
|
@@ -68,6 +99,12 @@ class RestServer < Sinatra::Base
|
|
68
99
|
end
|
69
100
|
end
|
70
101
|
|
102
|
+
documentation "Update id of #{model} to be provided data" do
|
103
|
+
payload "Whatever JSON you want updated to be. Needs to be valid JSON"
|
104
|
+
response "Data in #{model}"
|
105
|
+
status 204
|
106
|
+
status 404
|
107
|
+
end
|
71
108
|
put "/#{model.downcase}/:id" do |id|
|
72
109
|
puts request.path.downcase
|
73
110
|
data = JSON.parse(request.body.read)
|
@@ -83,6 +120,10 @@ class RestServer < Sinatra::Base
|
|
83
120
|
204
|
84
121
|
end
|
85
122
|
|
123
|
+
documentation "Update id of #{model} to be deleted" do
|
124
|
+
response "Empty"
|
125
|
+
status 204
|
126
|
+
end
|
86
127
|
delete "/#{model.downcase}/:id" do |id|
|
87
128
|
puts request.path.downcase
|
88
129
|
if ENV['mongodb']
|