schemaless_rest_api 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 983c38081a6ee3d08a9d75b2e9425899329d1df050974088a5aac6eb21d924e5
4
- data.tar.gz: 2ed42db41c915a2106da88ac4d9179ea5bc55a8a594ec47dd0bc89f93f3ca8ef
3
+ metadata.gz: c80f60796bfe046a8af8d4e4068ae847379ab7a6a2f78b03e06732f2b6b1d8a6
4
+ data.tar.gz: 1473a6597e940ad4949b306d6cb72b5b49badda3ec3b16b2e118407eaefaa45d
5
5
  SHA512:
6
- metadata.gz: ceafb7b6ed5b550dd7aae21ab964ec181ccb2b82221aaaf931596c5a379bbd4a4881eecda5b9896fb4179adb8ab6f37197b320cf6018213fda5979e42c266474
7
- data.tar.gz: 276bb03925b12bc91bebeee8344b863033b237b336880a799ee7f309a90e27d4d8f86ab0a3c4a532e2e23cc001eacf1c9d9b14d6d3dd8991c694edad4407c449
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,27 +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
- 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?
55
64
  id = ""
56
65
  if data["id"]
57
66
  id = data["id"].to_s
@@ -64,6 +73,7 @@ class RestServer < Sinatra::Base
64
73
  else
65
74
  Entities.models[model][id] = data
66
75
  end
76
+ SchemalessRestApi.logger.info "Created #{id}, CorrelationId: #{request_id}"
67
77
  [201, JSON.generate({ id: id })]
68
78
  end
69
79
 
@@ -74,14 +84,9 @@ class RestServer < Sinatra::Base
74
84
  response["Access-Control-Allow-Headers"] = "*"
75
85
  end
76
86
 
77
- documentation "Retrieve all instances of #{model} or filtered by query param" do
78
- response "Data in #{model}"
79
- query_param :key_to_search_in_data, "Value of key"
80
- status 200
81
- end
82
87
  get "/#{model.downcase}" do
83
88
  response["Access-Control-Allow-Origin"] = "*"
84
- SchemalessRestApi.logger.info request.fullpath
89
+ SchemalessRestApi.logger.info "GET #{request.fullpath}"
85
90
  if ENV["mongodb"]
86
91
  if params == {}
87
92
  JSON.generate(MongoClient.get_all(model))
@@ -96,18 +101,13 @@ class RestServer < Sinatra::Base
96
101
  end
97
102
  return JSON.generate(matching_values)
98
103
  end
99
- rescue Exception => e
104
+ rescue StandardError => e
100
105
  [404, "Nothing found using #{params}. Only first param considered. #{e.message}"]
101
106
  end
102
107
 
103
- documentation "Retrieve specific instance of #{model} by id" do
104
- response "Data in #{model}"
105
- status 200
106
- status 404
107
- end
108
108
  get "/#{model.downcase}/:id" do |id|
109
109
  response["Access-Control-Allow-Origin"] = "*"
110
- puts request.path.downcase
110
+ SchemalessRestApi.logger.info "GET #{request.fullpath}"
111
111
  if ENV["mongodb"]
112
112
  results = MongoClient.find(model, id)
113
113
  return not_have(id) unless results.first
@@ -127,15 +127,9 @@ class RestServer < Sinatra::Base
127
127
  response["Access-Control-Allow-Headers"] = "*"
128
128
  end
129
129
 
130
- documentation "Update id of #{model} to be provided data" do
131
- payload "Whatever JSON you want updated to be. Needs to be valid JSON"
132
- response "Data in #{model}"
133
- status 204
134
- status 404
135
- end
136
130
  put "/#{model.downcase}/:id" do |id|
137
131
  response["Access-Control-Allow-Origin"] = "*"
138
- puts request.path.downcase
132
+ SchemalessRestApi.logger.info "PUT #{request.fullpath}"
139
133
  data = JSON.parse(request.body.read)
140
134
  if ENV["mongodb"]
141
135
  results = MongoClient.find(model, id)
@@ -150,13 +144,9 @@ class RestServer < Sinatra::Base
150
144
  204
151
145
  end
152
146
 
153
- documentation "Update id of #{model} to be deleted" do
154
- response "Empty"
155
- status 204
156
- end
157
147
  delete "/#{model.downcase}/:id" do |id|
158
148
  response["Access-Control-Allow-Origin"] = "*"
159
- puts request.path.downcase
149
+ SchemalessRestApi.logger.info "DEL #{request.fullpath}"
160
150
  if ENV["mongodb"]
161
151
  results = MongoClient.find(model, id)
162
152
  return not_have(id) unless results.first
@@ -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
+ };