schemaless_rest_api 0.3.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c6884db983603d3fe99853d2daf2448c5a112535d427c4dcf93085c03d0d572
4
- data.tar.gz: 233cf6a115128268144a612ff02e5de53c32b4f1dc0cb3e46a53f15ebc776e9d
3
+ metadata.gz: c80f60796bfe046a8af8d4e4068ae847379ab7a6a2f78b03e06732f2b6b1d8a6
4
+ data.tar.gz: 1473a6597e940ad4949b306d6cb72b5b49badda3ec3b16b2e118407eaefaa45d
5
5
  SHA512:
6
- metadata.gz: 6fddf98f70498d9efee708f86ba28a3ddc4da8b53c029886c964ba8be2078d8bc459d025c50236bb11c9992a8a4ef5edd62d3a05491d8686ac481c2d6a61238a
7
- data.tar.gz: ad73ffe9c66723853b0e0a9dc6ecaeb30371b7b12ceb33267715de10dcac052bb3d2107ad3fd4d93bff978239077d2a9434e526b8a32dc550676145b4d0b7db9
6
+ metadata.gz: b9d2068d71bef0434faa40fe237864fcb6fbebedbb0109013d995055a26975ca4109817215840b2cc22b178efdb3cdc1924e19d1450fcdda251f2ef5aad89e6a
7
+ data.tar.gz: ad9734ec4b544a6a686e791cacbb45cd70234e071f6af8d6d2ced82624e401d03a7d77ba33001a2cd0529a0ace33205c28eed296b853a8e728ed396c2ff0a7b1
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "sinatra"
4
- require "docdsl"
5
4
  require "puma"
6
5
  require "route_downcaser"
7
6
 
@@ -12,16 +11,28 @@ class RestServer < Sinatra::Base
12
11
  set :bind, "0.0.0.0"
13
12
  use RouteDowncaser::DowncaseRouteMiddleware
14
13
 
15
- register Sinatra::DocDsl
14
+ SWAGGER_FILES = %w[index.css swagger.html swagger-initializer.js swagger-ui-bundle.js swagger-ui-standalone-preset.js swagger-ui.css]
16
15
 
17
- page do
18
- title "Schmaless REST API"
19
- header "REST API using models #{Entities.models.keys}"
20
- introduction "REST APIs allowing CRUD on:
21
- #{Entities.models.keys.join("\n")}"
16
+ SWAGGER_FILES.each do |filename|
17
+ get "/#{filename.gsub(".html", "")}" do
18
+ response["Access-Control-Allow-Origin"] = "*"
19
+ if filename.end_with? '.json'
20
+ content_type :json
21
+ elsif filename.end_with? '.css'
22
+ content_type :css
23
+ elsif filename.end_with? '.js'
24
+ content_type :js
25
+ end
26
+
27
+ File.read("./lib/schemaless_rest_api/swagger/#{filename}")
28
+ end
22
29
  end
23
30
 
24
- doc_endpoint "/docs"
31
+ get "/swagger.json" do
32
+ response["Access-Control-Allow-Origin"] = "*"
33
+ content_type :json
34
+ SwaggerBuilder.build_swagger_for(Entities.models, request.host_with_port)
35
+ end
25
36
 
26
37
  def has_id?(model, id)
27
38
  Entities.models[model].key?(id)
@@ -31,28 +42,25 @@ class RestServer < Sinatra::Base
31
42
  [404, JSON.generate({ error: "'#{id}' not found" })]
32
43
  end
33
44
 
34
- documentation "Get summary" do
35
- response "Basic basic summary of API"
36
- end
37
45
  get "/" do
38
46
  summary = { models: Entities.models.keys.to_s,
39
- docs_url: "<a href=/docs>/docs</a>" }
47
+ docs_url: "<a href=/swagger>Swagger docs</a>" }
40
48
  summary[:db] = MongoClient.client.summary.to_s if ENV["mongodb"]
41
49
  JSON.generate(summary)
42
- rescue Exception => e
50
+ rescue StandardError => e
43
51
  [500, e.message]
44
52
  end
45
53
 
46
54
  Entities.models.each_key do |model|
47
- documentation "Create instance of #{model}" do
48
- payload "Whatever JSON you want. Needs to be valid JSON"
49
- response "Id of created #{model}"
50
- status 201
51
- end
52
55
  post "/#{model.downcase}" do
53
56
  response["Access-Control-Allow-Origin"] = "*"
54
- SchemalessRestApi.logger.info "POST #{request.fullpath}"
55
- data = JSON.parse(request.body.read)
57
+ request_id = SecureRandom.uuid
58
+ headers["X-Request-Id"] = request_id
59
+ SchemalessRestApi.logger.info env
60
+ SchemalessRestApi.logger.info "POST #{request.fullpath}, CorrelationId: #{request_id}"
61
+ request_body = request.body.read
62
+ data = {}
63
+ data = JSON.parse(request_body) unless request_body.empty?
56
64
  id = ""
57
65
  if data["id"]
58
66
  id = data["id"].to_s
@@ -65,6 +73,7 @@ class RestServer < Sinatra::Base
65
73
  else
66
74
  Entities.models[model][id] = data
67
75
  end
76
+ SchemalessRestApi.logger.info "Created #{id}, CorrelationId: #{request_id}"
68
77
  [201, JSON.generate({ id: id })]
69
78
  end
70
79
 
@@ -75,11 +84,6 @@ class RestServer < Sinatra::Base
75
84
  response["Access-Control-Allow-Headers"] = "*"
76
85
  end
77
86
 
78
- documentation "Retrieve all instances of #{model} or filtered by query param" do
79
- response "Data in #{model}"
80
- query_param :key_to_search_in_data, "Value of key"
81
- status 200
82
- end
83
87
  get "/#{model.downcase}" do
84
88
  response["Access-Control-Allow-Origin"] = "*"
85
89
  SchemalessRestApi.logger.info "GET #{request.fullpath}"
@@ -97,15 +101,10 @@ class RestServer < Sinatra::Base
97
101
  end
98
102
  return JSON.generate(matching_values)
99
103
  end
100
- rescue Exception => e
104
+ rescue StandardError => e
101
105
  [404, "Nothing found using #{params}. Only first param considered. #{e.message}"]
102
106
  end
103
107
 
104
- documentation "Retrieve specific instance of #{model} by id" do
105
- response "Data in #{model}"
106
- status 200
107
- status 404
108
- end
109
108
  get "/#{model.downcase}/:id" do |id|
110
109
  response["Access-Control-Allow-Origin"] = "*"
111
110
  SchemalessRestApi.logger.info "GET #{request.fullpath}"
@@ -128,12 +127,6 @@ class RestServer < Sinatra::Base
128
127
  response["Access-Control-Allow-Headers"] = "*"
129
128
  end
130
129
 
131
- documentation "Update id of #{model} to be provided data" do
132
- payload "Whatever JSON you want updated to be. Needs to be valid JSON"
133
- response "Data in #{model}"
134
- status 204
135
- status 404
136
- end
137
130
  put "/#{model.downcase}/:id" do |id|
138
131
  response["Access-Control-Allow-Origin"] = "*"
139
132
  SchemalessRestApi.logger.info "PUT #{request.fullpath}"
@@ -151,10 +144,6 @@ class RestServer < Sinatra::Base
151
144
  204
152
145
  end
153
146
 
154
- documentation "Update id of #{model} to be deleted" do
155
- response "Empty"
156
- status 204
157
- end
158
147
  delete "/#{model.downcase}/:id" do |id|
159
148
  response["Access-Control-Allow-Origin"] = "*"
160
149
  SchemalessRestApi.logger.info "DEL #{request.fullpath}"
@@ -0,0 +1,16 @@
1
+ html {
2
+ box-sizing: border-box;
3
+ overflow: -moz-scrollbars-vertical;
4
+ overflow-y: scroll;
5
+ }
6
+
7
+ *,
8
+ *:before,
9
+ *:after {
10
+ box-sizing: inherit;
11
+ }
12
+
13
+ body {
14
+ margin: 0;
15
+ background: #fafafa;
16
+ }
@@ -0,0 +1,19 @@
1
+ window.onload = function() {
2
+ //<editor-fold desc="Changeable Configuration Block">
3
+
4
+ window.ui = SwaggerUIBundle({
5
+ url: "./swagger.json",
6
+ dom_id: '#swagger-ui',
7
+ deepLinking: true,
8
+ presets: [
9
+ SwaggerUIBundle.presets.apis,
10
+ SwaggerUIStandalonePreset
11
+ ],
12
+ plugins: [
13
+ SwaggerUIBundle.plugins.DownloadUrl
14
+ ],
15
+ layout: "StandaloneLayout"
16
+ });
17
+
18
+ //</editor-fold>
19
+ };