schemaless_rest_api 0.2.1 → 0.3.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: 772e554afa1851f8a505754ce97e174218f28b2fdfa7cb8cea9e20483fe1a0a0
4
- data.tar.gz: 442745cdc52411aa5e2db5d20fc9474188314ef94282db4b620675114a84f1ba
3
+ metadata.gz: 5970ba634fc9a47c750d7716b380b5af871942554a0f06b9927cb1f612a39d5d
4
+ data.tar.gz: 2b6734f5b0f5b102e39d488e8f27ca2270d3fdff20b0f3a04b4cdf903163e8d0
5
5
  SHA512:
6
- metadata.gz: 9c5f723fba1e431ee358afadfb6ed5d96bd081f8849d565fd23e31474c720c2ecf39b3f7e43e2eecef8bb896686e0e4d4a8a4837b4a1a35c565c253ab95015c1
7
- data.tar.gz: cb903faf022ce6d7605ae3ea60e570a548f2db2b5c246d67a1fff1c405b85854d49b4dde11b89390b7f2b855dcbf75f7e0ec74cfd3dc99b90c0f5ad2cdec902b
6
+ metadata.gz: f879739e486794d5464584cad902a764e0eeb8c9a90c1c92f607eef8773a2fb8739260bffff5b8a2c94bb0db2a594f6f267c8b97bd5df7bf7beef140efc00d64
7
+ data.tar.gz: f5ff9ddf4bf0f7f3870fb6a21354397913b8a6b158ca5dc0078e547d50b84097aa179dadae37daaa96960bb1c38cdbf828f7d4c77aff062115cdf4e61bb3a2d4
@@ -42,6 +42,13 @@ module MongoClient
42
42
  end
43
43
  end
44
44
 
45
+ if ENV['mongo_root_password']
45
46
  MongoClient.client = Mongo::Client.new(
46
47
  [ "#{ENV['mongodb']}:27017" ],
47
- :database => 'api')
48
+ database: 'api', password: ENV['mongo_root_password'],
49
+ user: 'root')
50
+ else
51
+ MongoClient.client = Mongo::Client.new(
52
+ [ "#{ENV['mongodb']}:27017" ],
53
+ database: 'api')
54
+ end
@@ -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,17 +43,33 @@ 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
- id = SecureRandom.uuid
32
52
  data = JSON.parse(request.body.read)
53
+ id = ''
54
+ if (data['id'])
55
+ id = data['id'].to_s
56
+ else
57
+ id = SecureRandom.uuid
58
+ data['id'] = id
59
+ end
33
60
  if ENV['mongodb']
34
61
  MongoClient.insert(model, data, id)
35
62
  else
36
63
  Entities.models[model][id] = data
37
64
  end
38
- [201, id]
65
+ [201, JSON.generate({ id: id })]
39
66
  end
40
67
 
68
+ documentation "Retrieve all instances of #{model} or filtered by query param" do
69
+ response "Data in #{model}"
70
+ query_param :key_to_search_in_data, "Value of key"
71
+ status 200
72
+ end
41
73
  get "/#{model.downcase}" do
42
74
  puts request.path.downcase
43
75
  if ENV['mongodb']
@@ -47,7 +79,7 @@ class RestServer < Sinatra::Base
47
79
  [200, JSON.generate(MongoClient.find_all(model, params))]
48
80
  end
49
81
  else
50
- return [200, Entities.models[model].to_s] if params == {}
82
+ return JSON.generate(Entities.models[model].values) if params == {}
51
83
 
52
84
  Entities.models[model].values.find_all { |val| val[params.keys[0]] == params.values[0] }
53
85
  end
@@ -55,6 +87,11 @@ class RestServer < Sinatra::Base
55
87
  [404, "Nothing found using #{params}. Only first param considered. #{e.message}"]
56
88
  end
57
89
 
90
+ documentation "Retrieve specific instance of #{model} by id" do
91
+ response "Data in #{model}"
92
+ status 200
93
+ status 404
94
+ end
58
95
  get "/#{model.downcase}/:id" do |id|
59
96
  puts request.path.downcase
60
97
  if ENV['mongodb']
@@ -68,6 +105,12 @@ class RestServer < Sinatra::Base
68
105
  end
69
106
  end
70
107
 
108
+ documentation "Update id of #{model} to be provided data" do
109
+ payload "Whatever JSON you want updated to be. Needs to be valid JSON"
110
+ response "Data in #{model}"
111
+ status 204
112
+ status 404
113
+ end
71
114
  put "/#{model.downcase}/:id" do |id|
72
115
  puts request.path.downcase
73
116
  data = JSON.parse(request.body.read)
@@ -83,6 +126,10 @@ class RestServer < Sinatra::Base
83
126
  204
84
127
  end
85
128
 
129
+ documentation "Update id of #{model} to be deleted" do
130
+ response "Empty"
131
+ status 204
132
+ end
86
133
  delete "/#{model.downcase}/:id" do |id|
87
134
  puts request.path.downcase
88
135
  if ENV['mongodb']
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SchemalessRestApi
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -6,9 +6,15 @@ require_relative "schemaless_rest_api/entities"
6
6
  require "json"
7
7
  require "securerandom"
8
8
 
9
+ def models_env_not_set
10
+ !ENV["models"] || ENV["models"].empty?
11
+ end
12
+
9
13
  def extract_models
14
+ return [] if (!Entities.models.empty? && models_env_not_set)
15
+
10
16
  error_msg = "Make 'models' environment variable an array (e.g ['model1', 'model2'])"
11
- raise "Please set 'models' ENV variable. #{error_msg}" if ENV["models"].nil?
17
+ raise "Please set 'models' ENV variable or create 'db.json'. #{error_msg}" if ENV["models"].nil?
12
18
 
13
19
  models = eval(ENV["models"])
14
20
  raise error_msg unless models.is_a? Array
@@ -16,6 +22,24 @@ def extract_models
16
22
  models
17
23
  end
18
24
 
25
+ SEED_FILE = 'db.json'.freeze
26
+
27
+ if File.exist? SEED_FILE
28
+ puts "Seeding db based on '#{SEED_FILE}'"
29
+ seed_data = JSON.parse(File.read(SEED_FILE))
30
+ seed_data.each do |entity, values|
31
+ Entities.models[entity.to_sym] = {} unless Entities.models[entity.to_sym]
32
+ if values.is_a? Array
33
+ values.each do |value|
34
+ next unless value['id']
35
+ Entities.models[entity.to_sym][value['id'].to_s] = value
36
+ end
37
+ else
38
+ puts "Entity 'entity' doesn't have array, skipping"
39
+ end
40
+ end
41
+ end
42
+
19
43
  extract_models.each do |model|
20
44
  Entities.models[model.to_sym] = {}
21
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemaless_rest_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-14 00:00:00.000000000 Z
11
+ date: 2024-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo